Saturday, 24 November 2012

Java Code Review Tool

One of the most painful but important steps of a java project, is the code review. checking and reviewing thousands of lines of some body else's code for small things like braces, name style, documentation and overall design can really knock you out and make you feel like there is a heavy rock right on the top of your heart.

fortunately, there are tools to reduce your pain and automate this precudrue as mush as possible. There are 2 tools that I am familiar with, each of them has their own features, and by using them together, really can make your life much easier.

first one I recommand is CheckStyle. But its checking rule is too strick, so you better set up your own one. It checks for the followings in your code:
  1. Java comments and docs
  2. naming conventions
  3. header
  4. imports
  5. size violations
  6. white space
  7. blocks
  8. modifiers
  9. and etc.
second one is PMD, it is meant to check the overall design of your codes. such as:
  1. empty catch block
  2. unused if statement and local variables
  3. duplicate codes
  4. classes that could be singletons
  5. inproperiate variable and method names
  6. and etc.
And also, there are still a lot of tools that you can find in interenet to help you fight, instead of wasting those extremely power tools, from now, pick them as your weapons and armors, and beat the code review.

Saturday, 17 November 2012

Checked VS Unchecked exception

Checked exceptions
- any subclass extend Exception, including  Exception itself, excluding RuntimeException
- must be catch or declare in a throws clause.
- represent invalid conditions outside the direct control of the program, Such as wrong format of user input, (which we were dealing a lot during the implementation of A2 in CSC207), database problems, etc 

Unchecked exceptions
- extend RuntimeException only, but notice, RuntimeException itself is a subclass of Exception
- the programmer may not know that this kind of exceptions could be thrown, thus, they are not forced to be declared.

- represent bugs in the program, which cannot be reasonably recovered from at run time. Such as NullPointerException, IndexOutOfBoundsException etc.


Saturday, 10 November 2012

When to declare "throws Exception"


 
In Java, when defining a method, we also need to declare in the method head what exception may be throw. That's what I was believing, but, in my instructor's slides, some methods she did this way, some methods she just throw exception right the way. such as following:
public void methodA() throws AnException {   
 //do something    throw new AnException();
}
public void methodA() {  
 //do the same thing    throw new AnException();
}

And I am kind of confusing in which situation we need to declare, 
is there a specific requirement or restriction? or we can do it either way depends
on our mood?
after some google, I find that there are two different major type of exceptions, 
one is checked, which does not extend RuntimeException, and those exceptions must
be declared. In the other hand, unchecked exception is the exception that extends
RuntimeException, and then either way is allowed(declare/not declare). But now I have other doubts like, in which circumstances our exception should extends RuntimeException?
what is the difference between those two? and what is the advantage on each?

Sunday, 4 November 2012

leave it behind....review first

Since we are having a Midterm tomorrow, I decided to leave that implementation after I finished our second midterm....XD

Table implementation

I created a Table class based on 2d ArrayList. The constructor of this class is just simplely take in a 2d String ArrayList. Now the requirements of the assignmnet changed a little bit, so that this Table class would be able to handle both String and Int type of ArrayList. I could build up two different constructors, so that one take in String ArrayList, and another one take in Int ArrayList. But the problem is, the instance of this class was initialized as a String ArrayList, thus a Int ArrayList cannot be assigned to it. I tried to initial another instance with Int ArrayList, but NetBean warned me that there is a name clash between those two constructors, they have the same erasure. Anybody knows what does "erasure" means in Java? And I think my method of implementing this task is wrong, is there another way? Take a rest, come back later...