Roadmap › 1. .NET › Step 26
CC
Clean Code, Testing & Best Practices
Naming, functions, error handling, code smells and refactoring, unit testing with xUnit and Moq, TDD, code review, Git workflow, security and logging best practices for .NET teams.
Quick revision for interview day. Print it or save it as a PDF.
Clean code rules
- Intention-revealing names; verbs for methods, nouns for classes.
- Functions: small, one thing, ≤ 3 params, guard clauses.
- No magic numbers; no commented-out code; comments explain why.
- Exceptions for exceptional cases; global handler; structured logs.
- Boy Scout rule; small PRs; consistent formatting.
Naming conventions (.NET)
| Element | Style | Example |
|---|---|---|
| Class, method, property | PascalCase | OrderService.PlaceOrder |
| Interface | I + PascalCase | IOrderRepository |
| Local, parameter | camelCase | orderTotal |
| Private field | _camelCase | _repository |
| Async method | Async suffix | GetOrderAsync |
| Constant | PascalCase | MaxRetryCount |
Testing
- AAA: Arrange · Act · Assert. FIRST tests.
- xUnit:
[Fact],[Theory]+[InlineData]; ctor = setup,Dispose= teardown;IClassFixture<T>. - Moq:
new Mock<T>(),.Setup(...).ReturnsAsync(...),.Verify(..., Times.Once). - Doubles: dummy, stub, fake, mock, spy.
- Pyramid: unit ≫ integration ≫ E2E.
WebApplicationFactory<Program>for API tests; Testcontainers for real SQL Server.TimeProvider/FakeTimeProviderfor time.
Code smells → refactorings
Long method → Extract Method · God class → Extract Class · Switch on type → Polymorphism · Duplicates → Extract/DRY · Long params → Parameter Object · Primitive obsession → Value Object · Feature envy → Move Method.
Git
bash
git switch -c feature/x
git add -p && git commit -m "Why + what"
git fetch && git rebase origin/main
git push -u origin feature/x
git stash / git stash pop
git cherry-pick <sha>
git revert <sha> # safe undo on shared branch
git reset --soft HEAD~1 # undo last local commit, keep changesSecurity quick list
Parameterised SQL · [Authorize] + ownership checks · HTTPS/HSTS · Identity password hashing · Antiforgery · Razor encoding · Key Vault for secrets · updated packages · no secrets/PII in logs.