Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to name a predicate function?

Tags:

coding-style

I'm looking at a function called:

WhetherAddFloor(leg) -> bool

and when I see code like:

if(WhetherAddFloor(l)) ...

it smells odd and am wonder if something like:

CheckAddFloorNeeded(leg) -> bool

wouldn't be better? Thoughts?

like image 422
Tom Kirby-Green Avatar asked Dec 17 '11 22:12

Tom Kirby-Green


2 Answers

In languages where question marks are allowed in function names the style is to append one to the end if the function returns boolean:

FloorNeeded?(leg)

But where that isn't syntactically possible the convention is to use the word 'is' at the beginning:

isFloorNeeded(leg)

It's generally easy to read and understand and it seems like it'll fit your situation.

like image 118
Jack Danger Avatar answered Oct 14 '22 19:10

Jack Danger


WhetherAddFloor() sounds as quite an unfortunate choice to me.

CheckAddFloorNeeded() is better, if the function has a substantial amount of work to do, and you would like to advertise that fact to those who use the interface.

IsFloorNeeded() is also good, if the function does not have a substantial amount of work to do, (say, it is just an accessor to a previously computed member,) or if you want to keep this little bit of information secret from those using the interface.

like image 28
Mike Nakis Avatar answered Oct 14 '22 17:10

Mike Nakis