![]() |
![]() |
1531 | ![]() |
||||
![]() | |||||||
| CMA maintenance checks | |||||||
This object / function is only used in one translation unit. As this function is not used outside of that translation unit, it would be better practice to reduce its visibility by moving its definition into that translation unit and: 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.h
int sort ();
// TU1.cpp
int sort ()
{
// implement sort only used in TU2
}
// TU2.cpp
#include "TU1.h"
void foo()
{
sort ();
}
// TU3.cpp
void bar()
{
}
A new library is added with an external function 'Sort', and is to be used in TU3.
Example:
int sort ();
// TU1.cpp
int sort ()
{
// implement sort only used in TU2
}
// TU2.cpp
#include "TU1.h"
void foo()
{
sort ();
}
// TU3.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 'sort' in TU1 by moving it to TU2 would have resulted in a link error, as sort would no longer be available to other translation units.
Example:
// TU2.cpp
static int sort ()
{
// implement sort specific to TU2
}
void foo()
{
sort ();
}
// TU3.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 |