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
Coding Standard
Code Generation
Unit Testing
Code Coverage
Code Analysis
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:
Reduce repetitive coding.
Generate code in less time with fewer bugs.
Produce consistent code that adheres to the coding standards.
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?
To reduce the effort required for creating a module or project.
To create bug free, maintainable quality code.
To make the developer’s life easier.
Some of the code generation tools for Java
Spring IDE
iBatis
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.
Code Templates
Boiler Plate Code
Framework Specific Code
End to End code Generation
Code Templates
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.
Developers can edit the existing templates or define new ones depending on the need.
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.