• 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

what sort of indenting you prefer?

 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fully agree.
Option A is more clarified.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the Sun style guide states you should use B.
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is purely personal preference. There isn't a better one. As long as the program work, that is it.
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, maybe there should be some Ergonics research on this....
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am in the same shoes. Relatively new to Java, I probablly care more about syntx than style.
 
Mark Herschberg
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic