пятница, 20 марта 2009 г.

Ugly condition

I have found a method for validating date:

// check that it is valid format
// d,m,y
// m,y
// y
if ((nYearFrom != null && nMonthFrom != null && nDayFrom != null ||
nYearFrom != null && nMonthFrom != null ||
nYearFrom != null) &&
(nYearTo != null && nMonthTo != null && nDayTo != null ||
nYearTo != null && nMonthTo != null ||
nYearTo != null))

After a short thinking, this condition can be simplified to:
if (nYearFrom != null && nYearTo != null)

File.separatorChar

I have just found a funny snippet in Java code:

File pom = new File(dir.getAbsoluteFile()
+ String.valueOf(File.separatorChar) + "pom.xml");


Actualy class java.io.File has two constants:
public static final char separatorChar = '\';
public static final String separator = "" + separatorChar;

This is the same symbol, but separatorChar is a char, and separator is a String.

So, instead if using String.valueOf(File.separatorChar) developer could use File.separator.