Transcript
Page 1: Clean Code Part III - Craftsmanship at SoCal Code Camp

Clean Code IIISoftware

CraftsmanshipSan Diego, June 24rd 2012

SolCalCodeCamp

Page 2: Clean Code Part III - Craftsmanship at SoCal Code Camp

Theo Jungeblut• Senior Software Developer at

AppDynamics in San Francisco

• architects decoupled solutions tailored to business needs and crafts maintainable code to last

• worked in healthcare and factory automation, building mission critical applications, framework & platforms for 8+ years

• degree in Software Engineeringand Network Communications

• enjoys cycling, running and [email protected]

www.designitright.net www.speakerrate.com/theoj

Page 3: Clean Code Part III - Craftsmanship at SoCal Code Camp

Overview• Why Clean Code?• The Power of Simplicity• Tools - Your Best Friend• From Names to Classes• The "Must Read"-Books• Summary• Q&A

Page 4: Clean Code Part III - Craftsmanship at SoCal Code Camp

Does writing Clean Code make us more efficient?

Page 5: Clean Code Part III - Craftsmanship at SoCal Code Camp
Page 6: Clean Code Part III - Craftsmanship at SoCal Code Camp

What is Clean Code?

Page 7: Clean Code Part III - Craftsmanship at SoCal Code Camp

Clean Code is maintainable

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

Page 8: Clean Code Part III - Craftsmanship at SoCal Code Camp

Software Engineering

vs. Craftsmanship

Page 9: Clean Code Part III - Craftsmanship at SoCal Code Camp

Software Engineering

ANDCraftsmanship

Page 10: Clean Code Part III - Craftsmanship at SoCal Code Camp

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 11: Clean Code Part III - Craftsmanship at SoCal Code Camp

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 12: Clean Code Part III - Craftsmanship at SoCal Code Camp

Keep it simple, stupid(KISS)

Page 13: Clean Code Part III - Craftsmanship at SoCal Code Camp

KISS-Principle – “Keep It Simple Stupid”

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

by Kelly Johnson

Page 14: Clean Code Part III - Craftsmanship at SoCal Code Camp

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 15: Clean Code Part III - Craftsmanship at SoCal Code Camp

Graphic by Nathan Sawaya courtesy of brickartist.com

Page 16: Clean Code Part III - Craftsmanship at SoCal Code Camp

Source Code Conventions

Page 17: Clean Code Part III - Craftsmanship at SoCal Code Camp

.NET Tools and their ImpactTool name Positive Impact Negative Impact

Resharper compiling ++++ VS responsiveness --

FxCop code quality ++ compiling time -

StyleCop code consistency +++ compiling time -

StyleCop plugin for Resharper

compiling time +++ VS responsiveness --

Ghost Doc automated docs potentially worse doc

Spell Checker fewer spelling errors ++ performance --

Code Contracts testability, quality ++ compiling time --

Pex & Moles automated test ++ compiling time --

Page 18: Clean Code Part III - Craftsmanship at SoCal Code Camp

Resharper

Features:– Code Analysis – Quick Fixes– Code Templates – Code Generation– Code Cleanup– Many, many more…

“The single most impacting development addition to Visual Studio”

http://www.jetbrains.com/resharper/

Page 19: Clean Code Part III - Craftsmanship at SoCal Code Camp

FxCop / Static Code AnalysisCode Analysis:– Correctness– Library design– Internationalization and localization– Naming conventions– Performance– Security

http://msdn.microsoft.com/en-us/library/3z0aeatx.aspx

Page 20: Clean Code Part III - Craftsmanship at SoCal Code Camp

Style Cop with R# IntegrationCode Consistency & Readability:– Automated check of C# coding

standard– Enforceable at check-in with TFS

check-in Policy

– Full Integration in Resharper with Style Cop plugin:

– Code Analysis – Quick Fixes– Code Cleanup

Page 21: Clean Code Part III - Craftsmanship at SoCal Code Camp

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

• Save keystrokes and time• Simplify documenting your code• Benefit of the base class documentation

Ghost Doc

Page 22: Clean Code Part III - Craftsmanship at SoCal Code Camp

Spell Checker

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

• Spelll chicking for literals and comments in VS

Page 23: Clean Code Part III - Craftsmanship at SoCal Code Camp

http://msdn.microsoft.com/en-us/devlabs/dd491992

• Design-by-Contract programming• Improved testability• Static verification• API documentation integration with

Sandcastle

Page 24: Clean Code Part III - Craftsmanship at SoCal Code Camp

• Pex automatically generates test suites with high code coverage.

• Moles allows to replace any .NET method with a delegate.

Microsoft Pex & Moles

http://research.microsoft.com/en-us/projects/pex/

Page 25: Clean Code Part III - Craftsmanship at SoCal Code Camp

Names Matter• Meaningful Names• Intention Revealing Names• Use Pronounceable Names• Use Searchable Names• Avoid Encoding (Hungarian)• Don’t be cute• Pick One Word per Concept• Use Problem Domain Names

* Chapter extract: Robert C. Martin –” Clean Code”, Parson Education, Inc. 2008

Page 26: Clean Code Part III - Craftsmanship at SoCal Code Camp

Functions

• Small – Do One Thing• One Level of Abstraction• No or only few Arguments• Have No Side Effects• Prefer Exceptions to

Returning Error Codes• Don’t Repeat Yourself

* Chapter extract: Robert C. Martin –” Clean Code”, Parson Education, Inc. 2008

Page 27: Clean Code Part III - Craftsmanship at SoCal Code Camp

Comments

• Comments o not Make Up for Bad Code• Explain Yourself in Code• Clarification• Warning of Consequences• ToDo Comments• Javadocs in Public APIs

* Chapter extract: Robert C. Martin –” Clean Code”, Parson Education, Inc. 2008

Page 28: Clean Code Part III - Craftsmanship at SoCal Code Camp

Bad Comments• Mumblings• Redundant Comments• Misleading Comments• Journal Comments• Noise Comments• Don’t use a Comment When you Use a

Method or a Variable• Commented-Out Code

* Chapter extract: Robert C. Martin –” Clean Code”, Parson Education, Inc. 2008

Page 29: Clean Code Part III - Craftsmanship at SoCal Code Camp

The Purpose of Formatting• Team Rules – Consistency is King• Vertical Openness Between Concepts• Vertical Distance• Horizontal Alignment• Indentation• Write Journey Style Code

Resharper & StyleCop – “Code Cleanup”

* Kind of Chapter extract: Robert C. Martin –” Clean Code”, Parson Education, Inc. 2008

Page 30: Clean Code Part III - Craftsmanship at SoCal Code Camp

Classes• Class Organization • Encapsulation• Classes Should be Small• The Single Responsibility Principle• Cohesion• Organize for Change• Insolating from Change

* Kind of Chapter extract: Robert C. Martin –” Clean Code”, Parson Education, Inc. 2008

Page 31: Clean Code Part III - Craftsmanship at SoCal Code Camp

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 32: Clean Code Part III - Craftsmanship at SoCal Code Camp

The “Must Read”-Book(s)by Krzysztof Cwalina, Brad Abrams

Framework Design Guidelines

“teaches developers the best practices for designing reusable libraries for the Microsoft .NET Framework.”

Page 33: Clean Code Part III - Craftsmanship at SoCal Code Camp

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 34: Clean Code Part III - Craftsmanship at SoCal Code Camp

Downloads, Feedback & Comments:

Q & A

Graphic by Nathan Sawaya courtesy of brickartist.com

[email protected] www.speakerrate.com/theoj

Page 35: Clean Code Part III - Craftsmanship at SoCal Code Camp

References… http://clean-code-developer.comhttp://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://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://msdn.microsoft.com/en-us/library/bb833022.aspxhttp://unity.codeplex.com/http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11

Page 36: Clean Code Part III - Craftsmanship at SoCal Code Camp

… 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/

Pex & Molehttp://research.microsoft.com/en-us/projects/pex/

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 37: Clean Code Part III - Craftsmanship at SoCal Code Camp

… thanks for you attention!

And visit and support the

www.sandiegodotnet.com

Please fill out the feedback, and…

www.speakerrate.com/theoj


Top Related