views:

333

answers:

9
+8  Q: 

Java coding style

How do you keep yourself coding to standards? There is stylecop and resharper for C#. Are there any tools/eclipse plugins for code analisys in Java? Which of them do you use?

+4  A: 

Here at work we use Checkstyle to ensure that the code follows certain rules. Checkstyle is also available as plugin for Eclipse.

Ham
Link is not working to check and tried to configure in the Eclipse as well
harigm
Both links work for me. If there is a problem with installing the plugin on your machine it's not my fault. So why the downvote?
Ham
+8  A: 

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard. Checkstyle is highly configurable and can be made to support almost any coding standard. An example configuration file is supplied supporting the Sun Code Conventions. As well, other sample configuration files are supplied for other well known conventions.

PMD is integrated with JDeveloper, Eclipse, JEdit, JBuilder, BlueJ, CodeGuide, NetBeans/Sun Java Studio Enterprise/Creator, IntelliJ IDEA, TextPad, Maven, Ant, Gel, JCreator, and Emacs.

FindBugs uses static analysis to inspect Java bytecode for occurrences of bug patterns. Static analysis means that FindBugs can find bugs by simply inspecting a program's code: executing the program is not necessary. This makes FindBugs very easy to use: in general, you should be able to use it to look for bugs in your code within a few minutes of downloading it. FindBugs works by analyzing Java bytecode (compiled class files), so you don't even need the program's source code to use it. Because its analysis is sometimes imprecise, FindBugs can report false warnings, which are warnings that do not indicate real errors. In practice, the rate of false warnings reported by FindBugs is less than 50%.

The MYYN
PMD is integrated into jDeveloper, do you need to 'activate' it? It does it work by default?NM- I just looked at the http://pmd.sourceforge.net/integrations.html and it looks like it needs to be installed and then activated. I thought it was built in.
ProfessionalAmateur
+2  A: 

What's wrong with a bit of self-discipline? If you can't do it for something as trivial as formatting, I don't hold out much hope for other quality aspects of code.

Tom Hawtin - tackline
How can you be sure, that every single person in your team are aware of code conventions?
folone
@folone, when @Tim Hawtin - tackline runs his team's code through Checkstyle, it's no doubt 100% perfect every time. I wish I could say the same for my code.
Yar
There's a development philosophy that a human should never do what a computer can do. Yes, when writing code it adheres to conventions 95% of the time. Do I now spend an hour searching for that final 5% or do I just use a tool like 'jalopy' or the eclipse formatter to do it for me. I'll give you one guess.
bgiles
My philosophy is that the computer and its cretinous software can get the hell out of my way.
Tom Hawtin - tackline
A: 

We use Checkstyle at my company, only for javadoc checks though.

It is nice in the way that you can configure different modules to include stuff to check for, and also on what levels (as in protected, public, private etc).

A heads-up is that for javadoc-checking you need to explicitly state if a method is allowed to throw several exceptions. This is so because the creators think that it is bad design to have a method throw more than one exception (they are in other words trying to control design, not related IMHO).

Lars Andren
+4  A: 

Checkstyle and FindBugs and PMD. The latter two are much more than just style tools. But there is usually some effort to be put in, in order to get an agreement among the members of the team and, as well as for configuration of the tool.

bertolami
A: 

Having a coding standards document that is distributed to all of the developers in the team/department/organisation is a good start.

t0ne
I am not sure, how do you assure that everybody reads and remembers it, even after every little change. If you want to assure that people loose a lot of effort and motivation. Otherwise it will be just another document nobody reads.
bertolami
It's use has to be mandated. If you want to have a coding standard you need some way of describing that standard and a policy to ensure that everyone adheres to the standard. Your coding standard shouldn't have lots of little changes made to it either once published. I've found the best way is to have a high level agreement about basic coding standards (such as readability and clarity) and not worry too much about minutiae of bracket placement etc. YMMV. Obviously junior developers need more guidance than the more experienced.
t0ne
+4  A: 

In addition to the above, Jalopy is a good tool for formatting the code.

JoseK
A: 

The eclipse compiler warnings and code formatter does the job for us. Although I'm aware that checkstyle does quite a bit more semantic checking for actual design of code, rather than just laying it out sensibly.

Geoff
+3  A: 

You can have a look at Sonar. It's an open source project that really makes easy the execution of Checkstyle, PMD and Findbugs.

Simon Brandhof