The Daily WTF
Today’s an interesting case of code that seems perfectly reasonable at first glance. Sent to us from FinalGuy, this Java code caused no end of problems for him due to its rather unexpected behavior:
public void setCurrentPage(int currentPage) { if ( currentPage < 1 ) { currentPage = 1; } if ( currentPage > maxPages ) { currentPage = maxPages; } this.currentPage = currentPage; }
This code exists in the context of a Java Bean, aka a class that’s just meant to hold data, and gets some bonus features for accessing that data. And this setter clearly is meant to do some validation, and this is validation that seems pretty reasonable: you can’t be on a page of a result set that comes before the first or comes after the last. It’s very much an Object Oriented Programming 101 example of how setters are meant to be used to
To read the full article click on the 'post' link at the top.