c - Dealing with a large amount of variables -
i writing large c program , have 1 file file.c
contains large number of variables. let's has function named func()
being used other files.
unfortunately, function func()
has use large amount of variables , since contains lot of code i've decided write functions used func()
in order make code more readable. now, have 3 main possibilities:
declare variables global.
declare variables local inside
func()
, pass these variables arguments functions insidefile.c
using struct (or other trick can think of).declare variables local , instead of using other functions throw code inside
func()
result in long , unreadable code.
any suggestions ?
in general, second option best.
over-use of global variables considered bad practice.
it's may source of poor performance in cases because global has loaded, whereas if can keep variable local can stored in register.
also, code reuse easier. function receives parameter more general. can called in various use cases. function uses global can performed in 1 context.
in addition, globals available, , thus, probably, used everywhere in program. means accessed , written many places. validating values can cause overhead.
bottom line: modular code more readable , easier maintain.
Comments
Post a Comment