clean code: tips for writing clear and concise code

30
Clean Code: Tips for Writing Clear and Concise Code Ben Rand Director of IT, Senior Developer Job Industrial Services, Inc.

Upload: others

Post on 23-Mar-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Clean Code: Tips for Writing Clear and Concise Code

Clean Code: Tips for Writing Clear and Concise CodeBen Rand

Director of IT, Senior Developer

Job Industrial Services, Inc.

Page 2: Clean Code: Tips for Writing Clear and Concise Code

About the speaker

Ben Rand

Ben has been using AutoCAD since Release 12. He learned to program

using LISP in AutoCAD, worked his way up through VBA, VB6 and

VB.NET, and now spends most of his days programming in C#

(occasionally still in AutoCAD!), and dabbling in other languages such as

Java, Ruby and Python. He has worked in the Industrial Engineering

field for more than 17 years as a CAD Manager, developer and IT

Director. In 2013, he was the 2013 Top DAUG overall winner at AU, and

he served a mentor for the AutoCAD Mentor All-Star team.

Ben is the proud father of four children and enjoys reading and playing

a variety of sports including pickleball, volleyball, and tennis. In 2018,

Ben was a member of two USTA men's league teams. The 18 and over

team won the National Championship, while the 40 and over team was

the Finalist.

Page 3: Clean Code: Tips for Writing Clear and Concise Code

Course Objectives

Name classes, methods

and variables meaningfully

NMNAME MEANINGFULLY

CFCOHESIVE FOCUSED

UTUNIT TESTS

CCCLEAN CODE

Write more cohesive,

focused methods and

classes

Utilize unit tests to support

refactoring towards clean

code

Write cleaner code

Page 4: Clean Code: Tips for Writing Clear and Concise Code

Confession: I have written bad code

• Professionally

• In many languages:

• LISP

• VBA/VB6

• VB.NET

• C#

• Java

• Ruby

• Javascript

• I still write bad code, but I’m getting better at:

• Recognizing it when I do

• Doing something about it

http://beamiller.wikia.com/wiki/Confessions_of_a_Shopaholic

Page 5: Clean Code: Tips for Writing Clear and Concise Code

I'm not a great programmer; I'm just a good programmer with

great habits. ― Kent Beck

https://matrix.fandom.com/wiki/File:Matrix_Code.gif

Page 6: Clean Code: Tips for Writing Clear and Concise Code

Why is Bad Code…bad?

• Bad code works BUT

• Is hard to read

• Is hard to reason about

• Tries to do too much

• Uses confusing names

• Relies on magic numbers/strings

• Is hard to maintain

• Is poorly formatted

• Lacks unit tests

You have to work extra hard to work with bad code—over and over again.

Page 7: Clean Code: Tips for Writing Clear and Concise Code

Why is Clean Code better?

• Clean code works AND

• Is easy to read

• Is easy to reason about

• Is easy to extend

• Is easy to understand, including several

weeks or months from now

• Is easy to maintain

• Has unit testshttps://blog.linkedin.com/2012/08/01/inday-games

Page 8: Clean Code: Tips for Writing Clear and Concise Code

The computing scientist’s main challenge is not to get confused by the complexities of his own

making.― E. W. Dijkstra

https://matrix.fandom.com/wiki/File:Matrix_Code.gif

Page 9: Clean Code: Tips for Writing Clear and Concise Code

Formatting• Indentation

• Samples 1-2

• Samples 3-4

• Magic numbers and strings

• Samples 5-10

• Spacing and line length

• Samples 12-13

Page 10: Clean Code: Tips for Writing Clear and Concise Code

Comments are, at best, a necessary evil.

― Robert C. Martin

https://matrix.fandom.com/wiki/File:Matrix_Code.gif

Page 11: Clean Code: Tips for Writing Clear and Concise Code

Comments• Need to be maintained

• Samples 6, 15

• Explain sections of code (refactor and rename?)

• Samples 16-18

• Versioning information

• Slide 12

• Zombie code

• TODO and Regions

• Slide 13

Page 12: Clean Code: Tips for Writing Clear and Concise Code

//Modified by Ben

Prefer Version Control System (VCS) to comments

Page 13: Clean Code: Tips for Writing Clear and Concise Code

TODO comments often become clutter

Page 14: Clean Code: Tips for Writing Clear and Concise Code

Deleted code is debugged code.― Jeff Sickel

https://matrix.fandom.com/wiki/File:Matrix_Code.gif

Page 15: Clean Code: Tips for Writing Clear and Concise Code

Functions• Short and do one thing

• Declare variables close to where used

• Avoid nested conditionals

• Slide 16

• Use verb-y names: Getxxx, Setxxx, Isxxx, Hasxxx

• Command or Query (not both)

• Samples 20-21

• Decomplicating Conditionals

• Slide 18-19

• Samples 23-24

Page 16: Clean Code: Tips for Writing Clear and Concise Code

• 25 declared variables

• Uncle Bob’s

recommendation for

method length?

• 20 lines

Page 17: Clean Code: Tips for Writing Clear and Concise Code

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough

to debug it.― Brian W. Kernighan

https://matrix.fandom.com/wiki/File:Matrix_Code.gif

Page 18: Clean Code: Tips for Writing Clear and Concise Code

bool someCondition;

if(someCondition == true)

{

//do something

}

Conditionals

if(someCondition)

if(someCondition == false) if(!someCondition)

if(someCondition != false) if(!someCondition == false)

Page 19: Clean Code: Tips for Writing Clear and Concise Code

It’s sort of a double negative.― Donald J. Trump

https://matrix.fandom.com/wiki/File:Matrix_Code.gif

Page 20: Clean Code: Tips for Writing Clear and Concise Code

Naming—Variables, Methods, Classes• Reveal intent

• Use long descriptive names instead of short cryptic names

• Use nouns for classes

• Use verb/verb phrases for methods

• Avoid single letter names

• Be pronounceable

• Sample 25

• Avoid “Hungarian notation” prefixes

• Sample 26

• Avoid disinformation

• Sample 28

Page 21: Clean Code: Tips for Writing Clear and Concise Code

One of my most productive days was throwing away 1000

lines of code.― Ken Thompson

https://matrix.fandom.com/wiki/File:Matrix_Code.gif

Page 22: Clean Code: Tips for Writing Clear and Concise Code

Classes• Should be short

• Can I describe its purpose in 25 words or less, without using: if,

and, or, but?

• Use nouns for names

• SOLID: https://en.wikipedia.org/wiki/SOLID

• Single responsibility principle (SRP)—single reason to change

• Open/closed principle

• Liskov substitution principle

• Interface segregation principle

• Dependency inversion principle

Page 23: Clean Code: Tips for Writing Clear and Concise Code

Unit Tests• Keep tests clean—readability!

• One assertion per test

• Single concept per test

• Organization: Arrange, Act, Assert

• F.I.R.S.T.:

• Fast

• Independent

• Repeatable

• Self-validating

• Thorough/Timely

Page 24: Clean Code: Tips for Writing Clear and Concise Code

Unit Tests – Test Driven Design (TDD)• Three Laws of TDD

1. You may not write production code until you have written a failing unit test.

2. You may not write more of a unit test than is sufficient to fail, and not

compiling is failing.

3. You may not write more production code than is sufficient to pass the

currently failing test.

--From Clean Code: A Handbook of Agile Software Craftsmanship, Robert C. Martin

See: https://blog.coryfoy.com/2006/08/tdd-bowling-game-part-1/ and

https://blog.coryfoy.com/2006/08/tdd-bowling-game-part-2/ for a tutorial on

TDD

Page 25: Clean Code: Tips for Writing Clear and Concise Code

Refactoring and Unit Tests: Credit Card Validator

Sample Code: Practice2_CreditCardProblem (Java)

Page 26: Clean Code: Tips for Writing Clear and Concise Code

SRP and Abstractions:CleanCodeLayer

Sample Code: CleanCodeLayer (C#)

Page 27: Clean Code: Tips for Writing Clear and Concise Code

Always code as if the guy who ends up maintaining your code

will be a violent psychopath who knows where you live.

--Martin Golding

https://matrix.fandom.com/wiki/File:Matrix_Code.gif

Page 28: Clean Code: Tips for Writing Clear and Concise Code

Recommended Reading

• Clean Code: A Handbook of Agile Software Craftsmanship, Robert C. Martin

• Working Effectively with Legacy Code, Michael C. Feathers

• Dependency Injection in .NET, Mark Seeman

• Design Patterns: Elements of Reusable Object-Oriented Software, Erich

Gamma, et al.

• The Pragmatic Programmer: From Journeyman to Master, Andy Hunt

• Refactoring: Improving the Design of Existing Code, Martin Fowler

• Code Complete, Steve McConnell

• Head First Design Patterns, Eric Freeman

• The Art of Unit Testing: With Examples in .NET, Roy Osherove

Page 29: Clean Code: Tips for Writing Clear and Concise Code

Contact Me• Email: [email protected]

• Twitter: @leadensky

• LinkedIn: www.linkedin.com/in/leadensky

2018 Courses at AU• AS197578-L: Data Mining in AutoCAD with Data Extraction

• SD195561: Control Your Code: Introduction to Source Control for Programmers and

CAD Managers

• SD226637: Clean Code: Tips for Writing Clear and Concise Code

Pluralsight Course• https://www.pluralsight.com/courses/autocad-extracting-data-drawings

Page 30: Clean Code: Tips for Writing Clear and Concise Code

Autodesk and the Autodesk logo are registered trademarks or trademarks of Autodesk, Inc., and/or its subsidiaries and/or affiliates in the USA and/or other countries. All other brand names, product names, or trademarks belong to their respective holders. Autodesk reserves the right to alter product and services offerings, and specifications and pricing at any time without notice, and is not responsible for typographical or graphical errors that may appear in this document.

© 2018 Autodesk. All rights reserved.

Don’t forget to fill out your class survey

before you leave AU!