Code Metrics - Lines of Code, Coupling
Code metrics provide quantitative measurements of software characteristics. They help teams assess code quality, identify problem areas, and track improvement over time.
Size Metrics
+---------------------------------------------------------+
| SIZE METRICS |
+---------------------------------------------------------+
| |
| Lines of Code (LOC) |
| - Total lines in the source file |
| - Simple but includes comments and whitespace |
| |
| Physical Lines of Code (PLOC) |
| - Non-blank, non-comment lines |
| - More accurate measure of actual code |
| |
| Logical Lines of Code (LLOC) |
| - Counted by statements |
| - Language-dependent |
| |
| Non-Comment Lines of Code (NCLOC) |
| - All lines excluding comments |
| - Commonly used in industry |
| |
+---------------------------------------------------------+
Coupling Metrics
+---------------------------------------------------------+
| COUPLING TYPES |
+---------------------------------------------------------+
| |
| CONTENT COUPLING (worst) |
| - Module A accesses internal data of Module B |
| |
| COMMON/SHARED COUPLING |
| - Both modules share global data |
| |
| CONTROL COUPLING |
| - Module A passes control flag to Module B |
| |
| STAMP COUPLING |
| - Module A passes data structure to Module B |
| |
| DATA COUPLING (best) |
| - Module A passes data items to Module B |
| - Minimal dependency |
| |
+---------------------------------------------------------+
Cohesion Metrics
Cohesion measures how closely related the elements within a module are. Higher cohesion is better โ it means the module has a clear, focused purpose.
Cohesion Levels (best to worst):
1. Functional - All elements contribute to a single task
2. Sequential - Output of one is input of next
3. Communicational - Elements operate on same data
4. Procedural - Elements are part of a sequence
5. Temporal - Elements happen at same time
6. Logical - Elements are logically related
7. Coincidental - No meaningful relationship (worst)
Key Takeaways
- LOC measures size but not quality
- Low coupling and high cohesion are desirable
- Use metrics to identify code that needs attention
- Metrics are tools for improvement, not goals in themselves