Choose I use a suffix or prefix when naming variables?

394 Views Asked by At

Suppose I should have a foreground and a background image, each with their own variable for width, height, and bits per pixel. Should I define my variables as fg_width, fg_height, fg_bits_per_pixel, bg_width, etc. or should I define them as width_fg, height_fg, bits_per_pixel_fg, width_bg, etc.

(Let me know if this post belongs in another stackexchange forum, I'm not sure if it belongs here)

1

There are 1 best solutions below

0
August Karlstrom On

Prefixing a noun with attributes is more natural since that is how you would write it in English (although with a space inbetween), e.g. foreground width rather than width foreground. So, when choosing a variable name, first consider what it describes in the most basic sense. In your example it's a width, a height and a ratio. Then add prefix attributes to make the names sufficiently accurate. For a boolean variable, choose an adjective instead and prefix it with a noun phrase, e.g. fg_width_defined.