Monday, August 27, 2007

Java :: FindBugs :: Dead Store

I ran FindBugs on one of our projects and I had a warning on some code similar to this, let's see how the code was written. This is just a sample code to show an example, don't expect any logic here :-)

// DeadStoreExample.java
import java.util.ArrayList;
import java.util.List;

public class DeadStoreExample {
public static void main(String[] args) {
List list = new ArrayList(); // Dead Store here.
list = getList();
System.out.println(list);
}

private static List getList() {
// Some intense operation and finally we return a java.util.List
return new ArrayList();
}
}

FindBugs reported me this problem: Dead store to list in DeadStoreExample.main(String[]). When I first saw it, I was like "What do you mean by Dead Store?" And when I looked into the code I saw a List object being created but not used. Instead, in the next line the reference variable is pointing to some other object in the heap. The object created on line number 6 is never used and hence it's a dead store.

So, what is a Dead Store? If we assign a value to a local variable, but the value is not read by any subsequent instruction, then it's called a Dead Store.

I love FindBugs, the thing that I like the most about FindBugs is that it tries to find all the lame bugs which many veteran programmers accidentally make. I always like to set my compiler warnings very high (even for my Javadoc comments). And now I have developed a habit of running FindBugs before I make a build or commit my code. It's one of those things like Unit Testing, you must try and get infected.

If a compiler or a tool can catch a bug, then I will always go for it. No one can write bug free code, at least I have not seen anyone till now. And I believe in automating anything that is possible so that we never have to do that manually again. That's why we have computers :-)

If you're not using FindBugs, I highly recommend that you to use it.

Sunday, August 26, 2007

Python :: Calculate the Size of a MySQL Schema Approximately

Hi,

Recently I wanted to find out the total size of a schema in a MySQL database. I searched for it but can't really find it. So, I wrote something on my own, really simple which doesn't really bother about a lot of error conditions. But I think that's ok, I mean really this is not a project. I am posting my code here now, feel free to optimize it and tweak it to your own requirement and let me know if you have a better solution :-)

#!/usr/bin/python

"""Calculates the total size of a MySQL database schema approximately."""

import MySQLdb
import getpass

def main():
    db_host = raw_input('Host name: ')
    schema_name = raw_input('Schema name: ')
    user_name = raw_input('User name: ')
    password = getpass.getpass('DB Password: ')
    db_conn = MySQLdb.connect(host=db_host, user=user_name,
            passwd=password, db=schema_name)
    db_cursor = db_conn.cursor(MySQLdb.cursors.DictCursor)
    db_cursor.execute('show table status')
    rows = db_cursor.fetchall()
    total_size = 0
    for row in rows:
        data_length = row['Data_length']
        if data_length is None:
            data_length = 0
        index_length = row['Index_length']
        if index_length is None:
            index_length = 0
        total_size = total_size + data_length + index_length
    print '------------------------'
    print 'Total size: %s KB.' % str(total_size / 1024)
    db_conn.close()


if __name__ == '__main__':
    main()

I didn't add any exception handling code, I thought it would clutter the actual code I wanted to show.