Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Sesion03 - Clean Code
Sesion03 - Clean Code
Clean Code
Writing Code for Humans
course by Cory House.
4. Lab - Java
3) Self-documenting
Dynamic
JS in C#
HTML in SQL
s
C#, Ruby
SQL
Python, etc.
Dynamic
SQL in
C# strings
• Leverage Libraries
Clean
Noise
High cyclomatic complexity Huge classes
Excessive indentation Long methods
Zombie code Repetition
Unnecessary comments No whitespace
Poorly named structures Overly verbose
Duplication Issues:
1. Decreases signal to noise ratio.
2. Increases the number of lines of code.
3. Creates a maintenance problem.
Clear intent
Layers of abstractions
Format for readability
Favor code over comments
Clean
Clean
User
Account
QueryBuilder Specific names lead to smaller more cohesive
classes
ProductRepository
Say what?
Get
Process
Pending
Start
Solution?
Refactor until the method name completely describes what it does.
RegistUser
No standard RegisUser
RegisterUsr
We talk about code
Dirty
open
start
status
login
Dirty Clean
open isOpen
start done
status isActive
login loggedIn
Dirty
on/disable
quick/slow
lock/open
slow/max
Dirty Clean
on/disable on/off
quick/slow fast/slow
lock/open lock/unlock
slow/max min/max
Dirty
Dirty Clean
Dirty
Dirty Clean
1. Fewer lines
2. No separate initialization
3. No repetition
4. Reads like speech
Clean
Clean
1. Strongly typed
2. Intellisense support
3. Documents states
4. Searchable
Dirty
Dirty
Dirty Clean
Dirty Clean
1. Intermediate variables
2. Encapsulate via function
Clean
Clean
Clean
InsuranceRate table
Examples
• Insurance rates
• Pricing structures
• Complex and dynamic business
Duplication Indentation
Unclear
> 1task
intent
Extract Method
Fail Fast
Return Early
Dirty
Clean
Clean
Clean
Dirty
Dirty
Clean
Clean
Multiple Hard to
Conditionals digest
Dirty
Dirty Clean
Clean
Clean
Don’t apologize.
Fix it before commit/merge.
Reduces readability
Creates ambiguity
Hinders refactoring
To Do
Summary
Documentation
Standardize
Watch out:
May be an apology or warning comment in disguise
Often ignored
Clean
Clean
Better testability
Method 1aii
Method 1aiii
Method 1b
Method 1c
Speaker
Register
Speaker Speaker
Register Register
Validate Registration
Validate Data
Appears Exceptional
Approve Sessions
Save Speaker
Always leave the code you’re editing a little better than you found it.
[Robert C Martin]