9

C++ rant, but I'm sure it is similar in other languages.

I cannot stand +=,-=; I understand it is shorter, but for some reason I would prefer seeing: sum = sum + 1; or the like. Am I just too much of a newb to appreciate it, or is it just preference.

Comments
  • 0
    And apparently I can't edit the comment, meant to have a ? at the end.
  • 2
    @Marowski it's better to write shorter code.
  • 1
    In Java too, as like above comments, shorter the code, the better.
    But I prefer readable code:
    sum = sum + 1;
  • 0
    @naren that's readable code, a good programmer will write like that and try using some good editors they show errors if u use c =c +d. I use php storm for my php coding
  • 0
    @Marowski you will get used to it. I promise.
  • 1
    It's preference. I think the code is more readable your way but I always use ++ anyway.
  • 0
    After starting my job working in Delphi (object pascal), I never realized how much I miss "operator assign" operators.
  • 0
    I think you get a few millisecond performance benefit over +=
  • 0
  • 0
    It's splitting hairs, really, but that's what we do! I use += (or .= in PHP!) although it's just a preference. Shorter code would be the goal, but not at the expense of readability or maintainability.
  • 3
    The compiler should optimize both ways. It comes down to preference and readability. Some devs prefer short, dense code. That's fine for small teams that will be doing the maintenance for the life of the project. On larger teams, especially those that are distributed or contain a variety of skill levels, you're going to want to make sure the code conforms to the project's style guidelines. These guidelines will help the code seem much more uniform over the various modules, and will likely result in code that is a little verbose and easier to read for devs that are fresh on the ptoject. This will help the new guys as they are hired, and will help any future devs that inherit maintenance and new development.
  • 0
    It depends on the language. I cannot speak for all but I will say from a .Net C# perspective... ++ or += is fine for integer based additions and is technically better for efficiency. However, the worst thing you can do for efficiency is to do that with strings. When you use that for string concatenation you will create three strings in memory for each iteration in the stack thus being increasingly inefficient.
  • 0
    @simeg Not sure. I remember reading it a while back in the ol' days of PHP. Pretty sure ++$var is faster than $var++ too 😕
Add Comment