[thelist] Friday Freebies, part II

Chris Danek Chris.Danek at cmgisolutions.com
Thu Jul 20 17:22:24 CDT 2000


<tip author="Chris Danek [cdanek at bdsinc.com]" type="Java">
	Don't chain java functions. You are just asking for nullpointer exceptions:

bad:
	if (someObject.getName().getAge().add().giveRaise().isStupid())

good:
	Object o = someObject.getName();
		//you should check it for null here if it's going to happen
	Object o2 = o.getAge();
		//ditto
	Object o3 = o2.add();
		//ditto
	Object o4 = o3.giveRaise();
		//ditto
	if (o4.isStupid())

Trust me on this now. It's longer but it will catch -all- your nullpointer
exceptions. This is good practice in the long run, and will make other
people think you are good when they look through your code (and coversely,
curse at you when you don't).
</tip>

cd





More information about the thelist mailing list