• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Readability

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is it that
nameLength=nameLength +1 ;
is not considered readable but:
nameLength=nameLength++ ;
is?
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pres,
Personally, I think the first is more readable than the second. But you will find neither version in any code I would write. It's more common to increment without using the assignment operator. In fact, in a code review you would probably be asked to change it to the following example.
E.g. nameLength++;
If I had to increment by a certain number (e.g. 2), then I would code it like
nameLength += 2;
-Peter
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement nameLength += 2; involves typing two fewer characters, but is otherwise no more effective than the longer version, nameLength = nameLength + 2; and is neither more nor less readable. Using the += assignment operator is a good practice, however. If nameLength were a more complex expression, such as nameLength[ temp.calculateOffset( 5 ) + blanks++ ].item, it is definitely more readable to express incrementing this value by 2 using the += form. This is because the original programmer runs the risk of not coding the nameLength... parameter exactly the same on both sides of the = sign, and because a maintainer has a much easier time of recognizing what is happening, without having to read both complex expressions to determine that they are the same.
 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I say that
nameLength += 2
is more readable than
nameLength = nameLength + 2
Even more so when you have something like this:
nameLengths[ index ] += 2
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic