![]() |
![]() |
1514 | ![]() |
||||
![]() | |||||||
| CMA maintenance checks | |||||||
This object / function is only used in the same translation unit where it is defined. As this function is not used outside of this translation unit, it would be better practice to reduce its visibility by declaring it
static in C or moving it into an unnamed namespace in C++.
Any declarations to the function should be removed, or if they appear in the same source file, made static.
Reducing the visibility of objects and functions ensures that future modifications are in less danger of silently compiling and linking with unintentional behavior.
Example:
// TU1.cpp
int sort ()
{
// implement sort specific to TU1
}
void foo()
{
sort ();
}
// TU2.cpp
void bar()
{
}
A new library is added with an external function 'Sort', and is to be used in TU2.
Example:
// TU1.cpp
int sort ()
{
// implement sort specific to TU1
}
void foo()
{
sort ();
}
// TU2.cpp
int sort (); // typo here. 'S' not 's' in sort.
void bar()
{
sort ();
}
The code still compiles and links, however the logic is flawed. Restricting the visibility of function 'sort' in TU1 would have resulted in a link error, as sort would no longer be available to other translation units.
Example:
// TU1.cpp
static int sort ()
{
// implement sort specific to TU1
}
void foo()
{
sort ();
}
// TU2.cpp
int sort (); // typo here. 'S' not 's' in sort.
void bar()
{
sort (); // OK: Causes link error, 'sort' is not defined
}
See also:
![]() | ||
| QA·C Source Code Analyser 8.1.2
© 2013 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | Contents |