Clean Code Pt I

19
Clean Code Pt I Breaking Bad Habits Series

Transcript of Clean Code Pt I

Page 1: Clean Code Pt I

Clean Code Pt IBreaking Bad Habits Series

Page 2: Clean Code Pt I

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

Page 3: Clean Code Pt I

IntroductionWhy is Clean Code important?

What is Clean Code and how can we write it?

How can we identify Bad Code?

3

Page 4: Clean Code Pt I

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

Page 5: Clean Code Pt I

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

Page 6: Clean Code Pt I

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

Page 7: Clean Code Pt I

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

Page 8: Clean Code Pt I

Measuring

8

Page 9: Clean Code Pt I

CostHaving a messy code base can be expensive

We write more codeWe break somethingNo change is trivial

9

Page 10: Clean Code Pt I

CraftsmanshipKnowledge and Work

Knowledge of Principles, Patterns, Practices

Hard work and practice

Learning to write Clean Code is hard work!

10

Page 11: Clean Code Pt I

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

Page 12: Clean Code Pt I

How to write Clean Code?Practice!

Some easy things to do...

12

Page 13: Clean Code Pt I

NamesEnsure we use meaningful names

Use them everywhere!Variables, Functions, Arguments,

Classes…

Don’t be afraid to rename! (Ctrl r+r)

13

Page 14: Clean Code Pt I

CommentsDon’t write them

Code should explain itself

Only write comments in exceptional circumstances

14

Page 15: Clean Code Pt I

FormattingEnsure spacing between concepts

Keep code well indentedUse resharper to format

Keep variable names consistent

15

Page 16: Clean Code Pt I

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

Page 17: Clean Code Pt I

Coming SoonObjects and Data StructuresError HandlingUnit TestsClassesEmergent Design

17

Page 18: Clean Code Pt I

RecapContinue doing Katas and practicing these techniques as often as possible

We will look at more concepts and ideas soon

18

Page 19: Clean Code Pt I

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