1

I was just wondering, is it bad practice to define small functions, such as getters and setters, that only influence the member variables of the function and don't depend on external header file includes?

I am specifying that they don't depend on external header files so as to make it clear that I am trying to avoid circular includes.

Here is one example from a recent program I am writing:

void EnableDirtyFlag() { m_DirtyFlag = true; }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Alex
  • 75
  • 7
  • 3
    You have to remember to define them as either `static` or `inline`, or possibly both. That way you won't get multiple-definition errors. – Some programmer dude Mar 05 '23 at 20:05
  • I would say it's good practise, it's good to have the same code acting as declaration and definition if possible. Do you think different? What's your concern? – john Mar 05 '23 at 20:05
  • 4
    The downside is that if you change the definition of the function you have to recompile every source file that uses that header. If the definition is in its own source file you only have to recompile that source file. – Pete Becker Mar 05 '23 at 20:08
  • 1
    @john -- ignoring the difference between declarations and definitions can lead to massive recompilations. That's why they are different things, and deciding to do both in the same place is not automatically good. – Pete Becker Mar 05 '23 at 20:10
  • 1
    Defining a function in a header file - whether small or not - is not on its own a "bad practice". – Drew Dormann Mar 05 '23 at 20:18
  • 2
    For small or medium sized projects, it's fine. For larger projects, it incurs an incremental build churn penalty that can become prohibitive. – Eljay Mar 05 '23 at 20:25
  • @Someprogrammerdude I didn't know you could stop multiple-definition errors like that, good to know! – Alex Mar 05 '23 at 20:40
  • @PeteBecker Oh okay, thank you for the info! I wasn't aware of the recompilation. So in very large projects, this could take a lot of time which would lower the efficiency correct? – Alex Mar 05 '23 at 20:42
  • @Alex - You also have to consider what are the odds that you ever have to change this function (without changing the class itself). It's not like you will change `= true` to `= false`, right? – BoP Mar 05 '23 at 21:06
  • 1
    for templates thats your only option, so it must be basically OK – pm100 Mar 06 '23 at 18:35
  • As it probably is a member function, static or inline are not necessary, if it is defined inside a class. – Sebastian Mar 06 '23 at 19:27

1 Answers1

0

It comes down to why you’re allowed to define them there. Doing so makes it easier for the compiler to inline things and comes with the bonus of avoiding duplicating the signature between a declaration and a definition, but has the disadvantages of introducing additional coupling between source files and increasing compilation time. Which of these is more important in any given case is a question of professional judgment.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76