CLEAN CODE FOR HUMANS
Three Principles for clean code
- Always choose right tool for the job
- High signal to noise ratio (remove noise from code so that reader can easily read the logic and understand the intent)
- Self-documenting expressive code
Always choose right tool for the job
- Boundaries Matter
- Avoid Hack
- Avoid two language in one file (e.g. HTML , JS , CSS)
Maximize signal to Noise Ratio
- Logic that follow the TED rule.
- Terse (short and sweet)
- Expressive
- Do one Thing
Self-Documenting code
- Understanding the original programmer’s intent is most difficult
Self-Documenting code strives to do four things.
- Clear Intent
- Layers of Abstractions
- Format for readability
- Favor code over comment.
- Follow DRY Principles
Naming Class
- Noun
- Single Responsibility
- Be Specific
- Avoid generic suffixes ( for e.g. Product Repository class should know about the products , but should not expect a logic about sending email to site within that class)
Naming Methods
Naming Booleans
Assign Boolean Implicitly
Don’t Be AntI-negative
Ternary is Elegant
Avoid “Stringly”
Typed
String Concatenation
Strings are immutable in C#.Using StringBuilder class
Magic Number
Intermediate Variables
Be Declarative
Avoid Arrow Code
Watch for Flag Arguments
Too LOOOOOOOOONG FUNCTION
The Boy Scout Rule
Articheture
- Buy or Borrow, don't build it unless you're doing something very special.
- Focus on domain specifc problem how to solve business problems
Layers
Layers of codes means maintaining for long term
- Lack of Unit Tests
- if you're not running unit tests, your users are your testers.
Continuous Intergration
- Building code frequently improves code quality.
- Integration Problems are surfaced earlier
- Running automated unit tests helps find regression bugs.
- One large teams, this becomes even more important
Lack of Dependency Injection
Lazy Loading Problems (Lazy Loading Enable=false)
The each line is going back to SQL server and making round trip
(Lazy Loading Enable=false)
True in desktop app because it is connected to sql server and walk down the tree, but in web app it is terminally bad
Large Resultsets in EF
Always have a max limit cut it off
Big EDMX file
- Managing changes is painful, Metadata needs to be loaded, delay app
- Smaller unit of work edmx for e.g (customer and order, employee and payroll)