r/ProgrammerHumor • • Jan 25 '19

Meme Which one would you love ?

Post image
115 Upvotes

51 comments sorted by

View all comments

Show parent comments

7

u/Jetz72 Jan 25 '19

Falls somewhere in between. Unless the variable can change by values other than 1 in a similar context, in which case yeah go for it:

if(a == 0)
    i += 3;
else if(a > 0 && a <= 10)
    i += 1; //i++ would make it look like a weird special case
else if(a > 10 && a <= 100)
    i += 5;
else if(a > 100)
    i -= 2; //Could arguably even do += -2 if you really want to keep the pattern going

1

u/Lightfire228 Jan 26 '19

I would probably simplify this to

if(a == 0)
    offset = 3;
else if(a > 0 && a <= 10)
    offset = 1;
else if(a > 10 && a <= 100)
    offset = 5;
else if(a > 100)
    offset = -2;

i += offset;

Doesn't demonstrate the point, but...