Clean Code and Common Engineering Practices

15
CLEAN CODE AND ENGINEERING PRACTICES Ruth Sperer – Tech Lead and EP Champion AT&T Israel

Transcript of Clean Code and Common Engineering Practices

Page 1: Clean Code and Common Engineering Practices

CLEAN CODE AND ENGINEERING PRACTICESRuth Sperer – Tech Lead and EP ChampionAT&T Israel

Page 2: Clean Code and Common Engineering Practices

OVERVIEW▪ Clean code principles

▪ Code Review

▪ TDD

▪ Refactoring

▪ SCM, SCA, CI and other abbreviations

▪ Being a good programmer

Page 3: Clean Code and Common Engineering Practices

WHAT DO YOU CONSIDER CLEAN CODE?▪ Readable

▪ Error free

▪ Elegant

▪ Testable

▪ Efficient

Page 4: Clean Code and Common Engineering Practices

MEANINGFUL NAMES▪ For class, method or variable, name should suggest

▪ Why is exists

▪ What it does

▪ How it is used

▪ Don’t include type info▪ Why?

▪ Be honest▪ No side effects

▪ So what about comments?

Page 5: Clean Code and Common Engineering Practices

WHAT MAKES A GOOD FUNCTION?▪ Short

▪ Efficient

▪ Does one thing

▪ Reusable

▪ Few variables

▪ No side effects

Page 6: Clean Code and Common Engineering Practices

CODE REVIEW - MOTIVATIONDefinition: The analysis of source code in order to find defects

▪Early bug detection

▪Knowledge sharing

▪Code improvements

▪Social communication

▪Understanding

* Expectations vs. Actual

Page 7: Clean Code and Common Engineering Practices

CODE REVIEW – PRACTICAL GUIDE▪ Let the tools work for you:

▪ Formatter

▪ Lint

▪ Find problems, not solutions

▪ As a reviewer – be nice

▪ As a reviewed – leave you ego in the drawer

▪ Don’t skip it (no valid excuses)

Page 8: Clean Code and Common Engineering Practices

TDD

Page 9: Clean Code and Common Engineering Practices

REFACTORING▪ Why ?

▪ Cost of owning a mess*:

▪ Don’t make it to “The grand re-design”*

▪ Code that smells▪ Duplicate code▪ Longs methods▪ Deep nesting▪ Too many variables in a method

* Rober C Marting – “Clean Code..”

Page 10: Clean Code and Common Engineering Practices

REFACTORING TIPS▪ Valid changes – let your IDE work for you

▪ Renaming

▪ Extract variable

▪ Extract method

▪ Move

▪ Introduce parameter

▪ Unit tests coverage is your friend

Page 11: Clean Code and Common Engineering Practices

SCM – SOFTWARE CONFIGURATION MANAGEMENT▪ Track and control changes in software

▪ Confidence in change - Can always go back

▪ When to start using it?

▪ Git – free and open source

▪ Github – host

▪ Other free/commercial tools – CVS, SVN, ClearCase

Page 12: Clean Code and Common Engineering Practices

SCA – STATIC CODE ANALYSIS▪ Analyzing code without executing it

▪ Common Errors detectible by SCA▪ Buffers and arrays underflows \ overflows

▪ Resource leaks

▪ Potential null references

▪ Lack of initialization

▪ Common tools: Sonar, FindBugs, Checkstyle

Page 13: Clean Code and Common Engineering Practices

CI – CONTINUOUS INTEGRATION▪ is the practice of merging all developer working copies with a

shared mainline several times a day

▪ Considered an XP (extreme programming) habit

▪ Possibly dozens of “commits” every day

▪ Prevent “Integration Hell”

▪ Automated unit tests in build server

▪ Next step: CD – Continuous Delivery

Page 14: Clean Code and Common Engineering Practices

BEING A GOOD PROGRAMMER▪ Respect your ancestors

▪ The Netscape rewrite story

▪ Acknowledge your successors▪ Meaningful names, Comments

▪ Avoid code duplications

▪ “Leave the camp ground cleaner than you found it”

Page 15: Clean Code and Common Engineering Practices

RECOMMENDED READINGS▪ "Clean Code: A Handbook of Agile Software Craftsmanship“ by Robert

C. Martins

▪ “The Pragmatic Programmer” by Dave Thomas and Andy Hunt

▪ Effective <fill in your language>