I++ vs i+=1
-
Given this hidiom started in C, you've got to be doubly careful because in C
int q = (++i + 4) * (i++-1);
isn't just hideous and ugly and stupid, its also undefined what the result is (up to and including segmentation faults, later untraceable off-by-one errors and nasal demons). It's one of the canonical DONT DO THIS of C programming (modifying a variable twice without a sequence point in between).
-
i like to think of it as cluebat through eslint gated checkins. ;-) make your code pass ESlint and you can check in.
of course that is my opinion and yours is allowed to differ
-
-
-
well there is the louisville slugger propped up in the corner of my cube with several brown stains on it...
it's been years since i've had to do more than mention it...
-
I like my
++
s and--
s!I find them easier to parse.
++i
is something I immediately recognize as "incrementi
by one", wherei += 1
takes a few precious milliseconds to parse.That said:
- I avoid using it anywhere where preincrement and postincrement really matter
- If I do use it in a situation where preincrement and postincrement matter I always put a large
ACHTUNG!
comment next to it with an explanation of the process - I'm a preincrement kind of guy
Also, I'm advocating for changing every
WARNING
withACHTUNG
. In my head,Warning
sounds like "mind the bear outside, it might get cranky if you poke at it".Achtung
sounds like "Oi! Smeghead! There's a fucking bear! Don't fuck with it or it might chew your head off!"
-
Then there is this:
int i=10; while (i --> 0) { // i goes to 0 // do something }
-
Also, I'm advocating for changing every WARNING with ACHTUNG. In my head, Warning sounds like "mind the bear outside, it might get cranky if you poke at it". Achtung sounds like "Oi! Smeghead! There's a fucking bear! Don't fuck with it or it might chew your head off!"
that made me laugh out loud in a meeting. well played you glorious bastard. :-P
-
*takes a bow*
Happy to be of service, m'lady.
-
-
changing every WARNING with ACHTUNG
Why not
BITCH
orCOMPLAIN
?int i=10;while (i --> 0) { // i goes to 0 // do something}
the evil ideas thread is :one or more arrows.xml:
-
they were red stains before they dried.
(the fact that they're not of primate origin has yet to come up. :-P )
-
-
naughty maymay. SPANK SPANK SPANK
-
Given this hidiom started in C, you've got to be doubly careful because in C
int q = (++i + 4) * (i++-1);
isn't just hideous and ugly and stupid, its also undefined what the result is (up to and including segmentation faults, later untraceable off-by-one errors and nasal demons). It's one of the canonical DONT DO THIS of C programming (modifying a variable twice without a sequence point in between).
This.
If you want to ban everything simply because it CAN be abused to write terrible/ugly/wrong code, then you'll have to ban every operator and keyword of every programming language.
-
so i forbid ++ and -- in projects i have control over. it's not that big a deal and it means you can't pull that shite on me.
this is the kind of shit that breeds resentment among coworkers. I have to deal with this occasionally with the lead devs where I work and it's super grating having to go back and rewrite perfectly good code to fit someone's personal whims.
-
Rather than banning a sometimes useful operator, it seems like a reason for mandatory code review
-
so i forbid ++ and -- in projects i have control over.
Nazi!
Objecting to abuse of the operator is one thing. Objecting to it's use entirely is entirely another thing. Teach people the proper usage of it, including what sequence points are, and then tell them that code that results in UB will be rejected.
-
In what language does that compile?
Did anyone point out yet that that is legal C++? IIRC the comma acts as a sequence point, and i is evaluated and the result thrown away, then foo is called.
-
strong systems Hungarian?
Ada, where you'd declare a new int type for your loop counter.
-
this is the kind of shit that breeds resentment among coworkers. I have to deal with this occasionally with the lead devs where I work and it's super grating having to go back and rewrite perfectly good code to fit someone's personal whims.
while i understand that, it's not purely my whim. It's the defined code standards for the project.
and as with all parts of the code standards I welcome input, and have made (and will make) changes based on feedback when a good idea comes along. I take input from the team and define the code style. and enforce the code style with eslint (or other syntax checker as appropriate for the language)
I try very much to not be the despot on high, so maybe ban was too harsh a word for that...?
-
you cannot keep BITCH in production's code that could be audited one day or another year.
The marketing department has been very clear about that.Complain ? Well, if you are not happy, just leave.
-
-
I figured, but I'd already preëmptively written "Hanzo'd" in a bunch of posts today.
-
-
besides, where would you want to use i++ that you couldn't trivially use i+=1 instead?
Loops.
Yes, you can use the i+=1, but the post-increment just looks cleaner.
edit: Hanzo'd
-
Loops.
example please?
because in every language i know of this is a valid construct:
for (int i = 0; i < 42; i+=1) { } int j = 0; while (j < 42) { j+=1; }; int k = 0; do { k+=1 } until (k >= 42);
-
Did you even read the rest of that post? Or did you just knee-jerk reply?
-
well given that i posted before your edit appeared on my screen in the rest of it.... no i didn't.
but i have now. and while i get your "it looks cleaner" argument i'm afraid i'm going to have to reject it because i subjectively think it looks dirtier.
no, i do. i+=1 makes mroe sense to me than i++ so i+=1 is cleaner and i++ is dirtier.
-
no, i do. i+=1 makes mroe sense to me than i++ so i+=1 is cleaner and i++ is dirtier.
X is better than Y because Y is worse than X!
No it isn't! Y is better than X because X is worse than Y!
The first rule of the tautology society is the first rule of the tautology society.
-
well given that i posted before you edited in the rest of it.... no i didn't.
No you didn't. Duckhorse just didn't deliver my post in a timely manner. My edit was made within a discominute of my initial post (damn that 54s ninja edit window). Your post was made 3 discominutes after my post. Ergo, my post should have been fully available to you, at least if Duckhorse wasn't, well … Duckhorse.
-
ok. granted... i'll edit my post as apropriate.
-
X is better than Y because Y is worse than X!
No it isn't! Y is better than X because X is worse than Y!
The first rule of the tautology society is the first rule of the tautology society.
yes, i did say it was subjective.
that's why i don't accept the "dirty looking/clean looking" argument theyre's always going to be differences of opinion.
-
yes, i did say it was subjective.
that's why i don't accept the "dirty looking/clean looking" argument the
yre's always going to be differences of opinion.FTFY
But that's the primary purpose for things like the post-/pre-increment operators. They're shortcuts to allow you to simplify / cleanup your code. Can they be abused? Sure. Many similar shortcuts can be abused (I'm looking at you, ternary operators). That doesn't mean they shouldn't be used.
-
I'm just mad that i** isn't more prevalent for squares ;)
[code]O=O;[/code]
-
they're one fracking character shorter.... is that one character really that big of a deal?
serious question there.
I'm looking at you, ternary operators)
i'm not a fan of those either.....
-
I'm just mad that i** isn't more prevalent for squares ;)
[code]*O=O*;[/code]
AAAAAAH! POINTER MATH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FLEEEEEEEEEEEEEEEEEEEEEEEEE!
-
I would argue that in a proper strongly-typed language, a integer used solely for indexing an array would not be the same type as an integer storing business-logic data. And attempting to mix them would throw an error.
Well, guess what: that's what iterators are good for ;)
[code]
#include <array>int main(void)
{
std::array<int, 5> arr = {42, 42, 42, 42, 42};
int i = arr.begin();
}
[/code]main.cpp:6:9: error: cannot initialize a variable of type 'int' with an rvalue of type 'iterator' (aka 'int *') int i = arr.begin();
-
var q = (++i + 4) * (i++-1);
At least they didn't do
code?++j:j--;[/code]
I'm guessing you forbid ternaries too though
-
while i understand that, it's not purely my whim. It's the defined code standards for the project.
and as with all parts of the code standards I welcome input, and have made (and will make) changes based on feedback when a good idea comes along. I take input from the team and define the code style. and enforce the code style with eslint (or other syntax checker as appropriate for the language)
I try very much to not be the despot on high, so maybe ban was too harsh a word for that...?
Is it in the code standards because of your preference or someone else's?
And I can't see how anyone would ever win the argument to change the standards if you have already decided that the idiomatic short form is worse than your preferred style of dubious benefit.
-
-
Why doesn't std::array special-case bool like std::vector does?
-
they're one fracking character shorter.... is that one character really that big of a deal?
Depends how often you need to increment/decrement variables. That "one extra character per op" can really add up.
i'm not a fan of those either.....
I didn't say I wasn't a fan of ternary operators, just that they are often abused. I use them, but with caution.
-
Achtung sounds like "Oi! Smeghead! There's a fucking bear! Don't fuck with it or it might chew your head off!"
it makes me think I need to shoot something with a minigun
-
-
I'm guessing you forbid ternaries too though
i'm not a fan of them, no. that one wouldn't pass code review, but a well used one would.
-
Is it in the code standards because of your preference or someone else's?
And I can't see how anyone would ever win the argument to change the standards if you have already decided that the idiomatic short form is worse than your preferred style of dubious benefit.
my initial preferences, yes. but over the years the style has been altered with many suggestions from other developers. barely a quarter of the original rules are still in place unaltered, and fully half of them are completely changed now.
as for the idiomatic short form... I'll be honest it is not the short form itself i'm attempting to ban. it's the inline use of it. If i found a linter that was able to discriminate between pre/postincerment as a statement and in an expression i'd lift the ban.
see my original example: example 1 would be allowed if the sylechecker could discriminate it from example 2. i haven't found any such stylechecker and because stylechecks are completely automated (so code reviews can focus on algorythms and correctness and not style) that means that both forms are not allwoed.
-
but a well used one would.
I mostly just use ternaries for null checks on initializing variables (in certain languages anyway...)
like [code] $theValueThatWeReallyNeedToHaveInThisVariable = array_key_exists($key, $arr) ? $arr[$key] : SOME_DEFAULT; [/code]
-
I mostly just use ternaries for null checks on initializing variables (in certain languages anyway...)
that's an acceptable solution, although where available it is prefered to use the null coalescing operator (?? in C#)
-
I begrudgingly use the int? syntax, but don't like it because it looks like Visual Studio is questioning my ability to choose/recognize my own variables' types.
The condescending jerk.