Is the definition of setter and getter in .h the reason for more or less readability of the codes?

I have some direct habits (I think) to write getter and setter in C ++:

  • I usually like to define getters and setters in .h instead of .cpp, just because I think that .cpp usually has more codes than .h, I want to balance the number of codes in .h and .cpp.Also I think that getter and setter have less frequent changes than other functions.

  • I usually mark getters and setters "inline", but not for performance: just use "inline" as the getter and setter marker, when I see "inline" I can know it is a getter or setter method quickly. Therefore, I will prevent other methods from "inline", because "inline" is simply used as a marker to separate the getter and setter from other methods.

But after a while, I found that no person would use getter and setter in .h. Also seems unusual for inline getters and setters.

Is the habit really improving readability or just results in less understandable and less sane code?

+3


source to share


4 answers


  • you can define it in .h files, and that doesn't mean code readability if they are short. int getX () const {return _x; } if these functions are complex, perhaps you should avoid them.
  • setter is generally more complex, so you have to go to .cpp files.
  • about inline:
    • don't care about it until the optimization stage of development.
    • functions in .h are not automatically inlined, even the inline word is just a compiler recommendation. (different compilers have the word __force_inline)
  • you should read some code styles like Google C ++ code style or some others and take some rules there.


+1


source


I usually like to define getters and setters in .h instead of .cpp, just because I think that .cpp usually has more codes than .h, I want to balance the number of codes in .h and .cpp.

Is there a specific reason? You have to write code where it should be out of alignment with the (dubious) aesthetic sense of balance. In addition, a very short commonly used header file and a long compilation block can reduce overall compilation time.

Also I think getter and setter have less frequent changes than other functions.

It makes sense to reduce compilation time if the header is included in many other compilation units but does not abuse it, compilation time is important, but code readability is greater. Worry about this only if you are using a very old computer or working on a huge project.

I usually mark getters and setters "inline", but not for performance: just use "inline" as the getter and setter marker, when I see "inline" I can know it is a getter or setter method quickly, So I will prevent " inline "other methods because" inline "is used as a marker to separate the getter and setter from other methods.

Absolutely bad ! inline

- give a hint to the compiler to inline this method, not to use it for anything else! Could you use (valid) volatile

to highlight variables used only in functions const

? Use language constructs for your own purposes. Is always.

But after a while I found that nobody would add getter and setter to .h.



Any evidence from this?

Also seems unusual for built-in getters and setters.

Perhaps not explicitly (because there are currently inline

more hints to other programmers than to the compiler), but they are good candidates to be inlined because they are usually simple and the overhead is higher than the code they execute. Don't worry about where you put it, if the compiled data finds that it will improve the performance of your code (speed or memory size), it will make it inline without your help (but you can force it to do it or not using some extensions for a specific supplier).

Is the habit really improving readability or just results in less understandable and less sane code?

In my opinion (that I marked this answer as a wiki community), this is a bad habit because no one but you will understand your reasoning. This is amazing: if I see a method explicitly labeled as inline

, I expect the performance-critical method to be (and inline

make it obvious), and the startling code is error-prone code.

I am especially against inline used to denote getter / setter methods, choose a good coding convention and make it mine. It will evolve with your own (or your company) coding style and experience, but not reinvent the wheel before you master the existing material first ...

+1


source


The marking functions defined inside the class definition inline

are redundant, as these functions will still be inlined by default.

Only if you are defining a function outside of a class in a header file should a definition be defined inline

.

Also, as a general rule of thumb for defining simple one-line objects like getters and setters in a class definition, don't worry about it. Just set all the one-line functions automatically, this is common practice.

You can quickly see which ones are recipients and setters from the get

or prefix set

, and also because their definitions are so short. It can also help group them together physically.

0


source


  • While it is customary to place getters and setters in file headers, I would argue that this is not a good practice. If you need to change these functions at all, or they need to be more complex than one function, then all compilers using this header must be recompiled. Just create a rule for function definitions only in .cpp files and compile with LTCG or LTO if you are concerned that the compiler will no longer be inlined.

  • While historically the keyword inline

    can be a hint for the compiler to embed the function body in all modern compilers, I know to ignore it when choosing the optimal optimization. Its only modern use is to declare a built-in function, that is, one that can be present in multiple compilation units without breaking one definition rule. This is the concept of binding and a separate idea of ​​the inline function concept, although given the recent keyword reuse, the confusion is understandable. All functions declared in a class or structure definition are implicitly built-in functions, so there is no need to explicitly use the keyword inline

    .

0


source







All Articles