Jeevadharani. R
Senior Trainer, ICTACT

To produce quality code, industry follows some best practices. We have been exploring them one by one. Already we have discussed about Coding Standards in the last issue. Now let us discuss about Code Generation.

Industry best practices

  1. Coding Standard

  2. Code Generation

  3. Unit Testing

  4. Code Coverage

  5. Code Analysis

  6. Code Profiling

Code Generation

By using the code generation strategies, we can produce higher-quality, more consistent code in less time. Code generation tools are used for generating code without manual typing. It enables software developers efficiently:

  1. Reduce repetitive coding.

  2. Generate code in less time with fewer bugs.

  3. Produce consistent code that adheres to the coding standards.

  4. Create custom templates for any language.

There are similar set of code snippets need to be created like JavaBeans, Data Access Object (DAO) etc. These code snippets can be generated based on the meta-data. As Java has become more matured, many frameworks and tools are available to generate code based on UML diagrams, DB schema and Plain Old Java Object (POJO).

Why code generation tools?

  1. To reduce the effort required for creating a module or project.

  2. To create bug free, maintainable quality code.

  3. To make the developer’s life easier.

Some of the code generation tools for Java

  1. Spring IDE

  2. iBatis

  3. SHRIMP

There are different types of strategies and requirements for code generation. Based on the requirement we can adopt any of the following code generation strategies.

  1. Code Templates

  2. Boiler Plate Code

  3. Framework Specific Code

  4. End to End code Generation

Code Templates

  1. Powerful IDE feature which can hold variables and link java constructs logically. For example the ‘catch’ template will intelligently add all exceptions that need to be caught.

  2. Developers can edit the existing templates or define new ones depending on the need.

  3. Commonly used code snippets such as logger, project specific exception handling, input validation, struts action classes, spring injection etc. can be defined as templates. The templates can then be exported or imported and shared across the project.

Read More

In addition to java pages, templates can be defined for xmls, ant, maven scripts, jsp etc.

CodeSmith Generator is a software development tool that makes the developer’s job easier and faster. Technically speaking, it is a template driven source code generator that automates the creation of common application source code for any language (C#, Java, VB, PHP, ASP.NET, SQL, etc.). CodeSmith Generator includes many useful templates as well as entire set of templates for generating proven architectures. Using CodeSmith Generator, any template can be modified or a custom template can be written to generate the code exactly the way it is required.

Boiler Plate Code

Boiler plate code such as delegate methods of the encapsulated variables, constructors, hash code and equals, getters and setters in JavaBean, toSting can be generated by right clicking the code editor in the NetBeans IDE-> Insert Code -> select appropriate option to generate.

  • To generate getters and setters, follow the steps below.

  • ŽŽ
  • In the NetBeans IDE, Create a java project create a Java class in the project.

  • ŽŽ
  • Declare the variables required.

  • ŽŽ
  • Right click on the code editor a popup menu will be displayed. From that menu choose Insert Code option.

  • ŽŽ
  • A sub-popup menu will appear. From the sub-popup menu, select Getter and Setter option.

  • ŽŽ
  • A dialog box will appear. It lists all the class member’ names. Select the required class members for which the getter and setter has to be generated or select the class name to select all the class members. Then click Generate button.

  • ŽŽ
  • Now the class will be filled with getters and setters for the selected class members.

Framework specific code

Most of the modern day frameworks such as Spring, Hibernate, IBatis, Struts have code generation support either out of the box or using IDE tool or script based tool. UML diagrams can be transformed to java code, so that the code conforms to the design and manual work is eliminated.

End to End code Generation

  • Frameworks such as AppFuse, Spring Roo, Sculpture help to generate projects using specified frameworks.

  • Generates code for all layers. In addition, it generates necessary build scripts, unit test cases, record and play integration test cases, etc.

With the help of these Code Generators, error free codes with industry standard can be generated faster and easier. As a next step in Generating Quality Code, Unit Testing will be discussed in the next issue.