пятница, 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)

1 комментарий:

  1. Hm. But if nYearFrom is unequal null and nYearTo is unequal null too then something like nMounthFrom.equals("00") would still lead to an NullPointerException what the first version prevents.

    ОтветитьУдалить