Dashboard > People > codesmell > 2007 > November > 04 > Object construction
  codesmell Log In   View a printable version of the current page.  
  Object construction
Added by Uwe Schaefer, last edited by Uwe Schaefer on Nov 14, 2007  (view change)
Labels: 

Did you read "Effective Java"? If you didn´t, you should: it is one of the greatest book about java i have read so far. The only problem with this book is that - over time - you tend to forget some of the invaluable advices, you get from it.
At least, this happened to me: I wrote some Abstract a few weeks ago, that used some template Methods to delegate to its implementations, little less obvious, but essentially just like this:

public abstract class AbstractFoo{
	protected AbstractFoo()
	{
	   initialize();
	} 
	// can be used to initially read state.
	protected void initialize(){}

Already know what kind of trouble i was going to face? Lucky you

Few weeks later, i added an implementation to the codebase, that used some File Object:

public class FileFoo extends AbstractFoo{
        private File file;
	protected FileFoo(File file)
	{
	   super();
	   this.file=file;
	}

I remembered, that for the reason of initialization, i already added a method i could use, to read from that file. So i made use of that:

protected initialize()
	{
		readFromFile(file);
		[...]
	}

I guess, you see the trouble here, don´t you? The member file is not yet initialized, when calling the method.

"Do not use non-final methods during object construction"

Even worse: i added a template method, that encourages to be overwritten. Stupid me.
That´s what i call a bug pattern

Thanks to Cemal for pointing to this discussion on the Wicket-User Mainiling-List:
http://www.nabble.com/forum/ViewPost.jtp?post=13569154&framed=y

Posted by Anonymous at Nov 14, 2007 09:02 | Permalink | Reply To This
November 2007
Sun Mon Tue Wed Thu Fri Sat
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  

HisKit, MyKit, YourKit is for anyone
Working on weekends...

This site is powered by a free Atlassian Confluence Personal Server License. Evaluate Confluence for your organisation or read more about Confluence licensing here.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators