Clean Code
Clean Code
• https://www.slideshare.net/dhelper/writing-clean-code-in-c-and-net
• https://www.slideshare.net/theojungeblut/2013-106-clean-code-part-i-design-patterns
• https://www.slideshare.net/BlackRabbitCoder/automating-coding-standards
• https://www.codeproject.com/Articles/539179/Some-practices-to-write-better-Csharp-NET-code
• http://stackoverflow.com/questions/2834439/what-are-some-alternatives-to-resharper
2
3
• Easy to read & understand
• Impossible to hide bugs
• Easy to extend
• Easy to change
• Well documented (self documenting code)
• Well formatted
4
A developer spend 60% – 80% of his time
understanding code.
5
6
I like my code to be elegant and
efficient. Clean code does one
thing well.
Straightforward logic
Bjarne Stroustrup Hard for bugs to hide
Error Handling
7
Clean code is simple and direct.
Clean code reads like well-
written prose.
Readability
Grady Booch Crisp abstraction
8
Clean code can be read
Clean Code should be literate
Meaningful names
Dave Thomas Unit & Acceptance Tests
One way rather than many
ways for doing one thing
9
Clean code always looks like it
was written by someone who cares
Michael Feathers
10
You know you are working on clean
code when each routine you read
turns out to be pretty much what
you expected
Code makes it look like
Ward Cunningham the language was made for
the problem
11
Simple
Abstraction
12
The Dumb The Smart
Does everything Wrong Does everything Right
13
Variables
Names
Namespaces Parameters
Classes
14
Use Intention-Revealing Names
Take care with your names and change them when you find better ones
It should tell you why it exists, what it does, and how it is used
If a name requires a comment, then the name does not reveal its intent
Choosing good names takes time but saves more than it takes
15
Use Intention-Revealing Names
16
Use Intention-Revealing Names
•What this function Exactly does?!!
17
Avoid Disinformation
18
Make Meaningful Distinctions
Number-series naming
Noise Words
Distinguish names in such a way that the reader knows what the differences offer
19
Use Pronounceable Names
If you can’t pronounce it,
you can’t discuss it
20
Avoid Encoding
Member Prefix
The more we read the code, the less we see the prefixes.
21
Avoid Encoding
22
Always use English
23
Class Names
Data Info
24
Method Names
Methods should have verb or
verb phrase names
25
Don’t be Cute
26
Pick One Word per Concept
Can you spot the
difference?!!
A consistent lexicon is a great gift to the programmers who must use your code.
27
Add Meaningful Context
Better…
addrCity, addrStreet, addrZipCode, addrHouseNumber
class Address
28
Naming (Verbs & Keywords)
Good
Better
Good
Better
29
Small
Should be small
< 20 lines
30
Do one thing
Do it well
Do it ONLY
31
Arguments
The ideal number of arguments
Zero (Niladic) for a function is zero
One (Monadic)
Two (Dyadic)
Easier to understand
Three (Triadic)
33
Flag arguments
34
Dyadic functions
Reasonable
Problematic
35
Triadic functions
36
Output arguments
37
Structured Programming
38
Conditional statements & Loops
39
Have No side effects
Side Effect
40
Have No side effects
Side Effect
41
Returning result
42
Using variables
43
Using Expressions
44
“Don’t comment bad code,
rewrite it”
45
Explain yourself in Code
46
Informative comments
47
Explanation of Intent
48
TODO comments
49
Amplification
50
Mumbling
51
Redundant Comments
52
Commented-Out Code
53
Noise Comments
54
Dependency magnet
55
Exceptions or Error codes?
Open/Closed
principle
56
Extract try/catch blocks
57
Naming a class
The more ambiguous the class name, the more likely it has too
many responsibilities
59
Class organization
60
Single Responsibility Principle
61
Abstraction
Concrete
Abstract
62
Encapsulation
63
Inheritance
64
The law of Demeter
65
Don’t Repeat Yourself (No Duplication)
66
Don’t Repeat Yourself (No Duplication)
67
Don’t Repeat Yourself (No Duplication)
68
Principles of Object Oriented Design
Liskov substitution
Dependency Inversion
69
Liskov substitution
70
Avoid Negative conditionals
71
Encapsulate Boundary Conditions
72
Name at the appropriate level of Abstraction
73
Design Guidelines
Microsoft has official code conventions, follow the link and pay some time
reading..
https://msdn.microsoft.com/en-us/library/ms229042.aspx
74
75