• 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

Style Guide indentation

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
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

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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic