Monthly Archives: July 2012

A lot of things can be found in the snippet at “What is wrong with this code #02″. Here it is again: [sourcecode language=”java”] public MyObject { private String myField; … (imagine multiple fields) … public GenericObject toGenericObject() { GenericObjectFactory genericObjectFactory = GenericObjectFactory.getInstance(); GenericObject genericObject = genericObjectFactory.create("myObject"); genericObject.setField("myField", myField); // imagine multiple lines using genericObject.setField("field", field) return genericObject; } } [/sourcecode] Already many things are pointed out in the comments of the initial post. One thing that immediately struck me is this line: [sourcecode language=”java”] GenericObjectFactory genericObjectFactory = GenericObjectFactory.getInstance(); [/sourcecode] Don’t do this. GenericObjectFactory implies… Read Article →

Given that the functionality of the method toGenericObject must be preserved; there is something obviously wrong in this code, can you find it? If so, can you think of an easy solution? [sourcecode language=”java”] public MyObject { private String myField; … (imagine multiple fields) … public GenericObject toGenericObject() { GenericObjectFactory genericObjectFactory = GenericObjectFactory.getInstance(); GenericObject genericObject = genericObjectFactory.create("myObject"); genericObject.setField("myField", myField); // imagine multiple lines using genericObject.setField("field", field) return genericObject; } } [/sourcecode]

I asked what was ‘wrong’ in the following code. I had put ‘wrong’ in quotes, because it is an ambigious term. Here is the code: [sourcecode language=”java”] if (null == sessionProxy.getShoppingCart()) { throw new IllegalStateException("ShoppingCart may not be null"); } [/sourcecode] One thing that we find here, is the use of a session proxy object. This is convenient, because it can do things for us at a centralized place. So what is wrong? In my opinion there are several things wrong with this code: – violation of the Single Responsibility Principle – dealing with null… Read Article →

Scroll To Top