TDD, Ruby, & handling nil arguments -


part of practice of tdd involves easing core functionality edges, , starting testing simplest, degenerate case. i'm working in ruby, find myself writing test this:

it "does thing if given nil"   expect { my_method(nil) }.to do_the_thing end 

and production code this:

def my_method(arg)   if arg.nil?     do_the_thing     return   end    do_the_real_thing end 

i don't idea of littering every public method such guard-clauses, given dynamic nature of ruby, draw line? when reasonable put responsibility passing valid arguments on calling code?

i'd write focused, expressive methods aren't littered overly-defensive code, want build robust , correct system.

the practice of easing in edge-cases appeals me, i'm unsure how particular edge-case should handled.

i believe decision on how defensive write code depends on how reasonable circumstances of error condition are.

the question ask how reasonable assume condition occur in normal course of events.

if in normal course of events reasonable error may occur makes sense code accordingly.

if in normal course of events error condition should not happen, not put in check condition. if error condition occurs better have application throws fatal error , stop working there fundamentally wrong.

as example, have method accepting parameter country name selected pre-defined list in form. reasonable method assume valid country name. there no reason method sanity checking on (such checking string not valid country name).

in example, have method dealing parameters database source known of poor data quality. in situation reasonable check ensure parameters method expects.

writing code overly defensive may result in hiding of issues. have seen code written checks unreasonable things, logs issue , carries on running. these applications typically have unpredictable behaviour concealing coding problems should dealt with.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -