1

Most likely an incredibly silly question but here goes. So I am currently reading the C book by Kernighan and Ritchie and I am a bit confused as to what the effect of too many data connections has on the program. Is it strictly to do with the appearance and readability of the code or does it also have an adverse effect on the program ie cause it to be slower.

Below is the paragraph in question.

"If a large number of variables must be shared among functions, external variables are more convenient and efficient than long argument lists. As pointed out in Chapter 1, however, this reasoning should be applied with some caution, for it can have a bad effect on program structure, and lead to programs with too many data connections between functions"

DIB98
  • 53
  • 1
  • 6
  • A good write-up : https://softwareengineering.stackexchange.com/questions/148108/why-is-global-state-so-evil – Sander De Dycker Jun 25 '18 at 09:44
  • It's mostly for human understanding. – Steve Summit Jun 25 '18 at 11:06
  • If a function does one, cohesive, well-defined task, it should not need a lot of different inputs and/or outputs. If a function has lots of different inputs and/or outputs (no matter whether they're conventional function arguments, or global variables), experience has shown that the function is going to be harder to understand and use, because it's probably not performing one, cohesive, well-defined task. – Steve Summit Jun 25 '18 at 11:27
  • Oh I see thank you for the explanation. Thank you for the links also, v helpful. – DIB98 Jun 25 '18 at 16:42

1 Answers1

0

Too many external variables and the reader (and maybe the author) will have a problem keeping up the big picture in mind. In particular, the more the connections become, the more difficult it is for the programmer to keep track which functions actually read and write these variables. Read more in Are global variables bad?

Too big argument lists, and there is going to be some overhead for your program, since calling a function is costly. The function arguments should be pushed to the stack (or in registers). Read more in How much overhead is there in calling a function in C++?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • To a first approximation, for most purposes, calling a function is **not** costly, and avoiding function calls for this reason is a great way to create monolithic, impossible-to-maintain programs. – Steve Summit Jun 25 '18 at 11:08
  • 1
    Cheers, cleared things up for me! – DIB98 Jun 25 '18 at 16:40