c - Lesser of two evils when using globals via extern -
i'm working old code uses many global variables. i'm aware of many of disadvantages of using global variables, question not whether should using global variables or not.
after reviewing of code i've noticed 2 patterns , i'm trying decide 1 worse , why.
a similarity between 2 patterns global variables exposed using "extern".
the main difference between 2 patterns is:
some globals extern'ed/exposed in header files, in turn included in many source files #include
other globals extern'ed/exposed directly in source file itself
which of these 2 believe worse other? , why?
would consider them equally bad? , why?
1) hide can. if not need visible, don't allow people use them (by providing declarations).
2) use static if extern not necessary and… hide can.
which of these 2 believe worse other? , why?
the first; because unnecessarily visible other translations. second can cause linker errors, take insider knowledge use correctly in source/translation. linker issue can resolved making static
(again, if declaration visible 1 translation).
would consider them equally bad? , why?
no. if can hide globals' implementation , restrict access, have done codebase favor.
Comments
Post a Comment