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.
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"
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.
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
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 -
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 ]
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
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
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.
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
Joined: Nov 22, 2008
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
Joined: Aug 05, 2001
I think this is purely personal preference. There isn't a better one. As long as the program work, that is it.
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.
Joined: Aug 05, 2001
In fact, maybe there should be some Ergonics research on this....
Joined: Jan 30, 2000
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.
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.
Joined: Aug 05, 2001
I am in the same shoes. Relatively new to Java, I probablly care more about syntx than style.
Joined: Dec 04, 2000
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.