This document discusses principles and best practices for writing clean code, including SOLID principles, readability, testability, and maintainability. It emphasizes that readable code is understandable code with descriptive naming and avoiding confusion. Testable code focuses on isolation, decoupling, and separation of concerns. Maintainable code is easy to extend and understand with good test coverage. The document provides examples of good and bad code and recommends fighting code smells and anti-patterns.
3. In theory, it’s about...
S ingle responsibility principle
O pen/closed principle
L iskov substitution principle
I nterface segragation principle
D ependency inversion principle
16.01.2014
/
3
4. In practice, it’s all about... RTM
Readability
Testability
Maintainability
16.01.2014
/
4
5. Readable Code
«Programming is the art of telling another human what
one wants the computer to do.» - Donald Knuth
16.01.2014
/
5
6. Readable Code
«Programming is the art of telling another human what
one wants the computer to do.» - Donald Knuth
From http://www.osnews.com
16.01.2014
/
6
7. Readable Code
«Programming is the art of telling another human what
one wants the computer to do.» - Donald Knuth
From Pluralsight
16.01.2014
/
7
8. Readable Code
• Readable code is understandable code
• Naming
Class names
Context specific
Method names
Say exactly what they do
• The don’ts
Don’t confuse
Don’t hide
Don’t joke
Don’t be informal
• Tell a story!
«Programming is the art of telling another human what
one wants the computer to do.» - Donald Knuth
16.01.2014
/
8
9. Readable Code
Bad
«Programming is the art of telling another human what
one wants the computer to do.» - Donald Knuth
Better
16.01.2014
/
9
10. Testable Code
•
•
•
•
•
•
•
Making it testable, is making it clean
Isolation
Decoupling
Separation of concerns
TDD
Design
Clean is the side effect of testable
16.01.2014 /
10
11. Testable Code
«You know you should be unit testing, so
why aren’t you doing it?» - Roy Osherove
16.01.2014 /
11
Single responsibility principlethe notion that an object should have only a single responsibility.Open/closed principlethe notion that “software entities … should be open for extension, but closed for modification”.Liskov substitution principlethe notion that “objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program”. Interface segregation principlethe notion that “many client specific interfaces are better than one general purpose interface.”Dependency inversion principlethe notion that one should “Depend upon Abstractions. Do not depend upon concretions.”[5]Dependency injection is one method of following this principle.