Clean Code Pt I
-
Upload
paul-boocock -
Category
Presentations & Public Speaking
-
view
289 -
download
0
Transcript of Clean Code Pt I
Clean Code Pt IBreaking Bad Habits Series
Introduction‘Writing clean code is what you must do in
order to call yourself a professional.There is no reasonable excuse for doing
anything less than your best.’- Robert C. Martin (Clean Code: A Handbook of
Agile Software Craftsmanship)
2
IntroductionWhy is Clean Code important?
What is Clean Code and how can we write it?
How can we identify Bad Code?
3
What is Clean Code?For me:
Clean Code is readable and elegant. It is always concise, to the point and trustable (through tests). It should make the original author’s thought process clear.
4
What is Clean Code?I like my code to be elegant and efficient. The logic should be straightforward to make it hard for bugs to hide, the dependencies minimal to ease maintenance, error handling complete according to an articulated strategy, and performance close to optimal so as not to tempt people to make the code messy with unprincipled optimizations. Clean code does one thing well.
- Bjarne Stroustrup
5
Clean code can be read, and enhanced by a developer other than its original autor. It has unit and acceptance tests. It has meaningful names. It provides one way rather than may ways for doing one thing. It has minimal dependencies, which are explicitly defined, and provides a clear and minimal API. Code should be literate since depending on the language, not all necessary information can be expressed clearly in code alone.
- Dave Thomas
What is Bad Code?Bad Code can bring down a company
Code that impedes youCode you have to wade throughCode full of hidden pitfalls
6
So why did you write it?We’ve all written bad code
You were trying to go fast
We usually know when we’ve written messy code but we choose to leave it for another day
7
Measuring
8
CostHaving a messy code base can be expensive
We write more codeWe break somethingNo change is trivial
9
CraftsmanshipKnowledge and Work
Knowledge of Principles, Patterns, Practices
Hard work and practice
Learning to write Clean Code is hard work!
10
Identifying Bad CodeUnfortunately there is no universal method to identify bad code
One thing to remember, there is Clean Code in our code base
Because we don’t own it doesn’t make it bad
11
How to write Clean Code?Practice!
Some easy things to do...
12
NamesEnsure we use meaningful names
Use them everywhere!Variables, Functions, Arguments,
Classes…
Don’t be afraid to rename! (Ctrl r+r)
13
CommentsDon’t write them
Code should explain itself
Only write comments in exceptional circumstances
14
FormattingEnsure spacing between concepts
Keep code well indentedUse resharper to format
Keep variable names consistent
15
FunctionsSmall and Concise - DO ONE THING
Give it a name that explains what it doesCan help with spotting if it does two things
Avoid many arguments in the method definitionHave no side effects
16
Coming SoonObjects and Data StructuresError HandlingUnit TestsClassesEmergent Design
17
RecapContinue doing Katas and practicing these techniques as often as possible
We will look at more concepts and ideas soon
18
ReadingIf there is one book you read to help you become a better software craftsmanClean Code: A Handbook of Software CraftsmanshipRobert C. Martinhttp://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
19