-2

I write code in C. I have been striving to write more testable code but I am a little confused on deciding between writing pure functions that are really good for testing but require smaller functions and hurt readability in my opinion and writing functions that do modify some internal state.

For example (all state variables are declared static and hence are "private" to my module):

Which of this is more testable in your opinion:

int outer_API_bar()
{
   // Modify internal state
   internal_foo()
}

int internal_foo()
{
  // Do stuff

  if (internal_state_variable)
  {
     // Do some more stuff
     internal_state_variable = false;
  }
}

OR

int outer_API_bar()
{
   // Modify internal state

   internal_foo(internal_state_variable)

   // This could be another function if repeated many
   // times in the module
   if (internal_state_variable)
   {
      internal_state_variable = false;
   }
}

int internal_foo(bool arg)
{
  // Do stuff

  if (arg)
  {
     // Do some more stuff
  }
}

Although second implementation is more testable wrt to internal_foo as it has no sideeffects but it makes bar uglier and requires smaller functions that make it hard for the reader to even follow small snippets as he has to constantly shift attention to different functions.

Which one do you think is better ? Compare this to writing OOPS code, the private functions most of the time use internal state and are not pure. Testing is done by setting up internal state on a mock object instance and testing the private function. I am getting a little confused on whether to use or whether to pass in internal state to private functions for the sake of "testability"

closed as primarily opinion-based by user40980, GlenH7, user22815, durron597, ratchet freak Jul 18 '15 at 0:03

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

1

The second is better, but

int internal_foo(bool *arg); // modifies argument

keeps it testable while fixing your tiny function tradeoff, and

bool internal_foo(bool arg); // returns new value

is even better if you don't use the existing return value.

Note that in general though, having lots of little functions isn't necessarily a problem. If it makes your code less readable, you might just need to give them better names and/or re-work your logic.

The best and most general solution would be to remove your globals entirely, and have the caller pass an opaque API context into your outer function. This might be overkill in your case though, I can't tell from the code shown.

  • Thanks that makes sense. I do understand the argument for passing in opaque contexts / handles but compare that to OOPS where each object has private state as well that affects behaviour of methods. – user110036 Jul 28 '14 at 8:32
  • Object-private state is exactly analogous to opaque context state, and both are much better than global state. You can write perfectly OO code in C, you just have to do it without compiler support and syntactic sugar. – Useless Jul 28 '14 at 10:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.