It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Does everybody seriously follow the Style Guide? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Does everybody seriously follow the Style Guide?" Watch "Does everybody seriously follow the Style Guide?" New topic
Author

Does everybody seriously follow the Style Guide?

Andrew Bedley
Greenhorn

Joined: Jun 07, 2011
Posts: 10
I am just starting out learning Java. It has been recommended that I follow the Java Programming Style Guide that is located on this website. A lot of it does immediately make sense and seems worthwhile.

There are two things I don't like and find a hassle. Now keep in mind I am currently a nobody at this stuff, but it makes me feel rebellious all the same:

1.
All indenting is done with spaces and not tabs.
Is this really done? I have read the reasoning about tabs being different in different programs or something, but man, that is rough. I haven't tried Notepad++, but in gedit in ubuntu, it gets frustrating in a hurry to hit the space bar 12 times on every line (or whatever), since there is no automatic indenting...

2.
Matching braces always line up vertically in the same column as their construct.
I too like the braces to match up, but I would rather they match up with the code that is written inside the construct rather than lining up with the construct itself. That way you can tell instantly what codes belongs in what group. I know you can do this more or less the same following the style sheet. However in Notepad++ you get a red line that traces between matching constructs, and then if the code touches the red line, then it is within the construct. I just took a javascript class, and we did not have specific style cues, so I kind of made my own up a little bit. I loved the fact that when "if statements" and other constructs were nested that red line was helpful for keeping the scope straight. To me that is the most important use of proper indentation, and it just seems easier.

My understanding is this is the right way for the braces:


This is how I would prefer, since it makes it clearer when you are debugging. All you do is follow down the curly brace without any counting backwards four spaces:
Andrew Bedley
Greenhorn

Joined: Jun 07, 2011
Posts: 10
Okay maybe I partly answered my own question. I found in gedit that there is a setting where you can push the tab button and it spits out 4 spaces instead of a tab symbol. I assume this is okay? This makes me feel much more relieved, because some efficiency alarm in my head was seriously ringing.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3605
    
  14

Yes, most editors allow you to automatically convert tabs to spaces.

Not everybody follows the style guide given here on this site. Most Java programmers put the starting brace on the same line as the declaring statement, like this:
Everyone has their way of writing things; what's important is that you adhere to the style that is considered the norm, when you are working in a group.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

I don't use the style guide that's here on JavaRanch. Which style I use depends on where I work and what agreements people have made about code style. The most important thing about coding style is that you choose one style and use it consistently - don't use one style here and another style there. Consistency makes your code much easier and pleasant to read.

When you work in a team together with other programmers you have to agree on a common style. I've worked on projects for many different companies and with many different people, and sometimes they use things in a style that I don't like, but I have to get used to their style and adopt it.

Andrew Bedley wrote:1.
All indenting is done with spaces and not tabs.
Is this really done? I have read the reasoning about tabs being different in different programs or something, but man, that is rough. I haven't tried Notepad++, but in gedit in ubuntu, it gets frustrating in a hurry to hit the space bar 12 times on every line (or whatever), since there is no automatic indenting...

gedit certainly supports automatic indenting and you can set it so that if you press tab it will insert spaces instead of a tab character. Look at the settings in gedit. But besides gedit there are many other programs which are better suited to editing source code, for example geany (an editor that has some IDE-like features), or a real IDE such as Eclipse, NetBeans or IntelliJ IDEA. These IDEs can format your code automatically, and there are extensive ways to customize how you want the code to be formatted.

Point 2: What you're proposing is an unusual style for braces. Most of the time the first style you mentioned is used:

Or the GNU / Unix / C style is used:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Andrew Bedley wrote:Does everybody seriously follow the Style Guide?

I suspect very few people follow the ranch style guide in all respects. And quite a few people aren't even aware of its existence, or don't care. It's just one set of opinions among many. It works well for some people, and many others use something else.

Most professional programmers learn to follow the stated style guide of whoever employs them - if there is a stated standard. Otherwise they do whatever the hell they want to. Which often matches the ranch style guilde in many ways, but rarely all of them.

Andrew Bedley wrote:
All indenting is done with spaces and not tabs.

Is this really done?

Yes - by a surprisingly large number of people. Almost every major organization I know of -- except Sun. Sun, in a remarkably stupid and boneheaded move, decided to embrace mixing tabs and spaces. While setting the tab size at 8, and the indent level at 4. So the first level of indentation is 4 spaces, and the next is 8 spaces, or 1 tab. And those had better line up. The next level is 12 spaces, or 1 tab and 4 spaces. And the next level is 16 spaces, or 8 spaces and 1 tab, or 2 tabs. And so on.

So yes, some of the folks running Sun were idiots. And they left their utterly idiotic stamp on much of the source code written by Sun employees. Which, despite its incredibly stupid indentation scheme, is fairly useful and important to be able to look at and understand. So here's what I do: leave my tabs set to 8 spaces, so I can read Sun's code. Never use tabs myself. And if anyone around me uses tabs for indentation, have them shot and killed. Repeatedly. Because it's a fundamentally stupid idea, at this point.

To be clear: if, in the beginning, everyone had decided to only use tabs for indentation, and no spaces - that would have worked out very well. But that's not what happened. The moment some people started mixing tabs and spaces, that required agreement on how many spaces a tab is worth. This led to the downfall of using tabs for spacing.

Use spaces, period. And if you indent at 4 spaces while someone else is used to indenting at two, well, they may whine about it a bit. But fundamentally, as long as they're not complete idiots, they can still read it.

Regarding point 2: brace wars are a huge waste of time. I don't care; any competent programmer should be able to read any of the major brace styles and NOT... WHINE... ABOUT... IT!!! and also, they should be able to adopt any of the common standards, and write code in that manner. And not whine about it. the important thing, from my point of view, is that at an organizational level, someone in authority needs to pick a standard for a given organization, and establish it. And enforce it. And then, everyone else needs to learn to not whine about it, and move on.

Having said that, there are only two standard brace placement strategies that I've seen widely adopted. Anything else seems like a pointless waste of time to me.
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

Definitely use consistent style within a team. And have a tool like CheckStyle check for inconsistencies.

If within a team yuo wuold agree to write all "uo's" as "uo", than that's a perfectly acceptable consistent form of communication, you see? I mean "yuo see?"

The next guy in the team who doesn't adhere to the style will make a few colleagues irritated and frustrated about how they have to re-format the code. And in some cases it will make "diff-ing" code a bit more difficult.

You will find yourself in situations where there is no styleguide available to you and there is no consistency within the code or the team. If you're in the position to change that, do it fast. If you're not, just stick to your own style and hope you don't have to look alot at other peoples code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
Stephan van Hulst wrote:Yes, most editors allow you to automatically convert tabs to spaces.. . .
You mean "all editors which are any good for programming". I do think gedit is a good programming editor.
Pene charl
Greenhorn

Joined: Jun 14, 2011
Posts: 21

If you follow the proper indentation and naming convention should be help to make your code read easily.
And it's show up your professional way of smart developer..


Regards:Prince Charles
http://www.princecharl.blogspot.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
If yuo (sic) look in Wikipedia, yuo find abuot eleven different indentation styles. The two usually used in Java™ are K&R and Allman.
Andrew Bedley
Greenhorn

Joined: Jun 07, 2011
Posts: 10
My thanks to all who replied.

I must say, Wow! This is quite the active, intelligent place.

I feel a lot more confident in the Style Guide after reading all these postings. The guide seems supported, defended and followed by many people.

Now that I realize that there are ways to make the tab do four spaces, I will follow that convention easily.

I will also fall into line with the parentheses. I can't wait for the day I am part of a team of coders, with stylistic conventions and everything!

Thanks,

Andrew
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Great! In the meantime, no matter how you choose to produce your indenting, do make sure it is consistent. (From the code you posted it looks like you're on track for that.) I've been answering questions on Java forums for a long time and it's amazing how much code you see on the forums which looks like it was indented by chimpanzees wearing boxing gloves.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

Paul Clapham wrote:... it's amazing how much code you see on the forums which looks like it was indented by chimpanzees wearing boxing gloves.

or worse...not indented at all


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
jake dickens
Ranch Hand

Joined: Mar 23, 2011
Posts: 30


Just depends on how you want to do it i use tabs when i code it's faster.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

jake dickens wrote:Just depends on how you want to do it i use tabs when i code it's faster.

It may be faster when you write it, but if you encounter the problems mentioned above, it can end up costing someone else orders of magnitude more time when they can't read your code.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

jake dickens wrote:i use tabs when i code it's faster.

I don't think anyone's suggesting you shouldn't use the Tab key when typing. But any decent editor can be set to convert that into spaces. So speed isn't relevant.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38100
    
  22
jake dickens wrote:

Just depends on how you want to do it i use tabs when i code it's faster.

Actually that code is awkward to read because of the lack of spaces. It should read. . . or whatever.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

I prefer the braces on a separate line, especially for methods. I'm not opposed to opening braces on the same line though. I've seen indenting braces occasionally, and it's jarring. Don't do that.

For the code base I'm working on now, the original developers followed a different standard: don't indent anything ever. It's full of deeply nested if/else structures too, which means it's almost unreadable. It's the best proof I've ever seen that coding standards are a good idea. Fortunately, most IDEs have a format or beautify option so you can reformat code into any indent and bracing style you prefer.
 
Consider Paul's rocket mass heater.
 
subject: Does everybody seriously follow the Style Guide?
 
Similar Threads
String to String array
doubt in indexOf()
Need a method for recognizing a pallindrome
Java Strings Getting a Substring
Exception