среда, 21 июля 2010 г.

Consider the following code snippet:

public class SomeClass {

 private Validator validator;


Variable validator really isn't use. It could be simply removed, by author decided to mark it as @SuppressWarnings("unused") - just in order to make compiler and Eclipse silent. 


четверг, 22 апреля 2010 г.

Collection setter

There is two major ways to write setters for collection-type members.
This example combines both of them.
public void setPhones(List<PhoneNumber> phones) throws Exception
this.phones = phones;

четверг, 4 марта 2010 г.

Compare float numbers

This is how Chuck Norris compares float numbers:
if (!Float.valueOf("0.0").equals(price.getActualPrice()))
return true;

понедельник, 22 февраля 2010 г.

As to me, this is a really unobvious way to parse strings:

protected void parseSummaryLines()

// NOTE: First letters are ommited in order to
// support capitalized words as well
String RESULT_GOOD_TEXT_1 = "othing"; // Nothing
String RESULT_GOOD_TEXT_2 = "uccessful"; // Successful
String RESULT_BAD_TEXT_1 = "assword"; // Password
String RESULT_BAD_TEXT_2 = "failed"; // Failed


четверг, 7 января 2010 г.

Next generation programming style

A very good article about programming:

in particular, this one is great:

Bad books samples

Today I read a blog entry How Programming Books Promote Code Smells.

This is true: many books contain examples of bad code.

One of my favorite examples is the main method with try/catch like this one.

public static void main(String args[]) {
FileInputStream fstream = ...
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());

This try/catch is absolutely NOT NEEDED! If you skip them, the JVM does exactly the same work. the well-known "Throw early catch late" principle says that you shoudn't catch exception until you know what to do with it (except the trivial actions that the system does by default).

среда, 1 апреля 2009 г.

nStartFrom - 12

Quite a fun usage of variable nStartFrom:

public void setStatementFromAddress(CCallableStatement ccstmt,
CAddressStruct address, int nStartFrom) throws SQLException
nStartFrom = nStartFrom - 12;
ccstmt.setString(nStartFrom + 0, address.getStreet());
ccstmt.setLong(nStartFrom + 1, address.getCityID());
ccstmt.setString(nStartFrom + 2, address.getCity());
ccstmt.setLong(nStartFrom + 3, address.getCountyID());
ccstmt.setString(nStartFrom + 4, address.getCounty());
ccstmt.setString(nStartFrom + 5, address.getRegionID());
ccstmt.setString(nStartFrom + 6, address.getRegion());
ccstmt.setLong(nStartFrom + 7, address.getCountryID());
ccstmt.setString(nStartFrom + 8, address.getCountry());
ccstmt.setString(nStartFrom + 9, address.getZIP());
ccstmt.setString(nStartFrom + 10, address.getPhone());
ccstmt.setString(nStartFrom + 11, address.getFax());
ccstmt.setString(nStartFrom + 12, address.getEMail());