code complexity & repetition analysis tool
at main 285 lines 5.4 kB view raw view rendered
1# Lines of Code (LOC) 2 3## Overview 4 5Lines of Code (LOC) is a fundamental software metric that measures the size of a codebase. Mccabre provides several LOC variants to give you a complete picture. 6 7## Metrics Provided 8 9### Physical LOC 10 11Total number of lines in the file, including everything. 12 13```rust 14fn hello() { // Line 1 15 16 17 println!("Hi"); // Line 4 18} // Line 5 19// Physical LOC = 5 20``` 21 22### Logical LOC 23 24Non-blank, non-comment lines that contain actual code. 25 26```rust 27fn hello() { // Logical 28 // This is a comment (not counted) 29 30 println!("Hi"); // Logical 31} // Logical 32// Logical LOC = 3 33``` 34 35### Comment Lines 36 37Lines that contain comments (single-line or multi-line). 38 39```rust 40// This is counted // Comment line 41/* This too */ // Comment line 42let x = 5; // inline // Code line (code takes precedence) 43``` 44 45### Blank Lines 46 47Lines that contain only whitespace. 48 49## Why LOC Matters 50 51### Productivity Tracking 52 53- Monitor codebase growth over time 54- Estimate project size 55- Compare different implementations 56 57### Maintenance Effort 58 59Larger codebases generally require: 60 61- More time to understand 62- More effort to maintain 63- More potential for bugs 64 65### Code Density 66 67Compare logical vs physical LOC: 68 69- High ratio (logical/physical): Dense code, few comments 70- Low ratio: Well-commented, more likely to be readable code 71 72## Using LOC with Mccabre 73 74### Commands 75 76Mccabre provides multiple ways to analyze LOC: 77 78```bash 79# Dedicated LOC analysis with ranking 80mccabre loc src/ 81 82# Full analysis including complexity and clones 83mccabre analyze src/ 84 85# Complexity analysis includes LOC 86mccabre complexity src/ 87``` 88 89### The `loc` Command 90 91The `loc` command provides focused LOC analysis with powerful ranking capabilities. 92 93#### Basic Usage 94 95```bash 96# Rank files by logical LOC (default) 97mccabre loc src/ 98 99# Rank files by physical LOC 100mccabre loc src/ --rank-by physical 101 102# Rank files by comments 103mccabre loc src/ --rank-by comments 104 105# Rank files by blank lines 106mccabre loc src/ --rank-by blank 107``` 108 109#### Directory Ranking 110 111Group and rank files by directory: 112 113```bash 114# Rank directories by total LOC, files ranked within each 115mccabre loc src/ --rank-dirs 116 117# Rank directories by comments 118mccabre loc src/ --rank-dirs --rank-by comments 119``` 120 121### Sample Output 122 123#### File Ranking 124 125```text 126LINES OF CODE ANALYSIS 127 128SUMMARY 129Total files analyzed: 12 130Total physical LOC: 2246 131Total logical LOC: 1360 132Total comment lines: 135 133Total blank lines: 751 134 135FILES RANKED BY Logical LOC 136 1371. crates/core/src/complexity/loc.rs (Logical LOC: 297) 138 Physical: 403 | Logical: 297 | Comments: 37 | Blank: 69 139 1402. crates/core/src/reporter.rs (Logical LOC: 212) 141 Physical: 260 | Logical: 212 | Comments: 16 | Blank: 32 142``` 143 144#### Directory Ranking 145 146```text 147DIRECTORIES RANKED BY Logical LOC 148 149DIRECTORY: crates/core/src 150 Total Physical: 1126 | Logical: 583 | Comments: 42 | Blank: 501 151 152 Files: 153 reporter.rs (Logical LOC: 212) - P: 260 | L: 212 | C: 16 | B: 32 154 loader.rs (Logical LOC: 150) - P: 204 | L: 150 | C: 8 | B: 46 155``` 156 157### JSON Output 158 159```bash 160# File ranking as JSON 161mccabre loc src/ --json 162 163# Directory ranking as JSON 164mccabre loc src/ --rank-dirs --json 165``` 166 167```json 168{ 169 "files": [ 170 { 171 "path": "src/main.rs", 172 "metrics": { 173 "physical": 120, 174 "logical": 85, 175 "comments": 25, 176 "blank": 10 177 } 178 } 179 ], 180 "directories": null, 181 "summary": { 182 "total_files": 1, 183 "total_physical": 120, 184 "total_logical": 85, 185 "total_comments": 25, 186 "total_blank": 10 187 } 188} 189``` 190 191## Interpreting Results 192 193### Healthy Ratios 194 195**Comment Ratio**: `comments / logical` 196 197- 0.1-0.3 (10-30%): Generally good 198- <0.05: Likely under-commented 199- >0.5: Possibly over-commented or tutorial code 200 201**Code Density**: `logical / physical` 202 203- 0.6-0.8: Good balance 204- <0.5: Many blank lines/comments (verbose) 205- >0.9: Very dense (potentially hard to read) 206 207## LOC Limitations 208 209### Not a Quality Metric 210 211More LOC doesn't mean: 212 213- ✗ Better code 214- ✗ More features 215- ✗ More value 216 217### Context Matters 218 219Compare LOC only within similar contexts: 220 221- Same language 222- Same problem domain 223- Same team/style 224 225### Generated Code 226 227LOC counts everything, including: 228 229- Auto-generated code 230- Vendored dependencies 231- Build artifacts 232 233Use `.gitignore` to exclude these (Mccabre respects gitignore). 234 235## Advanced Use Cases 236 237### Finding the Largest Files 238 239```bash 240# Top 10 largest files by logical LOC 241mccabre loc src/ --rank-by logical | head -30 242``` 243 244### Identifying Under-Commented Code 245 246```bash 247# Files ranked by comment count (ascending) 248mccabre loc src/ --rank-by comments 249``` 250 251### Directory Hotspots 252 253```bash 254# Find directories with the most code 255mccabre loc . --rank-dirs --rank-by logical 256``` 257 258### Tracking LOC Over Time 259 260```bash 261# Create baseline 262mccabre loc src/ --json > baseline.json 263 264# Later... 265mccabre loc src/ --json > current.json 266 267# Compare using jq 268jq '.summary.total_logical' baseline.json 269jq '.summary.total_logical' current.json 270``` 271 272### CI Integration 273 274Integrate with your CI to track: 275 276- LOC growth per sprint 277- LOC per feature 278- Comment ratio trends 279- Detect large file additions 280 281## See Also 282 283- [Cyclomatic Complexity](./cyclomatic-complexity.md) 284- [Clone Detection](./clone-detection.md) 285- [Configuration](./configuration.md)