How to Write a Great Function, Part 1 - Naming

| No TrackBacks
There are a lot of ways to tell that a function is going to be less than great.  Perhaps the most obvious sign is the name of that function.  It's actually a lot like real life: if your name were Bedwetter McStupidpants, you're probably destined for a life of crime, insanity, and gum disease.

Here's an example: I spent several months working in a system where the critical function in the system was called... DoStuff2.  Unfortunately, I am being serious; it was a large, critical program for the energy industry and pretty much every use case you could ever draw up ran right through this bleak disaster of a function called DoStuff2.

We'll go through the specifics of just how horrible DoStuff2 was/is, but right now, I'm only looking at the name.  I don't even know where to start on the questions here.  What does DoStuff2 actually do?  And what is this stuff that it's doing this mysterious action to?  And, for the love of God, what happened to DoStuff1?

This is completely obvious, but a function name is your first and best chance to communicate the intent of your function.  If it's not clear from the name what your function does, no one will use it, the function rots, and it's quickly headed down the road of Bedwetter McStupidpants.

The whole idea of a function is to encapsulate complexity, right?  Start with that in your function name.  I don't care if, in the act of performing this function, it uses a worker thread or calls a web service or launches a nuclear strike against the people of the Arctic.  In the function name, I only want to know what it will do for me.

Let's say your function calculates a subtotal.  What should we name that?  Gee, I don't know, perhaps CalculateSubtotal?

Look at the naming convention there: it's verb then noun, and both words are as specific as possible.  It's like a command your dog would understand, if it had a slightly better grasp on vocabulary.  "Rover, eat food!  Rover, go potty!  Rover, calculate subtotal!" If you ever shouted, "Rover, do stuff!" he'd probably pee on your shoes out of spite.

gorillasuit.jpg
The nice thing about a specific verb-noun naming convention for your functions is that the naming never gets out of control.  You never get monstrosities like PerformCalculation (every calculation in the universe?), SubtotalGenerationInit (just init?  does it ever finish?), PleaseGiveMeAubtotalOrIWillHuntYouDown-AndMakeYouWearAGorillaSuitToWork (definite style points for that one, but slightly lengthy).

Let's conclude.  Your function name should be short, meaningful, and specific; if it's not that, come up with a better name or learn to live with a horrible function.

No TrackBacks

TrackBack URL: http://www.codypowell.com/mt/mt-tb.cgi/6

About the Author

The Art of Delightful Software is written by Cody Powell. I'm a dad, husband, software developer, and entrepreneur. I'm a cofounder at Famigo, where we make mobile work for families.

Twitter: @codypo
LinkedIn: codypo's profile
Personal blog: Goulash
Email: firstname + firstname lastname dot com

Categories