Tuesday, April 13, 2010

Eclipse - code formatting for Beginner!

First things first! Eclipse is a Open source IDE, an Integrated Development Environment for development using languages like C, C++, PHP (both dynamic and static) for different platforms like mobile, embedded, thick and thin servers!

Two Myths broken!
1. It doesn't come with the development kit unlike Visual studio!
2. It is not for just Java.

So, with the SDK pre-installed on your machine, run your copy of eclipse that you can download for free at the eclipse project website.

This post will hence discuss about the Eclipse for J2SE although most features are same in J2EE version too!

One most important issue most developers like me spend time on is not on the code-logic but the alignment of the code! :)

So here's what you hate when you write your code in Eclipse - The alignment of the braces!

class HelloWorld{
   public static void main(String[] args){
       System.out.println("Hello World");
   }
}

which we always like to see as,


class HelloWorld
{
   public static void main(String[] args)
   {
       System.out.println("Hello World");
   }
}


Eclipse says YES to you! Go to the preferences dialog (Cmd + , in the mac) and search for 'formatter' in the filter text-box or browse your way through Java > Code Style > Formatter.




There you have it! Click edit and you can change EVERY aspect of code formatting from indentation to how your comments appear in your code window!

You might be interested in setting your braces as shown below: (The changes your make are immediately reflected on the preview pane on the right. This comes real handy!)

Also for beginners, this might be useful!

Unlike Visual studio, the code that you type in Eclipse is not auto formatted when you type the code, letting the choice to be yours!

If you want a section formatted properly, after typing in a hurry, you can at anytime select just the section  and hit cmd+shift+f to format it or without a select, if you perform the same command, the entire code is formatted.

You can also correct indentations alone with cmd + i. You can find these under the source menu in Eclipse!

1 comment:

  1. thanks for ur response.hey instead of giving tips if u write the basic ideas of eclipse in one post and if u give tips then and there it will be more helpfull nw and also for future reference.(like what nap did for VI).dnt ask whether i want u to feed the banana,it will be more usefull for future use tats y.keep going expecting more from u.

    ReplyDelete