views:

315

answers:

14

I don't know how to describe it, but I'm looking for a tool that makes suggestions to my code on how to be... more elegant.

For example to make a nested if() statement a switch statement. something like this. As I don't know how to describe my problem I don't know what to search for. I thing code beautifier do not specifically change statements, right?

thanks!

+6  A: 

Have a look at IntelliJ IDEA "Intentions":

cherouvim
That's pretty impressive. Things like `intentions/Control Flow/Replace If with Switch` really save a lot of time. Gotta look into idea again some time. +1
seanizer
A: 

Most IDEs will have formatting capability. Eclipse, which is a free, OpenSource IDE, will format Java soruce files

djna
+3  A: 

Except for the auto-formatting and style checking your IDE provides, you may be interested in a static code analysis tool like Crap4J, or something from this list of SCA tools for Java. They help you improve the readability of your code, and warn you when you use error-prone constructs. There are IDE plugins for most tools to let you find bad code as you write it.

gustafc
+1  A: 

You could use a tool like emmaecl, which integrates nicely with Eclipse.

You could also use something like Sonar which does quality analysis on your sources and provides metrics and hints on how to improve the code. Sonar is a web service you install. It would integrate nicely with an automated build tool like Hudson, but that's not required.

extraneon
+1  A: 

You seem to be looking for a tool that will help you improve not your code's visual appearance and consistency (this is what a beautifier does), but its higher-level structure. FindBugs is your friend. Besides locating real bugs in your code, it will also identify more than 80 bad coding practices and more than 60 dodgy constructs (see this list).

Diomidis Spinellis
A: 

Eclipse can format your code. You can customize it here: Window/Preferences/Java/Code Style

Maybe static code analysis tools like Findbugs or PMD could interesst you.

r3zn1k
+1  A: 

Have a look at FindBugs and PMD for improving the quality of your Java source codes.

Have a look at this post for beautifying your Java source codes.

Bytecode Ninja
A: 

Eclipse has a code "Clean Up" functionality. You can find it under

Window -> Preferences -> Java -> Code Style - Clean Up.

The nice thing about this is that you can check this preferences in with your project in SVN. This way if you have multiple people collaborating on a project you just define your code Clean Up options and maybe your Formatter options. This goes a long way to not having to define everything in coding guidelines (they are still necessary, but there's a lot of stuff you can now define via those options).

Herminator
+3  A: 

You might want to have a look into Netbeans. It provides code hints, which point out common practices and mistakes.

MicSim
+1  A: 

You can use Artistic Style (works for C, C++, C# and Java) which has many options you can set so you can set-up styl you like.

You can use it either from command line or if you look hard you might find plugins for the mainstream IDEs (I know about one for Qt Creator).

Palmik
A: 

You might also want to explore Checkstyle.

Adarsh R
A: 

Agree above all suggestions.

ps: Like manufaturing a Ferrari, handmade is inevitable for piece of art:)

卢声远 Shengyuan Lu
A: 

If using the IDE, then at least Netbeans and Eclipse both have functionality to clean up and reformat code.

Netbeans allows you to configure the code formatting to quite detailed level, i.e. including line break before the curly braces or not. Breaking long lines etc. Settings/Preferences->Editor->Formatting.

After configuration you just use ctrl+shift+f (cmd+shift+f) on your open file and all the crap gets automagically cleaned up.

I'm pretty certain the same thing is available for Eclipse also. Haven't dug into the configuration part, but formatting at least was available and was reformatting and cleaning up the code pretty neatly.

From commercial standalone tools jindent does the trick http://www.jindent.com/ for example.

Laazik
A: 

I'd look at Sonar if I were you (http://sonarsource.org), it uses many of the aforementioned tools, such as PMD, Checkstyle and Findbugs and gives you an intuitive overview of the quality of your code.

darri