How to name variables that are structures

I often work on private projects with WinApi, and as you might know, it has thousands of names and typedefed structures like MEMORY_BASIC_INFORMATION.

I'll stick with this in my question, which is even preferable or better if you want to name a variable of this type. Is there some kind of leadership style for this case?

For example, if I need this variable for the VirtualQueryEx function.

Some ideas:

 MEMORY_BASIC_INFORMATION memoryBasicInformation;   
 MEMORY_BASIC_INFORMATION memory_basic_information;

      

Just use the structure name without capital letter and with or without underscore.

MEMORY_BASIC_INFORMATION basicInformation;
MEMORY_BASIC_INFORMATION information;

      

Short form?

 MEMORY_BASIC_INFORMATION mbi;

      

I often see this style using the struct name abbreviation.

 MEMORY_BASIC_INFORMATION buffer;

      

VirtualQueryEx defines a third parameter lpBuffer (where you pass a pointer to the structure), so using that name might be an idea as well.

Greetings

+2


a source to share


3 answers


In general, it is not advisable to specify variables based on their type. Instead, try to provide additional information about a specific context and use case.

Using the MEMORY_BASIC_INFORMATION example, consider what the structure context is. Do you use information to iterate over several such information structures? Then maybe

MEMORY_BASIC_INFORMATION currentFrame;

      

Or, if you are doing a memory information test for some status, it might be a candidate.



MEMORY_BASIC_INFORMATION candidate;

      

You can now write documentation such as "candidate-candidate structure for ...".

You may find that you still want to include type information using the type prefix location. If so, you can name it mbiCurrentFrame

or mbiCandidate

.

If the purpose or context is truly abstract, such as in the case of the API functions themselves, I would choose something simple and straightforward, such as info

or buffer

unless those names could somehow be misinterpreted in context.

+2


a source


I think it hinges on a number of issues, and you just need to find the best balance while striving for readability.

  • Window width
  • Variables / types of similar names used in the subroutine.

Speaking of which, if I can handle it, I would probably use ...

MEMORY_BASIC_INFORMATION  info;

      

If there were other similar types or variable names, I would add some kind of descriptive modifier like ...



MEMORY_BASIC_INFORMATION  memBasicInfo;

      

Or, if window real estate was limited (some projects I worked on insisted on a max size of 80 char), I could go ...

MEMORY_BASIC_INFORMATION  mbi;

      

But to make it as readable as possible, I try to be consistent - and I think that's one of the most important things to keep in mind.

A bit all over the place, but I hope this helps.

0


a source


Try to keep the case and typedef abbreviation for example.

MEMORY_BASIC_INFORMATION MBI_temp

      

I am dealing with a lot of code that is and should remain portable on Linux and Windows, this was also a problem for us.

You can also do this in the camel case:

MEMORY_BASIC_INFORMATION MBITemp

      

.. but it doesn't seem self-evident.

The point is that anyone familiar with these structures should recognize them for what they are pretty quickly. Just make sure not to trump in a different namespace.

The key is just to be consistent in every tree you work on. This is really only a noticeable problem in two cases:

  • Globals
  • Long Mile Functions

If you have functions for so long that you have to scroll through five pages of declarations to see what a variable is, there are more serious problems to handle than variable nomenclature :)

Annoyingly, this can lead to some kind of weirdness due to syntax highlighting, picking it up as a constant, but also for the base typedef.

0


a source







All Articles