How’s Naming Techniques can Help You in Writing Readable Code?

Talha Tariq
Nerd For Tech
Published in
4 min readFeb 2, 2021

How’s taking care of writing names of functions, classes, and variables can help you in writing an efficient, clean, and readable code?

Whether you’re new in programming or a professional in it, you have to know about documenting and writing clean code.

Writing code is a different thing, but writing clean and readable code is also another one.

Now, what is readable code?

Suppose you’re reading a book that is without headings on paragraphs and instead of well-formatting, there is no separation in paragraphs, and those are also in a confusing order. What would you feel?? It can be quite frustrating while reading it. The same situation can occur in your code too. You have to write highly readable code like a good article.

Everybody wants to write a clean code, but meanwhile inner us 👇😃 :

Now let’s headed over to terms for writing clean code.

→ Naming (Variables, functions, and classes etc)

→ Indentation

→ Commenting

But in this article, we’re going for details of the naming of variables, classes, and functions only because it is a vast topic itself.

First, let’s see an example, then we’ll be headed over to cases in Naming.

Task: Write a program that asks the user to type the width and the height of a rectangle and then outputs to the screen the area and the perimeter of that rectangle.

Which is looking more clear for reading purposes??

And also, this is a simple program of just 9 lines. What if your code is of thousands of lines with multiple functions, classes, loops and all of these have some naming conventions of a, b, c, or something else? doesn't seem horrible. These are the benefits of thinking about naming.

Now the following points should be kept in mind while naming in the program.

Be clear:

is a variable name referring to one or more pizzas?

Be specific:

Is an employee better than a person?

Be meaningful:

Will c make sense to someone else reading your code? Or is the customer better?

Hope that you got up everything till now, that how’s writing better name can help us as well as others while reading.

Don’t be afraid:

Never be afraid of using longer names to improve readability. Good text editors and IDEs have completion so longer names won’t slow you down, and also modern programming languages and databases support names of 50 characters or more.

Now let’s see cases of naming such things, The most common case used by developer every day are the following:

1) Camel case

2) Snake case

3) Pascal case

Before going into an explanation, let’s have some fun for refreshment😃👇

Okay! Now let’s move on to our last thing (Explanation of cases):

→ Camel Case

Camel case combines words by capitalizing all words following the first word and removing the space, as follows:

Raw: user login count

Camel Case: userLoginCount

Uses: This case is used while naming local variables and objects in particular scopes, and also user-defined functions.

→ Snake Case

This is a trendy way used as a convention in the variable declaration in many languages. Pascal case combines words by capitalizing all words (even the first word) and removing the space, as follows:

Raw: user login count

Snake Case: user_login_count

Snake Case (All Caps): USER_LOGIN_COUNT

This style, when capitalized, is often used as a convention in declaring constants and global variables in many languages. When lower cased, it is used conventionally in declaring field names(File Handling).

→ Pascal Case

This is a trendy way used as a convention in the variable declaration in many languages. Pascal case combines words by capitalizing all words (even the first word) and removing the space, as follows:

Raw: user login count

Pascal Case: UserLoginCount

Uses: This case is used when we have to declare and use classes.

That’s all about Naming in Programming languages. Hope that you get this all stuff. Try your best doing such practices while you’re writing a code.

Thanks for reading!

--

--

Talha Tariq
Nerd For Tech

On the way to be a Software Engineer | Love to write about Islamic & Programming Topics | Founder of Learn & Grow