Commit 6d9a8a9
Refactor: Transform entire codebase to functional programming paradigm
Comprehensive refactoring applying functional programming principles throughout the codebase:
immutability, pure functions, composability, and explicit I/O boundaries.
CORE TRANSFORMATIONS:
GitIgnoreParser.cs:
- Convert to immutable record type with ImmutableArray and ImmutableDictionary
- Introduce static factory methods (FromFile, FromPatterns, Empty)
- Separate I/O operations (FromFile) from pure pattern creation (FromPatterns)
- Implement lazy, immutable regex cache
- Remove all mutable state (_patterns, _regexCache mutation)
FileFilterService.cs:
- Decompose ShouldSkip into composable predicate functions
- Create pure filter predicates (IsReparsePoint, IsIgnoredFileName, etc.)
- Extract GetExtensions as pure function using yield return
- Separate I/O (ReadFirstLines) from business logic (IsGeneratedCode)
- Replace mutable _gitIgnoreLoaded flag with Lazy<GitIgnoreParser>
- Use pattern matching and switch expressions for data flow
GitHelper.cs:
- Replace imperative while loop with pure tail recursion
- Implement FindRepositoryRootRecursive with pattern matching
- Extract HasGitDirectory as pure predicate
- Achieve immutable, declarative directory traversal
ProjectScanner.cs:
- Introduce immutable ScanContext record for scan state
- Replace StringBuilder accumulation with yield return enumeration
- Eliminate mutable _gitRepoRoot field
- Decompose scanning into pure recursive functions
- Separate formatting functions (FormatDirectoryEntry, FormatFileEntry)
- Use LINQ and ImmutableArray for collection building
- Isolate side effects (progress reporting, error logging) clearly
ContentBuilder.cs:
- Introduce ContentSection record for lazy content generation
- Replace StringBuilder with functional composition using SelectMany
- Use ImmutableArray.Builder for section assembly
- Implement lazy evaluation with Func<string> delegates
PathResolver.cs:
- Extract SelectPath as pure function
- Refactor GetFolderName using functional composition with ?? operators
- Separate pure path transformation from I/O operations
- Use pattern matching in GetOutputPath
- Create small, composable pure functions (TryGetDirectoryName, CleanRootPath)
FileUtilities.cs:
- Separate I/O (CheckFileBinaryContent) from analysis (IsStreamBinary)
- Extract pure functions: CalculateBinaryRatio, IsBinaryByte
- Define Utf8Bom as immutable static readonly field
- Use SequenceEqual for BOM comparison
- Achieve clear functional pipeline: read → analyze → return
ConfigLoader.cs:
- Introduce Result<T> monad for functional error handling
- Separate ReadConfigFile (I/O) from ParseConfig (pure)
- Use pattern matching with Match method
- Eliminate imperative try-catch flow
OutputFormatter.cs:
- Separate ResolveOutputPath (pure) from WriteFile (I/O)
- Extract SerializeToJson as pure function
- Use StringComparison.OrdinalIgnoreCase for case-insensitive comparison
- Compose WriteToFile from pure and impure operations
StatsCalculator.cs:
- Introduce immutable ProjectStats record
- Separate GatherStats (I/O) from FormatStats (pure)
- Use pattern matching for null handling
- Extract CountFiles and CountLines as focused functions
Program.cs:
- Load GitIgnore patterns upfront using pattern matching
- Pass immutable GitIgnoreParser to FileFilterService constructor
- Clearly define I/O boundaries at application entry point
FUNCTIONAL PROGRAMMING PRINCIPLES APPLIED:
✓ Immutability: ImmutableArray, ImmutableDictionary, sealed records
✓ Pure functions: Explicit separation of computation from side effects
✓ Composability: Small, focused functions that compose elegantly
✓ Explicit data flow: No hidden state, all dependencies visible
✓ I/O isolation: Clear boundaries between pure logic and effects
✓ Type safety: Leverage C# records and pattern matching
✓ Lazy evaluation: Yield return, Lazy<T>, Func<T> delegates
✓ Declarative style: LINQ, pattern matching, expression-bodied members
✓ Error handling: Result types, null pattern matching vs exceptions
✓ Recursion: Tail-recursive functions replacing imperative loops
BENEFITS:
- Enhanced testability: Pure functions easy to test in isolation
- Thread safety: Immutable state eliminates race conditions
- Maintainability: Clear data flow, explicit dependencies
- Composability: Small functions combine into complex operations
- Predictability: Same inputs always produce same outputs
- Debugging: Deterministic execution, no hidden state mutations1 parent 0ddc1c9 commit 6d9a8a9
11 files changed
Lines changed: 660 additions & 364 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | | - | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | 16 | | |
20 | 17 | | |
21 | 18 | | |
| |||
26 | 23 | | |
27 | 24 | | |
28 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
29 | 39 | | |
30 | 40 | | |
31 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| 22 | + | |
21 | 23 | | |
22 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
23 | 50 | | |
24 | 51 | | |
25 | 52 | | |
26 | | - | |
27 | | - | |
| 53 | + | |
28 | 54 | | |
29 | 55 | | |
30 | 56 | | |
31 | 57 | | |
32 | 58 | | |
33 | 59 | | |
34 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
35 | 91 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
24 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
25 | 34 | | |
26 | | - | |
| 35 | + | |
27 | 36 | | |
28 | 37 | | |
29 | 38 | | |
30 | | - | |
31 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
32 | 42 | | |
33 | 43 | | |
34 | 44 | | |
35 | 45 | | |
36 | | - | |
37 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
38 | 49 | | |
39 | 50 | | |
40 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
41 | 67 | | |
42 | 68 | | |
0 commit comments