Clean Code I - Best Practices

60
Clean Code I Chandler, Oct. 18 th , 2014 Best Practices Desert Code Camp 2014.2

description

All 3 Clean Code presentations provide great value by themselves, but taken together are designed to offer a holistic approach to successful software creation. This first session creates the foundation for the 2nd and 3rd Clean Code presentation on Dependency Injection, as it explains expected base knowledge. Why writing Clean Code makes us more efficient Over the lifetime of a product, maintaining the product is actually one - if not the most - expensive area(s) of the overall product costs. Writing clean code can significantly lower these costs. However, writing clean code also makes you more efficient during the initial development time and results in more stable code. You will be presented design patterns and best practices which will make you write better and more easily maintainable code, seeing code in a holistic way. You will learn how to apply them by using an existing implementation as the starting point of the presentation. Finally, patterns & practices benefits are explained. This presentation is based on C# and Visual Studio 2012. However, the demonstrated patterns and practice can be applied to every other programming language too. Note: Moving forwards this presentation will be updated with the latest version of the slides for the last event I did the presentation instead of creating new separate slide decks here on slideshare. Presentation dates and locations: 2014-11-14 SoCal Code Camp - Los Angeles, CA 2014-10-18 Desert Code Camp - Chandler, AZ 2014-10-11 Silicon Valley Code Camp, Los Altos Hills, CA

Transcript of Clean Code I - Best Practices

Page 1: Clean Code I - Best Practices

Clean Code I

Chandler, Oct. 18th, 2014

Best Practices

Desert Code Camp 2014.2

Page 2: Clean Code I - Best Practices

Theo Jungeblut• Engineering manager & lead by day

at AppDynamics in San Francisco

• Coder & software craftsman by night, first time dad and house builder

• Architects decoupled solutions & crafts maintainable code to last

• Worked in healthcare and factory automation, building mission critical applications, framework & platforms

• Degree in Software Engineeringand Network Communications

• Enjoys cycling, running and eating [email protected]

www.designitright.net

Page 3: Clean Code I - Best Practices

We are hiring!http://www.appdynamics.com/ careers

Page 4: Clean Code I - Best Practices

and there are much more positions

Page 5: Clean Code I - Best Practices

Your feedback is important!

http://www.speakerrate.com/theoj

Page 6: Clean Code I - Best Practices

Where to get the Slides

http://www.slideshare.net/theojungeblut

Page 7: Clean Code I - Best Practices

Overview

• Why Clean Code• Clean Code Developer Initiative • Principles and Practices• Code Comparison • Q&A

Page 8: Clean Code I - Best Practices

Does writing Clean Code make us more efficient?

Page 9: Clean Code I - Best Practices

The only valid Measurement of Code Quality

Page 10: Clean Code I - Best Practices

What is Clean Code?

Page 11: Clean Code I - Best Practices

Clean Code is maintainable

Source code must be:• readable & well structured• extensible• testable

Page 12: Clean Code I - Best Practices

Software Engineering

&Software

Craftsmanship

Page 13: Clean Code I - Best Practices

The “Must Read”-Book(s)by Robert C Martin

A Handbook of Agile Software Craftsmanship

“Even bad code can function. But if code isn’t clean, it can bring a development organization to its knees.”

Page 14: Clean Code I - Best Practices

Code Maintainability *

Principles Patterns Containers

Why? How? What?

Extensibility Clean Code Tool reuse

* from: Mark Seemann’s “Dependency Injection in .NET” presentation Bay.NET 05/2011

Page 15: Clean Code I - Best Practices

Clean Code Developer

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Page 16: Clean Code I - Best Practices

The 4 values of the CCD initiative

• Correctness• Evolvability• Production Efficiency• Continuous Improvement

http://blogs.telerik.com/justteam/posts/13-05-16/clean-code-developer-school

by Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de

Page 17: Clean Code I - Best Practices

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Clean Code Developer – 1st Iterationby Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de

Page 18: Clean Code I - Best Practices

Keep it simple, stupid(KISS)

Page 19: Clean Code I - Best Practices

KISS-Principle – “Keep It Simple Stupid”

http://blogs.smarter.com/blogs/Lego%20Brick.jpg

by Kelly Johnson

Page 20: Clean Code I - Best Practices

The Power of Simplicity

http://www.geekalerts.com/lego-iphone/

Graphic by Nathan Sawaya courtesy of brickartist.com

Graphic by Nathan Sawaya courtesy of brickartist.com

Page 21: Clean Code I - Best Practices

Chaos build from simplicity

Graphic by Nathan Sawaya courtesy of brickartist.com

Page 22: Clean Code I - Best Practices

Don’t repeat yourself

(DRY)

Page 23: Clean Code I - Best Practices

Don’t repeat yourself (DRY)by Andy Hunt and Dave Thomas in their book “The Pragmatic Programmer”

// Code Copy and Paste Method public Class Person { public string FirstName { get; set;} public string LastName { get; set;} public Person(Person person) { this.FirstName = string.IsNullOrEmpty(person.FirstName)

? string.Empty : string.Copy(person.FirstName);

this.LastName = string.IsNullOrEmpty(person.LastName) ? string.Empty : string.Copy(person.LastName);

}

public object Clone() { return new Person(this); }}

// DRY Method public Class Person { public string FirstName { get; set;} public string LastName { get; set;} public Person(Person person) { this.FirstName = person.FirstName.CloneSecured(); this.LastName = person.LastName.CloneSecured(); }

public object Clone() { return new Person(this); }}

public static class StringExtension { public static string CloneSecured(this string original) { return string.IsNullOrEmpty(original) ? string.Empty : string.Copy(original); } }

Page 24: Clean Code I - Best Practices

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Clean Code Developer – 1st Iterationby Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de

Page 25: Clean Code I - Best Practices

Clean Code Developer – 2nd Iterationby Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Page 26: Clean Code I - Best Practices

Separation of Concerns (SoC)

Single Responsibility Principle

(SRP)

Page 27: Clean Code I - Best Practices

http://www.technicopedia.com/8865.html

The Product

Page 28: Clean Code I - Best Practices

http://www.technicopedia.com/8865.html

Component / Service

Page 29: Clean Code I - Best Practices

http://technicbricks.blogspot.com/2009/06/tbs-techpoll-12-results-2009-1st.html

Class, Struct, Enum etc.

Page 30: Clean Code I - Best Practices

Separation of Concerns (SoC)

• “In computer science, separation of concerns (SoC) is the process of separating a computer program into distinct features that overlap in functionality as little as possible.

•A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors. “

probably by Edsger W. Dijkstra in 1974

http://en.wikipedia.org/wiki/Separation_of_Concerns

Page 31: Clean Code I - Best Practices

Single Responsibility Principle (SRP)

“Every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class.”

by Robert C Martin

http://www.ericalbrecht.com

http://en.wikipedia.org/wiki/Single_responsibility_principle

public class Logger : ILogger{ public Logger(ILoggingSink loggingSink) {} public void Log(string message) {}}

Page 32: Clean Code I - Best Practices

Read, Read, Read

Page 33: Clean Code I - Best Practices

Clean Code Developer – 2nd Iterationby Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Page 34: Clean Code I - Best Practices

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Clean Code Developer – 3rd Iterationby Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de

Page 35: Clean Code I - Best Practices

Information Hiding Principle

(IHP)

Page 36: Clean Code I - Best Practices

“.. information hiding is the principle of segregation of the design decisions on a computer program that are most likely to change, ..”

Information Hiding Principle (IHP)by David Parnas (1972)

http://en.wikipedia.org/wiki/Information_hiding

Page 37: Clean Code I - Best Practices

Interfaces / Contracts

public interface ILogger{ void Log(string message);}

• Decouple Usage and Implementation through introduction of a contract• Allows to replace implementation without changing the consumer

public class Logger : ILogger{ public Logger(ILoggingSink loggingSink) {} public void Log(string message) {}}

Page 38: Clean Code I - Best Practices

Liskov Substitution Principle

(LSP)

Page 39: Clean Code I - Best Practices

“Liskov’s notion of a behavioral subtype defines a notion of substitutability for mutable objects”

Liskov Substitution Principle (LSP)by Barbara Liskov, Jannette Wing (1994)

http://en.wikipedia.org/wiki/Liskov_substitution_principlehttp://www.objectmentor.com/resources/articles/lsp.pdf

Page 40: Clean Code I - Best Practices

Dependency Inversion Principle

(DIP)

Page 41: Clean Code I - Best Practices

Dependency Inversion Principle (DIP)

• “High-level modules should not depend on low-level modules. Both should depend on abstractions.

• Abstractions should not depend upon details. Details should depend upon abstractions.”

http://en.wikipedia.org/wiki/Dependency_inversion_principlehttp://www.objectmentor.com/resources/articles/dip.pdf

by Robert C. Martin

Page 42: Clean Code I - Best Practices

Next Desert Code Camp April 2015

http://www.desertcodecamp.com

Page 43: Clean Code I - Best Practices

Mini Conferences – User Groups

Page 44: Clean Code I - Best Practices

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Clean Code Developer – 3rd Iterationby Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de

Page 45: Clean Code I - Best Practices

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Clean Code Developer – 4th Iterationby Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de

Page 46: Clean Code I - Best Practices

Open Closed Principle(OCP)

Page 47: Clean Code I - Best Practices

An implementation is open for extension but closed for modification

Open/Closed Principle (OCP)by Bertrand Meyer (1988)

http://en.wikipedia.org/wiki/Open/closed_principle

Page 48: Clean Code I - Best Practices

Law of Demeter(LoD)

Page 49: Clean Code I - Best Practices

“• Each unit should have only limited knowledge about

other units: only units “closely” related to the current unit.

• Each unit should only talk to its friends; don’t talk to strangers

• Only talk to your immediate friends.”

Law of Demeter (LoD)Northeastern University (1987)

http://en.wikipedia.org/wiki/Law_Of_Demeter

Page 50: Clean Code I - Best Practices

Single Responsibility Principle

Open/Closed Principle

Liskov Substitution Principle

Interface Segregation Principle

Dependency Inversion Principle

SOLID

Robert C Martin: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

Page 51: Clean Code I - Best Practices

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Clean Code Developer – 4th Iterationby Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de

Page 52: Clean Code I - Best Practices

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Clean Code Developer – 5th Iterationby Ralf Westphal & Stefan Lieser – http://www.clean-code-developer.de

Page 53: Clean Code I - Best Practices

Clean Code Developer

Graphic by Michael Hönnig http://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/

Page 54: Clean Code I - Best Practices

Summary Clean Code

Maintainability is achieved through:

• Readability (Coding Guidelines)

• Simplification and Specialization (KISS, SoC, SRP, OCP, )

• Decoupling (LSP, DIP, IHP, Contracts, LoD, CoP, IoC or SOA)

• Avoiding Code Bloat (DRY, YAGNI)

• Quality through Testability (all of them!)

Page 55: Clean Code I - Best Practices

Downloads, Feedback & Comments:

Q & A

Graphic by Nathan Sawaya courtesy of brickartist.com

[email protected] www.speakerrate.com/theoj

Page 56: Clean Code I - Best Practices

References… http://clean-code-developer.comhttp://blogs.telerik.com/justteam/posts/13-05-16/clean-code-developer-schoolhttp://michael.hoennig.de/2009/08/08/clean-code-developer-ccd/http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOodhttp://www.manning.com/seemann/http://en.wikipedia.org/wiki/Keep_it_simple_stupidhttp://picocontainer.org/patterns.htmlhttp://en.wikipedia.org/wiki/Separation_of_concernshttp://en.wikipedia.org/wiki/Single_responsibility_principlehttp://en.wikipedia.org/wiki/Information_hidinghttp://en.wikipedia.org/wiki/Liskov_substitution_principlehttp://en.wikipedia.org/wiki/Dependency_inversion_principlehttp://stackoverflow.com/questions/6766056/dip-vs-di-vs-iochttp://en.wikipedia.org/wiki/Open/closed_principlehttp://en.wikipedia.org/wiki/Law_Of_Demeterhttp://en.wikipedia.org/wiki/Don't_repeat_yourselfhttp://en.wikipedia.org/wiki/You_ain't_gonna_need_ithttp://en.wikipedia.org/wiki/Component-oriented_programminghttp://en.wikipedia.org/wiki/Service-oriented_architecturehttp://www.martinfowler.com/articles/injection.htmlhttp://www.codeproject.com/KB/aspnet/IOCDI.aspxhttp://msdn.microsoft.com/en-us/magazine/cc163739.aspxhttp://msdn.microsoft.com/en-us/library/ff650320.aspxhttp://msdn.microsoft.com/en-us/library/aa973811.aspxhttp://msdn.microsoft.com/en-us/library/ff647976.aspxhttp://msdn.microsoft.com/en-us/library/cc707845.aspxhttp://unity.codeplex.com/http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11

Page 57: Clean Code I - Best Practices

… more ReferencesResharperhttp://www.jetbrains.com/resharper/

FxCop / Code Analysis http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspxhttp://blogs.msdn.com/b/codeanalysis/http://www.binarycoder.net/fxcop/index.html

Code Contractshttp://msdn.microsoft.com/en-us/devlabs/dd491992http://research.microsoft.com/en-us/projects/contracts/

StyleCophttp://stylecop.codeplex.com/

Ghostdoc http://submain.com/products/ghostdoc.aspx

Spellcheckerhttp://visualstudiogallery.msdn.microsoft.com/7c8341f1-ebac-40c8-92c2-476db8d523ce//

Lego (trademarked in capitals as LEGO)

Page 58: Clean Code I - Best Practices

Blog, Rating, Slides

http://www.DesignItRight.net

www.speakerrate.com/theoj

www.slideshare.net/theojungeblut

Page 59: Clean Code I - Best Practices

Time to say Thank You!

The Organizers

The

volu

ntee

rs

(how

abo

ut y

ou?)

The

Spon

sors

Page 60: Clean Code I - Best Practices

… thanks for you attention!

Please fill out the feedback, and…

www.speakerrate.com/theoj

And visit and supportyour local user groups!