I++ vs i+=1
-
Continuing the discussion from Today's security vulnerability: let's exploit hardware bugs!:
nah. i suggested it because of my hatred for the i++ idiom and added table for knights of the round.
seriously, just write it as
i+=1
because that's only one extra character and the meaning is much clearer!
I've never had an issue withi++
; it's always been clear to me that's the increment operator; similarlyi--
is the decrement. And whether it's pre- or post- is down to whether it's before or after the variable respectively.
This'll just be another war, won't it?
-
I'm using
i++
. Increment by one is a common usecase and it should rightly have its own idiom.
-
I use VB.NET most of the time at work.
It only has+=
and no++
.
-
yeah this will be a flame war i'm sure.
my problem isn't with the shortcut operator itself. it's the ability to embed it in other statements, and the reckless.abandon that some people do that to be "clever"
this doesn't bother me:
i++; var q = (i + 5) * i; i++;
but this does:
var q = (++i + 4) * (i++-1);
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.
Filed under: Unless i messed up those two segments should do the same thing to i and q
-
var q = (++i + 4) * (i++-1);
Agreed. That's terrible.
People, don't do that.
You are not clever. You're annoying.
-
I've always been fine with i++, although I don't like having it hidden within another method call. I've had to fix bugs before where someone replaced a
doSomething(someParameter, i++, someOtherParameter)
with other stuff, didn't notice the ++ in what they removed and ended up with weird side effects that weren't spotted for a few weeks
-
-
-
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.
You're awful.
-
my problem isn't with the shortcut operator itself. it's the ability to embed it in other statements, and the reckless.abandon that some people do that to be "clever"
Makes sense.
@Jaloopa said:I've had to fix bugs before where someone replaced a
doSomething(someParameter, i++, someOtherParameter)
with other stuff, didn't notice the ++ in what they removed and ended up with weird side effects that weren't spotted for a few weeks
I have a solution for people who write code like that…
-
maybe, but at least i'm honest about it.
besides, where would you want to use
i++
that you couldn't trivially usei+=1
instead?
-
-
My fix kept it nice and obvious by putting
i++
on its own licne, with a comment explaining why ("Increment fileref", not "Increment i")
-
Personally, I only ever use the (de|in)crement operator as a post-op, and only with
for
loops, either in thefor (;;)
block itself, or (far less often) in the loop body. Anywhere else is a very deliberate exception.
-
yeah this will be a flame war i'm sure.
There was some guy who showed up in the CS days and flamed about pre vs post.
I tend to use the
++
and--
operators in languages that use them, but I'm fine with+= 1
too.
-
besides, where would you want to use
i++
that you couldn't trivially usei+=1
instead?C++ iterators, where
operator++
is supported butoperator+=
may not be, depending on what flavor of iterator you have.
-
why make the exception for loops?
for (var i=0; i < 42; i+=1){ foo(bar, i); }
-
-
i would argue that operator overloading is it's own special evil. i do not like it, but i recognize that it is sometimes required for C++ programming.
there's a reason why i avoid C++ unless i have no other coice.
-
-
why make the exception for loops?
Force of habit, mostly; I'm used to just hammering out [sic] the standardfor
pattern
-
well..... you can change your habits. the easy way is to set your lint tool (lint, pylint, eslint, resharper, etc) to throw error on ++ or --. that'll force you to break that habit quickly.
and i would argue your code will be the better for it.
-
I might do that. But then most of the loops I write don't use an indexer, which rather renders the whole point
-
I wonder how no one has yet mentioned that in C++,
i++
is potentially a terrible bottleneck.In what language does that compile?
In C, comma is a valid binary operator, although useless. But C++ fixes its uselessness by letting the programmer overload it.
-
But then most of the loops I write don't use an indexer, which rather renders the whole point Moo
does it? i would think it renderes it trivial why havent you done it yet?!
:-P
-
I don't mind using pre-increment (++i), but never as a parameter. Essentially, parameters should not be clever. Returning temporaries or values and such is fine. Mutating state in a parameter is a breeding ground for bugs. The function call should do whatever the function does, not add surprise changes in the parameters.
-
yeah this will be a flame war i'm sure.
my problem isn't with the shortcut operator itself. it's the ability to embed it in other statements, and the reckless.abandon that some people do that to be "clever"
this doesn't bother me:
i++; var q = (i + 5) * i; i++;
but this does:
var q = (++i + 4) * (i++-1);
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.
<small> Filed under: Unless i messed up those two segments should do the same thing to i and q
I only find inline acceptable if it's in a for loop. Otherwise, screw you inline ++/--.
-
does it? i would think it renderes it trivial why havent you done it yet?!
You may be my queen, but that doesn't make you my queen (if you know what I mean)
@Gaska said:In C, comma is a valid binary operator, although useless. But C++ fixes its uselessness by letting the programmer overload it.
-
i++;
It's
++i;
dammit...
-
In C, comma is a valid binary operator, although useless. But C++ fixes its uselessness by letting the programmer overload it.
to anyone that overloads the comma operator
the problem you have it's called clever programmers. not ++ operator
related:
i once wrote:++i || i--
and tough it was clever. it isn't
-
In C, comma is a valid binary operator, although useless
Not true! It returns the value of the second argument, allowing you to put multiple unrelated expressions into the space of a single expression (I'm looking at you,
for(;;)
!), and acts as a sequence point for the few cases where that matters.
-
I've never had an issue with i++; it's always been clear to me that's the increment operator;
i++
is increment of an index.i += 1
is adding one to a int value stored in i. Don't mix them up.
-
Chaos forbid I should ever mix up and
int
and anint
!
-
Chaos forbid I should ever mix up and int and an int!
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.
-
strong systems Hungarian?
-
That's a nice enterprisey approach.
Filed under: [com.blakeyrat.enterprise.numbers.integers.BusinessInteger](#java)
-
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.
Is that a @ben_lubar I hear approaching…It is @ben_lubar that uses a language like that, isn't it?
-
Go doesn't have a
--i
or++i
operator andi++
is a statement, not an expression.
-
That… is not why I summoned you
-
I mean, it's useful to have IDs of different types of things be different types. That way, you can give the ID a method named Get that returns the object it is the ID of. And it means that you can have a function that takes an ID as a parameter but it gets typechecked by the compiler instead of just being a number.
-
Also, yes, you messed up.
Assume i = 3 at the start.
In the first example, you have:
i = 4
var q = (4 + 5) * 4 = 36
i = 5In the second example, you have:
var q = ((3+1) + 4) * (4 - 1) = 24
and then i gets set to 5 after being substituted the second time.What you wanted in the second example was either:
var q = (++i + 5) * i++;
or
var q = (++i + 5) * (++i - 1);
-
i++ is increment of an index. i += 1 is adding one to a int value stored in i. Don't mix them up.
Technically, you're adding an int value of
1
to a numeric value stored ini
.
-
-
And that's why i pan post/pre increment/decrement!
Like the prospectors did when they first went West in search of gold? ;)
-
That's a nice enterprisey approach.
type Array_Index is range 0 .. Integer'Last
How is that enterprisey?
-
-_-
-
IDK to me it seems like banning garlic in a restaurant because some chef's use it too much
-
well.... more like the chief chef banning shallots because they cost too much.
but, hey. i'm the chief chef of this kitchen! master of all that is within its walls! if i say that vidalia onion will be good enough then that's my choice is it not?
:-P
-
because some chef's use it too much
Some chef's parakeets use it too much? Some chef's sommeliers use it too much? Some chef's what?
-
it's your choice. that doesn't prevent me from saying that the solution IMHO it's to cluebat the devs. rather than baning things.