Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should variable names have adjectives before or after the noun? [closed]

I find that I am often a little inconsistent in my naming conventions for variables, and I'm just wondering what people consider to be the best approach. The specific convention I am talking about is when a variable needs to be described by a noun and an adjective, and whether the adjective should come before or after the noun. The question is general across all programming languages, although personally I use C++ and Python.

For example, consider writing a GUI, which has two buttons; one on the right, and one on the left. I now need to create two variables to store them. One option would be to have the adjective before the noun, and call them left_button, and right_button. The other option would be to have the adjective after the noun, and call them button_left, and button_right. With the first case, it makes more sense when reading out loud, because in English you would always place the adjective before the noun. However, with the second case, it helps to structure the data semantically, because button reveals the most information about the variable, and left or right is supplementary information.

So what do you think? Should the adjective come before or after the noun? Or is it entirely subjective?

like image 649
Karnivaurus Avatar asked Apr 08 '16 16:04

Karnivaurus


People also ask

What are the 4 rules for variable names?

Variable names are case-sensitive (age, Age and AGE are three different variables) There is no limit on the length of the variable name. A variable name cannot contain spaces. The variable name cannot be any Go keywords.

Do variable names have to be descriptive?

Descriptive Variable Names Only Go So Far Descriptive variable names really make reading code a lot easier. But the reality is that some variables are hard to describe accurately. In general, you want to be descriptive, but keep your variable names from about 8 to 20 characters.

What are the naming conventions for variables?

A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign " $ ", or the underscore character " _ ". The convention, however, is to always begin your variable names with a letter, not " $ " or " _ ".


1 Answers

I try to use noun_adj because it conforms to what I use for functions. When I write functions I tend to use verb_noun_adj, for instance:

def get_button_id():
    """Get id property of button object."""
    pass

This reads to me a bit more clearly that get_id_button because it is not entirely clear what you are getting here: is it getting the button.id or is getting a button called 'id' or maybe even something else? Unless you expand the name to be a bit more clear, like get_id_of_button which may be a bit too verbose for you.

There's probably an equally valid argument against what I'm doing here, but at least I'm being consistent in my madness?

like image 148
cmaceachern Avatar answered Oct 02 '22 14:10

cmaceachern