Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Naming Conventions For Maps/Lists in Dynamically-Typed languages

I am getting into Groovy language, which has dynamic typing (as well as optional static typing). It also has native support for Lists, Maps, and Ranges, so I find myself using lists and maps a lot, especially lists of lists, lists of maps, maps of lists, etc.

In static languages (esp with Generics) you always have an idea of what your type is. I am fairly new to dynamic languages, and it's getting a bit difficult to keep track of what my variable is supposed to be, so I was wondering if other people use some kind of variable naming conventions to keep these straight.

For example, suppose I have a map of dates as key and integers as values. Or List of integers, or List of Maps that contain strings as keys and account objects as values.

It seems like creating a clear convention behind variable names will help me keep track of what data type structure I am dealing with without having to look it up.

Any tips?

like image 438
Jean Barmash Avatar asked Mar 07 '09 23:03

Jean Barmash


People also ask

What are the variable naming conventions in Javascript?

Naming ConventionsVariable and function names written as camelCase. Global variables written in UPPERCASE (We don't, but it's quite common) Constants (like PI) written in UPPERCASE.

What are naming conventions examples?

What is an example of a good naming convention? Good naming examples include: [Project number] - Data Use Agreement - [Title of research project] Approval - Change to employee travel policy - February 2014.


4 Answers

The name of your variable should explain to someone reading the code what it is supposed to be, what it stands for. If you have a map of dates to integers, does it represent, for example (suggested variable names are in brackets):

  1. a number of payments due on that date (paymentsDue)
  2. a number of days between mapped date and some other point in time (daysPassed)
  3. a number of messages posted on that date on Stack Overflow (numberOfPostedMessages)

In languages where variable type is not readily available, you might want to append a prefix of suffix, such as paymentsDueMap. I would, however, advise against encoding any additional type information inside a variable name, such as datesToInts - that routinely does more harm than good.

Finally, if you have a complex data structure, such as a list of maps between strings and accounts, the best thing would be to encapsulate that into a separate class, and name it according to its intent.

like image 139
javashlook Avatar answered Oct 29 '22 01:10

javashlook


In static languages (esp with Generics) you always have an idea of what your type is.

After a while of programming in dynamic languages, you learn that using types this way is a crutch. Two pieces of advice:

  1. Use good variable naming. For instance, if you have a map of dates to ints, you can name it something like BirthdateToTotalLookup.
  2. Learn what visual clues to look for. It may seem obvious, but it took me a while to get in the habit of looking for clues like this:

    sum += x['10-16-92']
    

From the piece of code above, I can tell that x is a map that has a date as a key and returns a number of some kind.

like image 20
Jason Baker Avatar answered Oct 29 '22 00:10

Jason Baker


If the names can be kept short, then I tend to name maps something like "nounToNoun". So using your example of dates mapping to integers, I would name that "dateToCount" (if the integers are counters for something). That way its obvious that it is a map, and its obvious what is being mapped to what. The problem is that sometimes it is difficult to keep these sort of names short and readable. For example, "userToLoginHistory" starts getting a little unwieldy.

For lists I generally use a plural for the variable name. So "user" would be a single user, and "users" would be a list of users.

To be honest, I am not sure what a good name would be for a list of maps.

like image 34
KarstenF Avatar answered Oct 29 '22 01:10

KarstenF


This is a common beginner's lament. You could use a naming convention, but odds are you'll drop it before too long and focus on what the variable represents (its meaning in relation to the rest of the code) rather than worrying about how it's represented (it's "type").

like image 42
MarkusQ Avatar answered Oct 28 '22 23:10

MarkusQ