How to give a brief but meaningful name to my variables

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
theflysong
Member
Member
Posts: 27
Joined: Wed Jun 29, 2022 2:17 am
Libera.chat IRC: theflysong

How to give a brief but meaningful name to my variables

Post 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'
I'm a new man to develop operating system.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to give a brief but meaningful name to my variables

Post 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.
nullplan
Member
Member
Posts: 1789
Joined: Wed Aug 30, 2017 8:24 am

Re: How to give a brief but meaningful name to my variables

Post 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.
Carpe diem!
Post Reply