Should variable names have adjectives before or after a noun?

I find that I often disagree slightly in my naming conventions for variables, and I'm just curious about what people consider to be the best. The specific convention I'm talking about is when a variable should be described by a noun and an adjective, and whether the adjective should precede or after a noun. The question is common in all programming languages, although I personally use C ++ and Python.

For example, consider writing a graphical interface that has two buttons; one on the right, one on the left. Now I have to create two variables to store them. One option would be to have an adjective in front of a noun and call them left_button and right_button . Another option would be to have an adjective after the noun and call them button_left and button_right . In the first case, this makes sense when reading aloud, because in English you always put the adjective before the noun. However, with the second case, it helps to structure the data semantically, because button shows the most information about this variable, and left or right - additional information.

And what do you think? Should an adjective come before or after a noun? Or is it completely subjective?

+6
source share
2 answers

I am trying to use noun_adj because it matches what I use for functions. When I write functions, I use verb_noun_adj , for example:

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

This tells me more clearly that get_id_button , because it’s not entirely clear what you are getting here: does it get button.id or does it get a button named "id" or maybe something else? If you do not expand the name to be more clear, for example get_id_of_button , which may be a little detailed for you.

There is probably an equally valid argument against what I'm doing here, but at least I agree in my insanity?

+4
source

I think writing an adjective before a noun is more common. But if you think about it for a second, write it after the noun becomes easier when reading the code. An adjective can easily be considered an attribute of a noun. By the logical way of thinking, this is the best way, in my opinion.

+2
source

All Articles