Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Style Guide indentation  RSS feed

 
Peter Ream
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying to learn Java for the past several months. I read the style guide to start and feel it has some very good guidelines that i have been trying to follow. Particularly, the guide on indentation. I really like the guideline on lining up {}. I follow this and find it very easy to follow the code. In my learning experience, I lookup and follow many other peoples code. It appears that just about no one, except maybe on this site, follows this guideline. Why is that? I find myself, when using other's code correcting this before using.
 
Tim Cooke
Marshal
Posts: 3872
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter, welcome to the Ranch!

I assume you are referring to the different styles known as "Egyptian" or "C" style placement of braces?

There is no definitive right or wrong answer as both are acceptable for Java code. The Egyptian style is more common, and my preference, but even some of us Ranchers prefer the C style.

The most important factor to consider is that of consistency. If you are joining a team with a codebase that uses one or the other consistently then it is good style, even manners, to follow the same pattern.
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:
I assume you are referring to the different styles known as "Egyptian" or "C" style placement of braces?


It's also commonly known as the K&R Style, (after Brian Kernighan and Dennis Ritchie, authors of "The C Programming Language" in which this style is used).

I prefer this style, too. And yes, what's important is that you place your braces and indent consistently although I find anything other than K&R or C-Style/Allman/Horstmann confusing.

See https://en.wikipedia.org/wiki/Indent_style
 
Junilu Lacar
Sheriff
Posts: 11146
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And a big THANK YOU for paying attention to style.

Seems like we don't see many newbies pay attention to that kind of thing anymore these days. In my day, our instructors would hand our code listings back if they weren't properly indented, or if you did it often enough, they'd straight away give you an F for your non-effort.

Welcome to the Ranch!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!