Clean Code Pt I
Breaking 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
Introduction
Why 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 you
Code you have to wade through
Code 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
Cost
Having a messy code base can be expensive
We write more code
We break something
No change is trivial
9
Craftsmanship
Knowledge and Work
Knowledge of Principles, Patterns,
Practices
Hard work and practice
Learning to write Clean Code is hard work!
10
Identifying Bad Code
Unfortunately 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
Names
Ensure we use meaningful names
Use them everywhere!
Variables, Functions, Arguments,
Classes…
Don’t be afraid to rename! (Ctrl r+r)
13
Comments
Don’t write them
Code should explain itself
Only write comments in exceptional
circumstances
14
Formatting
Ensure spacing between concepts
Keep code well indented
Use resharper to format
Keep variable names consistent
15
Functions
Small and Concise - DO ONE THING
Give it a name that explains what it does
Can help with spotting if it does two things
Avoid many arguments in the method definition
Have no side effects
16
Coming Soon
Objects and Data Structures
Error Handling
Unit Tests
Classes
Emergent Design
17
Recap
Continue doing Katas and practicing these
techniques as often as possible
We will look at more concepts and ideas soon
18
Reading
If there is one book you read to help you
become a better software craftsman
Clean Code: A Handbook of Software Craftsmanship
Robert C. Martin
http://www.amazon.co.uk/Clean-Code-Handbook-Software-
Craftsmanship/dp/0132350882
19

Clean Code Pt I

  • 1.
    Clean Code PtI Breaking Bad Habits Series
  • 2.
    Introduction ‘Writing clean codeis 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
  • 3.
    Introduction Why is CleanCode important? What is Clean Code and how can we write it? How can we identify Bad Code? 3
  • 4.
    What is CleanCode? 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
  • 5.
    What is CleanCode? 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
  • 6.
    What is BadCode? Bad Code can bring down a company Code that impedes you Code you have to wade through Code full of hidden pitfalls 6
  • 7.
    So why didyou 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
  • 8.
  • 9.
    Cost Having a messycode base can be expensive We write more code We break something No change is trivial 9
  • 10.
    Craftsmanship Knowledge and Work Knowledgeof Principles, Patterns, Practices Hard work and practice Learning to write Clean Code is hard work! 10
  • 11.
    Identifying Bad Code Unfortunatelythere 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
  • 12.
    How to writeClean Code? Practice! Some easy things to do... 12
  • 13.
    Names Ensure we usemeaningful names Use them everywhere! Variables, Functions, Arguments, Classes… Don’t be afraid to rename! (Ctrl r+r) 13
  • 14.
    Comments Don’t write them Codeshould explain itself Only write comments in exceptional circumstances 14
  • 15.
    Formatting Ensure spacing betweenconcepts Keep code well indented Use resharper to format Keep variable names consistent 15
  • 16.
    Functions Small and Concise- DO ONE THING Give it a name that explains what it does Can help with spotting if it does two things Avoid many arguments in the method definition Have no side effects 16
  • 17.
    Coming Soon Objects andData Structures Error Handling Unit Tests Classes Emergent Design 17
  • 18.
    Recap Continue doing Katasand practicing these techniques as often as possible We will look at more concepts and ideas soon 18
  • 19.
    Reading If there isone book you read to help you become a better software craftsman Clean Code: A Handbook of Software Craftsmanship Robert C. Martin http://www.amazon.co.uk/Clean-Code-Handbook-Software- Craftsmanship/dp/0132350882 19