I++ vs i+=1


  • SockDev

    Continuing the discussion from Today's security vulnerability: let's exploit hardware bugs!:

    @accalia said:

    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 with i++; it's always been clear to me that's the increment operator; similarly i-- 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 :fire: war, won't it? :laughing:



  • I'm using i++. Increment by one is a common usecase and it should rightly have its own idiom.


  • area_deu

    I use VB.NET most of the time at work.
    It only has += and no ++.


  • SockDev

    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



  • @accalia said:

    var q = (++i + 4) * (i++-1);

    Agreed. That's terrible.

    People, don't do that.

    You are not clever. You're annoying.


  • BINNED

    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


  • BINNED

    @accalia said:

    var q = (++i + 4) * (i++-1);

    var r = i++ == myNum ? i-- : 42;

  • SockDev


  • area_deu

    @accalia said:

    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.


  • SockDev

    @accalia said:

    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…


  • SockDev

    maybe, but at least i'm honest about it.

    besides, where would you want to use i++ that you couldn't trivially use i+=1 instead?


  • SockDev

    @RaceProUK said:

    I have a solution for people who write code like that…

    i have a similar solution... :imp:


  • BINNED

    My fix kept it nice and obvious by putting i++ on its own licne, with a comment explaining why ("Increment fileref", not "Increment i")


  • SockDev

    Personally, I only ever use the (de|in)crement operator as a post-op, and only with for loops, either in the for (;;) block itself, or (far less often) in the loop body. Anywhere else is a very deliberate exception.



  • @accalia said:

    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.



  • @accalia said:

    besides, where would you want to use i++ that you couldn't trivially use i+=1 instead?

    C++ iterators, where operator++ is supported but operator+= may not be, depending on what flavor of iterator you have.


  • SockDev

    why make the exception for loops?

    for (var i=0; i < 42; i+=1){
        foo(bar, i);
    }
    

  • BINNED

    @accalia said:

    foo(bar), i;

    In what language does that compile?


  • SockDev

    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.


  • SockDev

    @Jaloopa said:

    In what language does that compile?

    none, i @accalia'd. fixed.


  • SockDev

    @accalia said:

    why make the exception for loops?

    Force of habit, mostly; I'm used to just hammering out [sic] the standard for pattern :smile:


  • SockDev

    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.


  • SockDev

    I might do that. But then most of the loops I write don't use an indexer, which rather renders the whole point :cow: :smile:



  • I wonder how no one has yet mentioned that in C++, i++ is potentially a terrible bottleneck.

    @Jaloopa said:

    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.


  • SockDev

    @RaceProUK said:

    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.


  • Winner of the 2016 Presidential Election

    @accalia said:

    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 ++/--.


  • SockDev

    @accalia said:

    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) :stuck_out_tongue_winking_eye:
    @Gaska said:
    In C, comma is a valid binary operator, although useless. But C++ fixes its uselessness by letting the programmer overload it.


  • Discourse touched me in a no-no place



  • @Gaska said:

    In C, comma is a valid binary operator, although useless. But C++ fixes its uselessness by letting the programmer overload it.

    :fire: 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



  • @Gaska said:

    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<!-- (the IOCCC) -->.



  • @RaceProUK said:

    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.


  • SockDev

    Chaos forbid I should ever mix up and int and an int!



  • @RaceProUK said:

    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.


  • BINNED

    strong systems Hungarian?


  • area_deu

    That's a nice enterprisey approach.


    Filed under: [com.blakeyrat.enterprise.numbers.integers.BusinessInteger](#java)

  • SockDev

    @blakeyrat said:

    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 and i++ is a statement, not an expression.


  • SockDev

    That… is not why I summoned you :stuck_out_tongue:



  • 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.


  • I survived the hour long Uno hand

    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 = 5

    In 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);
    

  • Winner of the 2016 Presidential Election

    @blakeyrat said:

    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 in i.


  • SockDev

    @izzion said:

    Also, yes, you messed up.

    And that's why i ban post/pre increment/decrement!


  • SockDev

    @accalia said:

    And that's why i pan post/pre increment/decrement!

    Like the prospectors did when they first went West in search of gold? ;)



  • @aliceif said:

    That's a nice enterprisey approach.

    type Array_Index is range 0 .. Integer'Last
    

    How is that enterprisey?


  • SockDev

    -_-

    <!--that is *plenty* descriptive discourse!-->


  • IDK to me it seems like banning garlic in a restaurant because some chef's use it too much


  • SockDev

    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



  • @Jarry said:

    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.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.