Long Method Name is Good!

Zeeshan Ali
2 min readJan 31, 2022
source=istockphoto

Hello Guys, In this post, we will discuss the Method and Variable Naming convention. I am pretty sure every programmer knows about it, and I am not going to repeat it but want to highlight a few points that I have encountered during code reviews of my juniors.

First and foremost, don't be shy about writing your variables and method names. Method and Variable names must be meaningful, no worries if it takes a few extra characters.

Most often, developers choose short names of variables or methods that don’t explain much of what it actually does and they write some comments on it to make it meaningful.

Let’s take an example.

// Customer code should not be Null or Empty,
// Customer code should not be less than 10 Chars
// Customer code should start with 25

public bool InvalidCustomerCode(customerCode){
return ( string.IsNullOrWhiteSpace(customerCode)
|| customerCode.Length< 10
|| !customerCode.StartsWith(25) );
}

If we remove the comments from the above function, the above function name will become meaningless and confusing, and to know it, we need to dive into it.

So what we can do? we can rename it, instead of putting comments on it.

public bool InvalidCustomerCodeIsNullOrLengthLessThan10OrBeginWith20(customerCode){
return ( string.IsNullOrWhiteSpace(customerCode)
|| customerCode.Length< 10
|| customerCode.StartsWith(20) );
}

I believe the big method name is good, it makes your code easy to understand for other developers and even for you if you visit your code after a long time.

But keep in mind, don’t make the method name extra-large, it should look like a phrase, not like a paragraph. :)

we can limit our method name by 50–60 chars long.

One more important thing is to remember, the above-given code snippet is just an example, it doesn’t mean that you should always write code logic as text in the function name, it will definitely create a problem when you can’t change the function name but you need to change the logic.

The core idea of this post is to tell the audience that always choose a good name for your function, that explains much of its functionality, no matter if it takes a few extra characters.

Thank you for your time, your feedback will be appreciated.

--

--

Zeeshan Ali

I am Zeeshan Ali, and i am Software Engineer since 2006.