« Using PC keyboard on MacBook Pro (aka swapping Cmd and Option keys) | Main | SharedCopy gets an upgrade »

August 24, 2007

True False parameters

Every so often, I get the chance to use functions that accepts a nondescript boolean as parameter... or *gasp* a sequence of nondescript booleans.

api.connect( true )

It's a chore for the maintainer (usually my future self) to have to look up what "true" meant. IDEs might be helpful at this point, bringing up code hints and what not.. unless of cos the function declaration itself isn't exactly perfect...

def connect(bool)
  ...
end

I've finally come round to do myself a favor (ok, I AM going to be the maintainer) by using self-explanatory truthy / falsey values instead

api.connect(  :auto_retry ) # true
api.connect( !:auto_retry ) # false

Reads better. No?

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e398212d6f883300e54ecfc6978833

Listed below are links to weblogs that reference True False parameters:

Comments

Much better!

-d

Personally, I prefer this more verbose invocation:

api.connect(:auto_retry => true)

@doug: if u wrote the method, sure! i was talking more abt 3rd party's

Image.capture(false, false, false, false, true)
photo.contrast(true)

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Blog powered by TypePad