Clean Code By Example
Published by O'Reilly Media, Inc.
Mastering the 4 pillars of clean code: naming, formatting, logic, and unit tests
As a writer of code, the compiler isn't your only audience. Your coworkers—and even your future self, when you fix bugs—must be able to easily understand your original intent. By getting into the practice of writing clean code from the start, you'll also save yourself time and build in a more efficient maintenance process down the line.
Join expert M. Scott Ford to learn why it's important to write code that's easily understood by both computers and humans, how to recognize cleaner code, how to write cleaner code in practice, and which refactoring tools you can use. You’ll see how to ensure that different aspects of your code are clearly and thoughtfully constructed, including names, formatting, and logic, discover what a "clean" unit test looks like, and learn how to ensure that your tests are fast and efficient.
What you’ll learn and how you can apply it
By the end of this live online course, you’ll understand:
- How to practice writing clean code
- What a clean unit test looks like
- How to use clean names, clean formatting, and clean logic as part of writing better code
And you’ll be able to:
- Recognize cleaner code
- Write cleaner code
- Refactor messy code to clean code
This live event is for you because...
- You’re a software developer who wants to write better code.
- You work with a large, unwieldy code base.
- You want to develop a more critical eye when reviewing code.
Prerequisites
- A computer with a programming text editor installed (e.g., Microsoft Visual Studio Code)
- Basic programming skills
Recommended preparation:
- Take Fundamentals of Software Engineering in 3 Weeks (live online course with Nathaniel Schutta)
Recommended follow-up:
- Take Technical Debt First Steps (live online course with Chelsea Troy)
- Take Hands-On Refactoring (live online course with Venkat Subramaniam)
- Read The Art of Readable Code (book)
- Read Building Maintainable Software, Java edition (book)
- Read Building Maintainable Software, C# edition (book)
- Read Beautiful Code (book)
Schedule
The time frames are only estimates and may vary according to how the class is progressing.
Introduction (30 minutes)
- Presentation: Write code for humans, not machines; What does it mean to “work with code”?; clean code definition; preview of topics the course covers
- Group discussion: Assess your experience working with code; assess a block of code
- Q&A
- Break
Clean names (50 minutes)
- Presentation: Why clean names are important; prefer clarity over brevity; acronyms and abbreviations; class and type names; method and function names; variable names; parameter names; constant names
- Group discussion: When have you been frustrated by a name that was unclear?
- Hands-on exercise: Create list of all of the acronyms and abbreviations commonly used on your projects and calculate the percentage of these that everyone is likely to understand
- Group discussion: Share the results of the exercise
- Hands-on exercise: Identify the class and type names that violate at least one rule
- Group discussion: When might it make sense to break the method and function naming rules?; How often do you change rule-breaking variable names?
- Hands-on exercise: Fix parameter names on your own
- Group discussion: Why is it worth having different rules for variable, parameter, and constant names?
- Q&A
- Break
Clean formatting (35 minutes)
- Presentation: Why clean formatting matters; indentation and bracket placement; line wrapping; white space
- Group discussion: How much do you value consistent formatting when reading code you didn’t write?; What maximum line length do you prefer and why?
- Hands-on exercise: Untangle this code
- Q&A
- Break
Clean logic (35 minutes)
- Presentation: Magic numbers and constants; parameter lists; predicate methods; making good use of loops
- Group discussion: How often do you replace magic numbers with named constants?; How many items is too many in a parameter list?
- Hands-on exercise: Convert if statements to use predicate methods
- Q&A
- Break
Clean unit tests (25 minutes)
- Presentation: What's a "unit" test?; keep your tests fast; a single assertion per test; keep your tests isolated
- Group discussion: How do you define a “unit” on your project?; How long does your test suite take to run?; Will your test suite run in any order?
- Hands-on exercise: Refactor this test method so that there is only a single assertion
Wrap-up and Q&A (5 minutes)
Your Instructor
Scott Ford
Scott Ford is the cofounder and chief code whisperer (CTO) of Corgibytes, a software consultancy dedicated to modernizing existing code bases. He’s a true mender at heart and a polyglot developer who’s fluent in 25 programming languages. His love of software restoration and remodeling began in college, where he and his team were responsible for retrofitting the testing tools for the X-31 jet fighter under hefty constraints. Where others found frustration, Scott found joy. In 2009 he teamed up with a friend from high school to build a business focused exclusively on software modernization and working with legacy code. He enjoys sharing his philosophy of maintenance and has been featured in books such as The Innovation Delusion. He has regularly been a guest lecturer on continuous delivery practices at Harvard University and is the author of three courses on LinkedIn Learning: Dealing With Legacy Code And Technical Debt, Code Quality, and Clean Coding Practices. Scott is the host of the podcast Legacy Code Rocks and enjoys helping other menders find a sense of belonging in a world dominated by makers. In addition to fixing old code, he’s an avid reader of science fiction and enjoys spending time with his family and kids.