Pages

Friday, July 11, 2014

Java8 Lambda expressions

What are lambda expressions :

Lambda expression is one of the new features introduced in Java8. Java is a strongly typed object oriented language. Functions were never first class citizens of Java language. The introduction of lambda expressions in Java8 enables treating functionality as method argument.  Lambdas do not actually allow functions to be passed around, but, they provide short cut to creating anonymous inner class containing a single method.  So, lambda expression is about writing concise and clear code. It also encourages functional style thinking.

How to use Java8 lambdas :

    1. Let’s look at an example of collection sorting: Collection.sort(List list, Comparator c )
    2. In Java7, you could either create seperate class which implements Comparator interface or create an anonymous inner class. Both of these solutions are verbose and looks cumbersome. Here is an example of using anonymous inner class for sorting of cityList by their name lenghts  :
Collections.sort(cityList, new Comparator(){
@Override
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
});
    1. The above code can be rewritten in Java8 using lambda expression as follows :
Collections.sort(cityList, (s1, s2) -> s1.length() - s2.length());


Writing lambda expressions :

    1. Lambda expression does not contain function declaration, name or a return type.
    2. It looks like a function. It’s input arguments need not have a type (but, you can supply one if you want).
    3. The type is inferred from context.
    4. The body of the function starts after the “->” sign
    5. If it is a simple/single line function then the keyword ‘return’ is not needed.
    6. If the body has a single statement (as in the example above), then, curly braces are not needed.
    7. If there is only a single input argument to the function, then, parenthesis are not needed.

Saturday, March 29, 2014

Test driven development, Automated unit testing and Continuous integration

Test driven development

Test driven development (TDD) is a development methodology where test cases drive development. The middle D in TDD signifies that tests drive development. You start by writing a test case, which will fail initially. You then define an interface and provide minimal implementation for the test to pass and then iterate over the implementation. By writing test cases first, you get better understanding of functional requirements and hence create better design.

Unit testing 

Cost of fixing a bug goes up exponentially based on when it is discovered. If a developer finds a bug when coding, it is very cheap to fix. If the bug is found by QE, it is more expensive to fix. If a customer finds a bug, it is expensive by an order of magnitude to fix it. Hence, even though there is upfront cost to creating unit tests, it will be easily recouped in a short period of time. Apart from cost savings, there are many other benefits to unit testing. It makes you create modular and cleaner interfaces. It increases your confidence in the product. It is especially helpful in ensuring functional integrity if your code base changing frequently.

Automated unit testing and Continuous integration

Automated unit testing is complementary to test driven development and can be carried out through continuous integration process. It consists of a servers which runs the build and then all unit tests either periodically or after every check-in. In addition to automated unit testing, continuous integration process can be setup to run a variety of static code analysis tools for finding bugs, checking code style, etc. It helps in monitoring and improving the quality of the code. Continuous integration also brings in the confidence that your code is ready to ship.