Tuesday, June 5, 2012

Coding Styles and Eclipse


When we are working in a group it is always important to keep a fix standard or a way of coding through the whole group. This increase the readability and maintainability of the code. In WSO2 we are given a set of coding standards. In order to automate this process, you can configure your IDE to perform some formatting related tasks automatically. In Eclipse, we use Code Templates to configure IDE.

Code Cleanup Template


What is 'Code Cleaning up' ? This refers to the way that eclipse cleanup your code when cleanup command is given. It remove all the unnecessary parts and unstandardized parts according to the configuration given. We are given this configuration through a XML file (https://sites.google.com/a/wso2.com/engineering/Home/eclipsecodeformattingtemplatesforwso2developers/WSO2_Eclipse_Code_Cleanup.xml). This sets below configuration.

  • Change non static accesses to static members using declaring type
  • Change indirect accesses to static members to direct accesses (accesses through subtypes)
  • Convert control statement bodies to block
  • Convert for loops to enhanced for loops
  • Remove unnecessary parentheses
  • Add missing '@Override' annotations
  • Add missing '@Override' annotations to implementations of interface methods
  • Add missing '@Deprecated' annotations
  • Remove unnecessary casts
  • Remove unnecessary '$NON-NLS$' tags
  • Add unimplemented methods
  • Remove trailing white spaces on all lines
  • Correct indentation
If you want you can have your own profile, and even you can export it. You can find how to do it in the following article http://www.ibm.com/developerworks/library/os-eclipse-clean/

Code Formatting Template


This template takes care of, how formatting is happen in the code level. Indentation, Braces, white spaces, comments, etc. are formatted according to the given configuration. Again this formatting template is also given to us in WSO2 as a template (https://sites.google.com/a/wso2.com/engineering/Home/eclipsecodeformattingtemplatesforwso2developers/WSO2_Eclipse_Code_Formatter.xml).

In Order to apply these templates, follow the steps given below.

  1. Window -> Preferences
  2. Java-> Code Style
  3. Select Clean up option in the preference page and point to the WSO2_Eclipse_Code_Cleanup.xml.
  4. Select Formatter option in the preference page and point to the WSO2_Eclipse_Code_Formatter.xml
In order to make our life easy we can use eclipse shortcuts : http://www.a2ztechguide.com/2011/08/eclipse-ide-keyboard-shortcuts-for.html

1 comment: