Page 1 of 1
How to give a brief but meaningful name to my variables
Posted: Thu Jul 06, 2023 6:36 am
by theflysong
I found my variables' name are too long and I need spend a large time in typing their name.But I don't know what should I do to shorten their name and keep their meaning?
Is there any ideas?
For example:
'gate_descriptor_t'
'divide_by_zero_error'
'write_serial_str'
Re: How to give a brief but meaningful name to my variables
Posted: Thu Jul 06, 2023 8:46 am
by iansjack
Keep them reasonably long and meaningful. It will pay huge dividends when you come to look at your code in a few months time. It doesn't increase the size of the executable and it's not that much extra typing. It also helps you to think about what you are actually doing when you use them.
Of course this doesn't apply to simple utility variables such as loop counters; there's no problem in using short, even single-letter variables there.
Re: How to give a brief but meaningful name to my variables
Posted: Thu Jul 06, 2023 8:58 am
by nullplan
Use an editor/IDE that allows for autocompletion. In VIM, for example, you can simply press CTRL-P to complete the word. Many IDEs have CTRL-Space or similar.
Those names you have cited aren't long. They are exactly the right length, and convey your meaning even to people that haven't read the code. That is a good thing. Too often I hear the advice that names should be long without the qualification that they should have the right amount of meaning. Being overspecific and writing novels worth of text each time is counterproductive. Being underspecific and having cryptic variable names people cannot understand is not helpful.