/images/function-names-right-oh.jpg

TL;DR: Advice for good function names and some examples of bad ones, a rant and why this creates readable and maintainable code. Or is this just nitpicking?

Name your functions good

First up a rant, I can't believe software engineers are still getting this so so wrong. Who am I talking about? Mostly the developers I'm working with really!! 😇 The most surprising thing? I've seen developers with 20 years experience ( claimed experience ), write functions that don't even come close to describing what they do.

Is naming hard?

That depends, sure if you've been a software engineer for 25 years you probably have it all worked out. Maybe the guide below will help!

It should do exactly what is on the tin!

A good function name tells us what the function does in a clear concise way, it gives us the action that is being performed, the name of the variable passed in and the type of output we will get. But we have so many coding cowboys that just can't get the basics right. I've seen examples where the function name and the variable name have no meaning at all and the return type is missing .. yuk

>> I think my biggest gripe is that we are paying good money for low quality code.

"Your function name should describe exactly what that function does, no more no less"

"It should do exactly what is says on the tin"

A function doing too much

This is one of the most common problems, and it is the main cause of badly named functions. It's difficult to name a function when it's doing 2-3-4 things.

## Start your function name with a verb

Oh and pick the right type of verb, try one of the suggestions below.

Type : Get/Obtain

get, fetch, retrieve, read, find, search, close, map

Type : Alter

set, change, edit, update, add, append, remove, delete, save, store, disable, hide, split, separate, merge, join

Type : Create

create, make, calculate

Type : Predicate/TrueOrFalse

is, has, can, should

Now for some examples of what you should never do

  1. Never start you function with "do"
  2. Never start you function with "lets"

Examples of the good the bad and the ugly

Predicate functions

Lazy developers don't bother with these, and it really is just plain laziness. Most of the time, these function are there to describe business rules. Here is a few examples that I used on an object call "Relationship".

isCarersCredit, isFuture, isPast, hasPayee, canAddPayeed

I use these to create the business rules for relationships. Without these predicate functions I end up with unreadable "if" statements that are hard to make sense of.

Comment your functions

Make no mistake this can be really difficult, if you find yourself writing what your function does, then guess what, your function name is wrong ( it's name should tell the reader what it does )!! Or that function is doing too much.

So what are good comments? You might what to comment why the function exists, not what it does. There is a difference between the two. Most of the time if your function is 5 lines of code, then you probably don't need a comment.

Conclusion: Investing in Quality

In the world of software development, code isn't just for computers; it's for humans too. Choosing meaningful function names is an investment in creating code that's not only functional but also comprehensible and maintainable. The next time you're tempted to name a function "doThis" or "handleThat," remember the impact of well-named functions on your codebase's quality. By following a few simple principles, you can level up your coding skills and contribute to a more readable and enjoyable development experience for everyone involved.

Remember, a well-named function is like a well-crafted headline—it draws readers (developers) in, provides a clear understanding, and leaves a lasting impression. So, go forth and name your functions well!