Pages

Thursday, December 29, 2011

A Look Into Java Annotations & A Real World Spring Example





An "annotation" is a type of programminglanguage definition and used as a “marker”. They can be thought as commentlines which programming language engine can understand. They don’t directlyaffect program execution but affect indirecly if wanted.

Definition

An annotation is defined with @interface keyword and is similar withan interface. It has attributes which are defined like interface methods.Attributes can have default values. Let’s define an annotation named “Page”,which defines UI pages of an application:

public @interface Page {
    int    id();
    String url();
    String icon()default "[none]";
    String name();default "[none]";
}

Usage

Annotations are widely used to informcompiler or compile-time/runtime/deployment-time processing.
Usage ofan annotation is simpler:

@Page(id=1, url=”studentView”, icon=“icons/student.png”,name=”Students”)
public class StudentWindow extends Window { … }

Annotationscan also be defined for methods and attributes:

@AnAnnotation
public String getElementName() {…}

@AnAnnotation(type=”manager”, score=3)
public int income;

Examples

1)   Reflection/code generation:

Methods havinga specific annotation can be processed at runtime:

public @interface MyAnnotation { ... }
public class TestClass {
    @MyAnnotation
    public static method1() { ... }
    @MyAnnotation
    public static method2() { ... }
    @MyAnnotation
    public static method3() { ... }
}

public static void main(String[] args) {
    for (Method method : Class.forName("TestClass").getMethods()) {
        if (method.isAnnotationPresent(MyAnnotation.class)) {
            // do what you want
        }
    }
}

2)   Spring bean configuration (this section requires Spring bean configurationknowledge):

Let’s useour “Page” annotation again:

package com.cmp.annotation;
public @interface Page {
    int    id();
    String url();
    String icon() default "[none]";
    String name(); default "[none]";
}

Say thatwe have a few classes having @Page annotation in a package:

@Page(id=1, url=”studentView”, icon=“icons/student.png”, name=”Students”)
public class StudentWindow extends Window { … }

If wedefine a bean configuration as below in a Spring application-context.xml file, Springwill create class instances “which has @Page annotation” placed in “givenpackage”.

<context:component-scan base-package="com.cmp.ui" annotation-config="true">
<context:include-filter type="annotation" expression="com.cmp.annotation.Page"/>
context:component-scan>

So, we havebeen enforced Spring to instantiate only a selection of classes at runtime.


For more detailed info about annotations, please refer to:

A Selection of Successful Software Engineering Posts (2011)





This selection contains 25 blog posts selected by CodeBuild which were placed on DZone in 2011.

Java
Producer and Consumer Pattern in Java
Quick Tip for Improving Java Apps Performance
JUnit Tutorial 2
Clientside Improvements in Java 6 & 7
Java Productivity Report 2011 (India vs Rest of the World)
Be a Better Java Programmer - A Reading List
Method Size Limits in Java
Why I Choose Java?
Best Practices List in Java
Lessons in Software Reliability
Music Components in Java Effects
WWW: World Wide Wait - A Performance Comparison

Software Engineering & Practices
The Principles of Good Programming
A Detailed Study on Understanding Code
Making Better Software - Forget New Features
Making Code Easy to Understand What Developers Want
Things to Remember When Doing Code Review
The Seven Phases of Introducing Continuous Integration

General
The Most Important Man in Tech Dies Last Week…It wasn't Steve Jobs.
The Top 10 Attributes of a Great Programmer
Build Facebook Bot in 2 Easy Hours
30 Free Programming E-Books
Signs That You're a Bad Programmer
A Complete List of All Major Algorithms (300)
10 Android Articles/Tutorials






Wednesday, December 28, 2011

Improve Software Quality with Tools and Processes





As most of the software developers know, software engineering is not just "coding". It's a complex process which requires engineering vision, analytical thinking, designing and software process management supported with tools. This article contains some important approaches, techniques and tools to improve software production quality.

Software quality depends on time and cost of course. But you can be sure that spent money and time will return as so much more. Quality standards will also be used for upcoming projects and increase company growth speed on midterm.




  • Choose a suitable process model and apply it correctly
As you know there are software process models like agilescrumiterative and incremental etc. Choose one of these according to the project type. You don't have to perform all rules strictly, you can modify most of them. The point is "using a process" here, for a systematic and ordered development.


  • Control the version of each required source
Using a version controlling system is a must. Even if only one developer exist, it must be used. Historical data, versioning, merging, ... is very important for increasing productivity. Otherwise developers will wait each other, can not detect performers and lines of previous changes, can not perform versioning systematically etc. SVNCVS and TFS are used for this purpose.

  • Track issues with easy-to-use tools
Parallel with using a process model, issue tracking tool usage is essential. JIRA-like tools are useful and easy-to-use. By tracking issues, you can track productivity and software growth and get production reports easily. Besides, those tools can be used to monitor developer work-hour productivity.

  • Perform and manage documentation
Perform documentation as required (e.g. %10 of total production time). No documentation is never a good solution as much as excessive documentation. It may include code documentation, requirement specifications, design documents, test documents, user manuals etc. Those documents are needed to be managed and shared also, by using version controlling tools or web based platforms (like Confluence). 

  • Use dependency management tools
Managing dependency libraries (library projects, jars, DLLs etc.) are a big problem especially for big projects. Configuring libraries to run the application after each release or after each project check-out is a hell. Use a dependency managing tool like Maven or at least auto build/copy script tools like Ant.

  • Use continuous integration
Building, deploying and versioning software is a big problem. Its time consuming and reduces productivity. Because of these, use a continuous integration tool (Hudson for example) and integrate it with dependency management or build tools (like MavenAnt, ..). The tool may be configured to perform a build on each commit, on clicking a button manually or on predefined periodic times...

  • Perform testing and integration testing constantly
Testing is very important for software quality. Test documentation which may consist test scenarios, results and relations with issues is required.  Also, testing (UI testing, integration testing, ...) must be performed constantly and periodically. Even if changing a single line of code may crush the whole system or crash a hidden functionality. for example, JUnit is very popular for Java applications.

  • Perform unit testing and automatize it
Unit testing is as important as the other testing methods. Unit tests provide pre-detection of most of the problems. By performing qualitative unit testing, time consumption for other testing methods also descreases. Automatizing these tests using continuous integration tools or at least command line tools (Hudson with Maven for example) are important to keep software consistency and reliability.

  • Collect metrics from production and use results
Coding metrics (e.g. line of code, abstraction ratio, cyclomatic complexity, ...) gives us some good viewpoints about software. For example, by using line of code maybe we can't (or we mustn't) determine the productivity of a developer; but we can determine the growth speed of software monthly. Complexity-like metrics tells us design errors before deployment. These metrics can be collected by tools or plug-ins (e.g. CodePro Eclipse plug-in). 

  • Follow best practices of coding and control with tools
There is no "golden rule" suitable with all software projects, but there are best practices for project management, architecture, designing, coding, testing for most situations. Performing those rules will increase the quality. For example, you can define rules for code production (about indentation, commenting, magic numbering, paranthesis etc.) and monitor convenience automatically and periodically with external tools or plug-ins like Maven Surefire Report plug-in.