aspose file tools*
The moose likes Java in General and the fly likes what sort of indenting you prefer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "what sort of indenting you prefer?" Watch "what sort of indenting you prefer?" New topic
Author

what sort of indenting you prefer?

Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Hi ranchers,
I found it interesting that many C/C++ programmers indent their coding in the following way ----
Option A

while many java programmers follow ---
Option B

Please tell me which one do you prefer in the following format (your language, option). Like in my case it is --- (java, B).
If you are a multi-language programmer then tell whether in all languages you follow the same indentation style or different?
Waiting for your interesting answers....
[I added [code] tags to preserve indentation. Duh. - Jim]
[ January 28, 2003: Message edited by: Jim Yingst ]

Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
I like the first style you mentioned.. but now I'm getting more accustom to the other indented because you can tell what type of block the bottom brace matches up with.
-Dale


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Peter Phung
Ranch Hand

Joined: Dec 06, 2001
Posts: 138
As a programmer who has written programs in both languages, i have used both styles of indenting. I don't really have any preference which style is used as long as the code is clear and easy to read, but i must admit that i use the second style more.


Pete<br />"Reality is an illusion <br />brought on by a lack of <br />drink, drugs and smut"
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
In C/C++ this is almost a religious war. I think most "old school" C programmers prefer the style of Option B. BUT, that's only because of a misunderstanding about typographic conventions.
Books on programming that include source code, including C and Java, try to minimize space as much as possible. Conrast this to actual program source code, in which you should use blank space liberally to make your source code readable.
In a book on programming, a method foo() might be written like this:

In the interest of clarity, I would rather write it like this:


Clearly, when the goal is to minimize space, the first method meets that goal. This is the typographic standard used pretty much in books on Java. But because most books use the first style, most people assume that is the "right" way of doing it.

However, if your goal is clarity, I think having the braces line up make it clearer where a block of code starts and ends. Also, liberal use of space makes it easier to read.


Rob
SCJP 1.4
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
I use the second style mostly. I find it easier to read and understand as opposed to having a brace floating in a line all by its lonesome.
As far as telling what line the closing brace belongs to you just make sure it lines up with the beginning of the line that the opening brace is on. That's fairly easy with most compilers now-a-days too.
just my $.02


Dave
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

I usually prefer style A, though this is not necessarily C/C++ style. All time classic K & R, I believe, uses style B.
Style A definitely contributes more to readability of the code, there's no doubt about it, at least in my mind.
The followers of style B have one strong point in their favour though, most C, C++ and Java programmers are used to putting a semi-colon at the end of the line. With style B, it doesn't harm much. Consder this -

Versus

Besides, the certification exams have code in style B, so I am getting used to it now.
- Manish
[ March 18, 2002: Message edited by: Manish Hatwalne ]
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Excellent responses from you dear fellows. And what Rob & Manish told are particularly knowledgable for me....
Despite the option A 's clarity, option B seemed to me just beautiful to look at!
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Fully agree.
Option A is more clarified.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Option B was not just invented to save line space in books, it was also used to save line space on those old terminals (VT100s come to mind). 24 lines isn't that much you know. It's the de-facto standard for C and C++ simply because the creators of those language said so, i.e. they used it in their language reference books consequently.
Option A originates in ALGOL60 and later Pascal. Simply because a lot of people started off with Pascal and switched to C/C++/Java later, they carried over their style to their new language.
kind regards
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Of course in REAL LIFE you usually do not get to choose what style you prefer. As a rule you have to follow the style guide of the shop that you work in.
For instance the JavaRanch Style Guide (also known as the Chicken Coop) states that you should use style A.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
And the Sun style guide states you should use B.
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Rob Ross:
Books on programming that include source code, including C and Java, try to minimize space as much as possible. Conrast this to actual program source code, in which you should use blank space liberally to make your source code readable.
In a book on programming, a method foo() might be written like this:


I have seen books (Core Java 2, for one) that use a variation of A to minimize space:

Personally, I like option A because your eyes can line up the braces more easily. Similarly, when space is critical, I prefer the variation above.


Java API Documentation
The Java Tutorial
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Of course in REAL LIFE you usually do not get to choose what style you prefer. As a rule you have to follow the style guide of the shop that you work in.
Dunno about that. In many places I've worked, there's been a choice. Sometimes this is because there is no style guide; sometimes it's because the style guide leaves this decision up to individual developers; sometimes it's because the guide (or at least, this part of the guide) is not really enforced. Not that's I'd ignore a guide just because it's not being enforced, but if there's a large preexisting code base that doesn't follow the guide, it may be better to conform to the de facto style of the project. Depends on the situation. In any event, braces styles are pretty easy to change after the fact, with various tools like Jacobe or Eclipse's Java editor. So if an employer wants me to change styles, it's pretty easy to make them happy.
Personally, I have a slight preference for A, because (a) funtionally, it's useful to see braces line up, and (b) aesthetically, those trailing braces at the end of a line just look ugly to me. But I also value my screen space, and option B does help keep code more compact. The K&R style may have originated due to the size of VT-100 terminals and the economies of book publishing, but it has benefits for me on a modern computer as well. The more code I can see at once without having to use a scroll bar, the better. So if someone wants me to use K&R on a project, I really don't mind much.
I've also sometimes been on teams that used the hybrid style Layne mentions:

This was pretty weird to get used to at first, but after a while I got to like it. You get both compactness and vertical alignment. The down sides are: (1) it may take a few more keystrokes to cut or past things to/from the first line, and (2) this style is not widely supported by tools like Jacobe or the Eclipse editor. (Or if it is, it's a recent addition.) The first is not a big deal, but the second could be a deal-breaker for me.
I'm also perfectly happy using horizontal alignment for short blocks:
orSure, this won't work if you need to add code later inside the block. But it's not a big deal to reformat later if necessary; I like the compactness I get now.
[ January 29, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Cindy Glass:
Of course in REAL LIFE you usually do not get to choose what style you prefer. As a rule you have to follow the style guide of the shop that you work in.

Well, in real-real life you'd use an indentation tool just before you check your code in (in CVS for example) and just after you've checked the stuff out; that way, nobode cares about other peoples indentation style, while the repository keeps all the stuff in one uniformly agreed upon style.
kind regards
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I think this is purely personal preference. There isn't a better one. As long as the program work, that is it.
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
What a tame religious war....
Death to style A users! Style B is the one true style! :-)
Seriously, the most important thing is to be consistent, with style A, B, or anything else.

--Mark
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
In fact, maybe there should be some Ergonics research on this....
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Seriously, the most important thing is to be consistent, with style A, B, or anything else.
Heck no! The important thing here is to switch back and forth at random. This makes it easier to detect those fellow programmers who become confused and upset by this sort of thing, so you can cull them from the herd.
Matt O'Toole
Greenhorn

Joined: Feb 02, 2002
Posts: 23
As a relative beginner I have no preformed opinion or habits. However, I've taken to Style B, because the issue for me is not where a code block starts or stops, but what line it belongs to. I find other people's code easier to read this way, too. I can understand why people prefer Style A, but that seems more to do with the extra whitespace created by putting the first brace on its own line. I suppose you could get the same effect by just skipping lines with Style B.
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
I am in the same shoes. Relatively new to Java, I probablly care more about syntx than style.
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Originally posted by Jim Yingst:
Heck no! The important thing here is to switch back and forth at random. This makes it easier to detect those fellow programmers who become confused and upset by this sort of thing, so you can cull them from the herd.

Jim, you might appreciate this web site about How To Write Unmaintainable Code
.
--Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what sort of indenting you prefer?