Variable Names, Some tips

Variable Names, Some tips

Variables can be your best friend, or your worst enemy and that depends on You. If you are not careful and just creating variables without putting enough thought into it, your program soon would be a nightmare. In small programs, int x, y; is OK but unfortunately real life programs are not "small". So, you need to be careful while declaring variables.

In this article we would see how to meaningfully name our variables. I would focus "how to meaningfully name variables" rather than "rules or conventions for declaring variables". Rules or conventions might be vary in languages but meaningful names should be used regardless of language. Here are some points you should think before name you variable.

Avoid Action Words

Avoid words (words which shows action) should never be use as variable names. A variable is a data container, not an actor. Variables either contains data to perform an action or data which is result of an action. For example:

int add; // it makes no sense.
int sum; // ?thats a good name.
int additionResult; // a good elaburative name??

Avoid 'Temp'

Keep in mind there is nothing called "temporary variables", a variable named "temp" shows carelessly implemented logic. A variable, once declared, will always be there, so how is it temporary? Or on the other hand if some one want to say that the data in that variable is temporary then that is the main function of a variable, to hold temporary data, so what's new then?

Its simple you name it 'temp' because you don't know what name it should be given. Remember If there is a purpose, there is a name, much batter then 'temp'.

Determine Category

On highest level a variable represent a single data unit or collection of data unit. I am talking about single variables and arrays. Put an extra 's' in the end of a variable representing a collection, for example:

int number; // a single number
int[] numbers; // a list of numbers?

Note that The collection doesn't necessarily mean array, it could be a stack, a set. a queue or any other relevant data structure.

Scope Matters

A variable representing a field in a class should have much more meaningful name than a variable in a small little private utility function. A field in a class is most probably going to have getters and setters and would be referenced in future while a little variable in a small function is not that much important.

Similarly a variable representing a parameter of a function should be much more meaningful than a variable declared inside of that function. A parameter name is what seen by callers of that method and they might not be interested in what is inside. Keep that in mind while writing your methods.

Conclusion

Variables are very important and should be declared carefully. Variable names should not be action words and should have a meaningful name rather than temp. Keep in mind number of object it represents and scope should be taken care of while naming variables.

That was our brief discussion about variables names, You may also want to check out Function Names, do it right and Meaningful class names. If you have any query or suggestion please mention it in comment section, I would love to hear from you.

Thanks for reading, Happy coding !

Sander Jobing

Senior Software Engineer bij Kverneland Group Mechatronics BV

8 年

Having a good naming convention is essential for extensibility of software.

回复
Muhammad Sulman

PSE @ NXB | PHP | JS | React | Laravel

8 年

hahaha

回复

要查看或添加评论,请登录

Muhammad Asher Toqeer的更多文章

  • Modern Java as Data-Oriented Language

    Modern Java as Data-Oriented Language

    Java, at its core, is an Object Oriented Language. But, Object Oriented Programming alone can't solve all of the…

  • Spring Framework Brief Overview

    Spring Framework Brief Overview

    History Spring is a framework for Java Enterprise Application Development. It started in 2003, at that time Java offers…

  • Java Modules Introduction

    Java Modules Introduction

    Classes are the basic unit of a program in Java. Packages are used to manage classes and modulesare used to manage…

  • Handling Mistakes as a Developer

    Handling Mistakes as a Developer

    This article is based on "A Pragmatic Philosophy" from the book: "The Pragmatic Programmer". Mistakes are Inevitable.

  • Java 8 Functional Programming Simplified

    Java 8 Functional Programming Simplified

    This article will explain Java 8 functional programming related concepts, i.e Lambda expressions, Functional…

  • 3 Years being a Software Engineer, Here are some things I have learned

    3 Years being a Software Engineer, Here are some things I have learned

    Life is a journey, so as your carrier and like every other journey, your carrier teaches you a lot of things. Here are…

    2 条评论
  • MVC and MVCS : Software Engineering

    MVC and MVCS : Software Engineering

    In previous article Architectural vs Design Patterns: Software Engineering we discussed in detail about what…

    2 条评论
  • Architectural vs Design Patterns: Software Engineering

    Architectural vs Design Patterns: Software Engineering

    Software design patterns are some proven ways to solve a reoccurring problem faced by programmers. These are general…

    2 条评论
  • Meaningful Class Names

    Meaningful Class Names

    This article is about naming classes, mainly it is about "how to meaningfully name your classes" remember in…

    3 条评论
  • Function Names, do it right

    Function Names, do it right

    In programming, naming identifiers (class, method, variable names) is one of the most crucial part of writing code. If…

    7 条评论

社区洞察

其他会员也浏览了