Why I like Groovy

I have tried few languages on JVM - Scala, Jython, jRuby and finally Groovy. I really liked Groovy for the following reasons and I believe I will be sticking with Groovy for long,


* Nearly a superset of Java. I can use what I know already. I was able to start immediately with what I know and what I hate (boiler plates). Entry barrier for a Java programmer is very low.

* Reuse Java libraries which I already know. Other JVM languages also provide this.

* Tooling - Eclipse. I can use the tool which I used for over a decade.

* Flexibility - No need to use Groovy fully. I can switch between Java and Groovy wherever I need/like.

* Incrementally learn groovy - I don’t have to learn Groovy completely. Iteratively i learn new ways of doing things - if I like I will retain, otherwise I will throw away.

* Reduce noise and boiler plate - Finally I don’t have to define getter and setters, public key words etc. It reduces lot of noise and let me focus on the implementation. This leads me to write programs which are simple and readable.

* Dynamic typing - I feel light when I use dynamic typing.

* Closures

Java on steroids == Groovy!

Stack - Data structure and applications

Stack is the fundamental datastructure which all programmers must know. In realworld it resembles a stack of plates, letters etc. Its a LIFO(Last In First Out) datastructure, meaning last added element will be the first one to be removed. Stack supports two key operations - push() and pop(). push() adds an item to the stack and pop() removes the first item in the stack.

Applications of Stack:
* Expression evaluation
* BackTracking
* Memory Management
* Recursion
* Reversing a string
* Tower of Hanoi can be solved using Stack

Stack implementation using Array(Written in Groovy):

package datastructures.stack

class StackArrayImpl implements Stack {

	int size;
	private static final int DefaultSize = 10;

	Object[] elements

	public StackArrayImpl() {
		this.elements = new Object[DefaultSize]
	}

	@Override
	public Object push(Object data) {
		if (size == elements.length) {
			ensureCapacity()
		}
		elements[size] = data;
		size++
		return data;
	}

	@Override
	public Object pop() {
		if (size == 0) return null
		def result = elements[size]
		elements[size] = null
		size--
		return result;
	}

	private boolean ensureCapacity() {
		int newSize = elements.length * 2
		elements = Arrays.copyOf(elements, newSize);
	}

	@Override
	public int size() {
		return size;
	}

}



Stack implementation using List:

package datastructures.stack;

import java.util.LinkedList;
import java.util.List;

@SuppressWarnings("rawtypes")
public class StackListImplementation implements Stack {

	List elements;

	StackListImplementation() {
		elements = new LinkedList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Object push(Object data) {
		this.elements.add(data);
		return data;
	}

	@Override
	public Object pop() {
		if (this.elements.size() == 0) return null;
		Object result = this.elements.remove(0);
		return result;
	}

	@Override
	public int size() {
		return this.elements.size();
	}

}


Practice of TDD

As programmers our job is to manage complexity and change in our applications we develop. If we don’t manage both complexity and change well, It will kill us sooner or later. 

Test Driven Development (TDD) is a software development process in which you write failing tests before you write code. It is the automated tests which drive your development. You incrementally build a suit of tests around your code and that acts as a shield to avoid regressions. After you reach a critical mass of tests around your code, you can be confident to make major changes to your code. 

While solving a non trivial problem of huge size its difficult to find where to start. And given the complexity of the software applications, its un-human to keep all the state in our mind. TDD comes to rescue by incrementally evolving our software and you start with the test first. TDD is just not limited to unit tests, it also includes integration and automated acceptance tests.

What TDD can provide?
Incremental development of high quality code
Provides concrete evidence that our code works
Manage change effectively with the suit of tests and minimizing regressions


Red-Green-Refactor cycle:
1. Write test first - which is a mental dump of what you try to solve. Ignore compilation errors, syntax.
2. [RED] Make it compile by fixing compilation errors and creating dummy classes/methods. These tests would fail however, its okay. Unit tests will and fail upfront.
3. [GREEN] Make it run by fixing the code
4. [REFACTOR] Refactor - Eliminate duplication & Improve the design

Don’t be (just) a polyglot programmer

As programmers its good to stay in the bleeding edge and learn new languages. I have noticed that people learn one or two languages every year. But i think learning a language for the sake of learning (or due to hype) is a waste of time and energy. For e.g. If everyone talks about functional programming then don’t try to learn Haskell.

It’s true that learning a new language such as Haskell might broaden your mind and the way you write code. But as programmers our most important job is to “create” more and consume less i.e. “Create > Consume”.

I think for a programmer following two are important which gives satisfaction in the long run,

1. Learn the concepts
    If you learn the concepts then its easier to learn to write it in a different syntax. 

2. Build something useful (need not be great)
   
You can start building something and learn on the way. What is the point of learning something if you don’t use it constructively?

Following quote from Bruce Lee is relevant here,

“I fear not the man who has practiced 10,000 kicks once, but I fear the man who had practiced one kick 10,000 times.” ― Bruce Lee

If I translate to programmers - Don’t try to learn 10 different languages, instead master One language and do something useful with it.

Happy learning Creating!

Hello World!

Being a programmer I would like to start my post with Hello World!

In this blog I am going to share my learnings, rants and experience as a programmer (with appropriate tags For e.g. rants will contain the “rants” tag). 

In short blog will be an output of the following command :)

"cat /sundar/brain | grep -i programming > proglog"

I believe web is the most important platform of our time and since beginning I was always interested in web application development. For the past 8 years as a programmer I had many learnings which I attempt to share in this blog and keep it organized.

I professionally use Java to pay my bills and personally interested in Rails, HTML5, JavaScript, Linux and Scala. 

You can subscribe to RSS feed here.

Cheers