Friday 15 March 2013

java - Redundant argument validation in defensive programming style -



java - Redundant argument validation in defensive programming style -

i have mill class, makes order objects, in turn create item objects. these 3 classes bundled in single bundle no other classes. each item has "itemmode" attribute. created excessive null checks accomplish defensive programming style. while confident coding style, though can bit verbose, i'm doubting approach because of next reasons:

if amount of created objects , amount of arguments need validated increases, that's whole lot of code must run. could performance issues? if so, talking nano-, milli- or plain seconds? considering mill able instantiate order , item objects, theoretically skip redundant if==null checks in other classes. know no mill can created mode null, no orders or items created mode null. however, know because wrote code. not priori clear other developers on project.

would maintain redundant checks or not?

i first made item class:

class item { //constructor protected item(itemmode mode) { if(mode == null) { throw new illegalargumentexception("mode should not null."); } } ... }

then order class:

class order { //constructor protected order(itemmode mode, ...) { if(mode == null) { throw new illegalargumentexception("mode should not null."); } } ... public void methodthatmakessomeitems() { ... } }

and lastly mill class:

class mill { //constructor public factory(itemmode mode, ...) { if(mode == null) { throw new illegalargumentexception("mode should not null."); } } ... public void methodthatmakessomeorders() { ... } }

java validation null factory

No comments:

Post a Comment