Friday, October 26, 2007

Five mistakes that will ensure project failure

The following is a very interesting article by Joel Spolsky who talks about the five-step guide to ensure project failure.

Five-easy-ways-to-fail

Tuesday, October 23, 2007

Proper use of Generics

Yesterday I was asked to review code written by an ex- colleague of mine for a particular module. As I was reviewing I came across these lines of code

Snippet of the code:-
--------------------------
Node node = // does something to the get the node;
List < object > nodes = new ArrayList < object >();
nodes.add(node);

As soon as I saw this I educated him on the proper use of generics. It should have been written this way

Proper snippet of the code:-
-----------------------------------
Node node = // does something to get the node
List < node > nodes = new ArrayList < node > ();
nodes.add(node);

This brings in type safety and the compiler ensures that you only add objects of type node into this list. As we all know that Java is a strongly typed language and the addition of generics to the Java API has only increased the type safety.

Thursday, October 18, 2007

An Excellent Article on Clean Code

I happened to come across this wonderful article about writing clean code and I thoroughly enjoyed reading it.

So here you go readers have a look at the article I am talking about
http://www.objectmentor.com/resources/articles/Clean_Code_Args.pdf

Let me know what you guys think? I definitely give this article a thumbs up!!!!!

Monday, October 15, 2007

Choosing the Right Collection

Here are my thoughts on choosing the right collection for your application written in Java although conceptually speaking I think the same can be applied to other programming languages as well because the concept of "data structures" are the same.

What are Collections?
-------------------------
Collections are essentially a group of elements in data structure terms and in Java its a group of objects which are provided by the java.util package. Essentially they are containers which hold a collection of objects and also provide functionality to modify the data structure dynamically.

Types of Collections
------------------------
Note:- Iteration order means the way in which the objects will be iterated over or the way in which the elements of the collection will be accessed or inserted into the collection.

List
  • ArrayList - Indexed Collection and Random Access List. Iteration order is insertion order.
  • LinkedList - A Doubly Linked List and Sequential Access with FIFO behaviour. Iteration order is insertion order.
  • Vector - Synchronized version of the ArrayList with FIFO behaviour. Iteration order is insertion order.

Set

  • HashSet - A Collection that does not hold any duplicates. Iteration order is undefined.
  • LinkedHashSet - A collection with a doubly linked list with FIFO behaviour. Iteration order is insertion order.

Map

  • HashMap - Key-Value Pair collection with no synchornization and permits nulls. Iteration order is undefined.
  • LinkedHashMap - Key-Value Pair Collection with a doubly linked list with FIFO behaviour. Iteration order is insertion order of keys or access order of keys.
  • Hashtable - Key-Value Pair collection with synchronization and does not permit nulls. Iteration order is undefined.

Generally speaking for most general purposes you can use ArrayList, Arrays and LinkedList for most applications however certain scenarios and use cases in an application demands that you use the right collection for efficiency purposes. The following are some scenarios I can think of where I choose an appropriate collection to get the job done.

Email Service: -

In some applications you may choose to send the email asynchronously in this kind of a scenario we can use an ArrayList where we add the email message to the list to be sent later.

System Properties : -

During an application startup most applications look for System properties defined in a properties file which allow the application to be initialized with the necessary system defined properties for the application to function properly. In this scenario we can use HashMap or even Hashtable according to the situation. HashMap is preferred except when you need to be thread safe or you dont want nulls.

User Management :-

Imagine you have a User who may have many roles in a system and the application must ensure that he must not have duplicate roles or in others words the same role should not be assigned to him twice. HashSet is a good candidate for such scenarios.

Guidelines:-

1) ArrayList and Vector give best performance because they access objects using index. Vector takes slightly more time but it is negligible.

2) LinkedList gives worst performance when accessing objects at the middle because it has to scan nodes to access objects.

3) HashSet gives better performance than TreeSet. Use TreeSet when you want ordering.

4) Use Hashtable when you want to have a thread safe collection otherwise use HashMap. Use TreeMap when you want ordering.

What does the readers of my blog have to say about this? Please feel free to post your comments on this topic.

Friday, October 5, 2007

Introduction to Agile Software Development

A request from a friend of mine who wanted to gain an insight into Agile software development and XP. I decided to post my thoughts in this blog. Here you go Prashanth this one is for you. I hope my friend and all the readers of my blog finds this post useful.

Summary
------------
"Agile" is a new buzzword for describing a bunch of related software development methodologies. Now you may ask what is a methodology? Well in short a methodology is a process for building software. There are many approaches to building software and many of these approaches have been formalized into published methodologies. The names of some of them are Waterfall, RUP (Rational Unified Process), XP (extreme programming), and Scrum.
Since this is a post about agile software development I will talk more about XP in depth. For more information on other agile methodologies please visit http://en.wikipedia.org/wiki/Agile_software_development.
All agile methodologies are based around the agile manifesto. For more info on agile maifesto please visit http://en.wikipedia.org/wiki/Agile_Manifesto.

In short agile processes are lightweight processes. When following an agile process, there is less emphasis on documentation and having everything finalized right from the beginning. Agile processes are designed to accommodate change. XP is probably the most well-known and the most popular example of an agile process. The other popular agile methodology is SCRUM.

Extreme Programming (XP) - An Agile Methodology
--------------------------------------------------------------
Overview
----------

  • Customer lists the features in the form of User stories that the software must provide.
  • Programmers break the user stories into standalone tasks and estimate the work needed to complete each task.
  • Customer chooses the most important tasks that can be completed by the next release keeping in mind the iteration cycle.
  • Programmers choose tasks and work in pairs.
  • Programmers write unit tests.
  • Programmers add features to pass unit tests.
  • Programmers fix features and tests as necessary, until all tests pass.
  • Programmers integrate code.
  • Programmers produce a released version.
  • Customer runs acceptance tests.
  • Version goes into production.
  • Programmers update their estimates based on the amount of work they've done in release cycle.
Phases of XP:-
----------------

Planning

  • User stories are written. (Customer writes the User stories)
  • Release planning creates the schedule.
  • Make frequent small releases. (At the end of every iteration)
  • The Project Velocity is measured.
  • The project is divided into iterations. (Iteration cycle is anywhere between 2 weeks to 4 weeks)
  • Iteration planning starts each iteration.
  • Move people around. (This promotes Collective code ownership)
  • A stand-up meeting starts each day. (SCRUM like meeting)
  • Fix XP when it breaks.

Coding

  • The customer is always available.
  • Code must be written to agreed standards.
  • Code the unit test first.
  • All production code is pair programmed.
  • Only one pair integrates code at a time.
  • Integrate often.
  • Use collective code ownership.
  • Leave optimization until last.
  • No overtime. (8 hours of work 5 days a week)

Designing

  • Simplicity.
  • Choose a system metaphor.
  • Use CRC cards for design sessions.
  • Create spike solutions to reduce risk.
  • No functionality is added early. Refactor whenever and wherever possible.
Testing

  • All code must have unit tests.
  • All code must pass all unit tests before it can be released.
  • When a bug is found, tests are created.
  • Acceptance tests are run often and the score is published.

Here are some really good links for some more info on Agile Software development.

Here are some excellent Books that you can read

  • Kent Beck. Extreme Programming Explained: Embrace Change.
  • Kent Beck. Test-Driven Development: By Example
  • Martin Fowler. Refactoring: Improving the Design of Existing Code

Friday, September 21, 2007

Programming to an Interface

This interesting post http://blog.sidu.in/2007/09/programming-to-interfaces-strikes-again.html inspired me to pen down my thoughts on this topic as well.

So here are my thoughts:-

1) What does it mean when you say "Program to an interface"? In order to answer this question lets take a look at the following snippet in Java.

First Snippet:-

public Animal {

public eat() {

// blah blah

}

}


public Horse extends Animal {

public eat() {

// blah blah

}

}

Second Snippet:-

public interface Animal {

void eat();

}


public class Horse implements Animal {

public void eat() {

//blah blah

}

}

Third Snippet:-

public interface Animal {

void eat();

}

public abstract class Mammal implements Animal {

public void eat() {

// some default implementation

}

}

In the above snippets Animal is a supertype and the way in which animals eat is different for eg:- a human being would chew and eat , a snake would swallow etc..... In this scenario you can achieve polymorphism via inheritance to solve this problem or an interface to decide on the implementation. The approach depends on the situation you are faced with. Inheritance promote code reuse when you have default implementations in a hierarchy however at the same time the subclasses are tightly coupled to the super class. Any change in the super class would propogate through the entire hierarchy which can become a maintainence nightmare if care is not taken.

On the other hand with interfaces you need to implement each method defined in all the classes that implement the interface. With interfaces you can have a class implementing one or more interfaces which provides flexibility. The interface approach also promotes the wrapper pattern.
Having said all this there is another approach you can take known as the skeletal implementation where you define the type using an interface and provide an abstract class that implements the interface with the default methods. This is very evident in the collection classes of the Java API.

So now we have three ways of solving the same problem..... What next? Finally in Java
When a class implements an interface, the interface serves as a type that can be used to refer
to instances of the class. A class that implements an interface should therefore say something
about what a client can do with instances of the class. It is inappropriate to define an interface
for any other purpose.

Conceptually speaking an interface is nothing but a supertype. The supertype could be a concrete class, an abstract class or the java construct "interface". Programming to an interface in Java does not mean having a Java interface for all classes unless the scenario demands it.