Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

A Validation of a Point in the CodeRanch Style Guide

 
Ranch Hand
Posts: 69
IntelliJ IDE Eclipse IDE VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been trying to make myself follow the CodeRanch style guide. I think it's improving the consistency and readability in my code.

This bit is right at the top. I ran into a situation last night that I think really validates it.

Section 1.1 - Indentation

//...
All if, while and for statements must use braces even if they control just one statement.i]

I'm working my way through Schildt's [i]Java A Beginner's Guide.
The exercise is on nested loops.

His code:


The issue I ran into is that I was confused as to where the println() statement fell. I thought that it fell inside the loop.

That's where this line in the guide comes into play. If Mr. Schildt had put braces in his for loop, it would have been obvious where the println() method fell.

This is how I wrote it:


Of course, writing this post has me rereading the guide and seeing things I missed before. I think I'm going to have to spend a little of my study time on it.
 
Marshal
Posts: 74778
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that indentation is confusing. You may see it in books for experienced people so as to save space on the page, but it is really confusing in a beginner's book.
 
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you cited, assuming that's how it appears in the book, is very poorly formatted.

Most people would have expected lines 7 to 11 to be indented one more level. This would make it clear that the closing brace on line 13 matches the opening brace at the end of line 6, not the one at the end of line 7. This would also make it clear that the closing brace on line 12 matches the opening brace on line 7.

Are you sure the code you cited was not an example of how poor indentation can make the code logic confusing and difficult to follow?


 
Douglas Knapp
Ranch Hand
Posts: 69
IntelliJ IDE Eclipse IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu,
You are correct, sir. Mr. Schildt did have another indent for lines 7 through 11. That was my mistake transcribing from the book. It does not detract from my point however. Braces on that for would have made the program that much more readable.
 
Junilu Lacar
Sheriff
Posts: 16767
281
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I would tend to agree with you about the readability.  Certainly, as a matter of style there should be braces around a if-statement's body. This makes it less likely to misinterpret which statements are part of the body versus those which are not. It also makes it less likely to introduce bugs later on if someone decides to add more statements to the if-statement's body and forgets to add braces.
 
reply
    Bookmark Topic Watch Topic
  • New Topic