Practical techniques for writing code that is robust, reliable, and easy for team members to understand and adapt.
In Good Code, Bad Code you’ll learn how to:
• Think about code like an effective software engineer
• Write functions that read like well-structured sentences
• Ensure code is reliable and bug free
• Effectively unit test code
• Identify code that can cause problems and improve it
• Write code that is reusable and adaptable to new requirements
• Improve your medium and long-term productivity
• Save yourself and your team time
The difference between good code or bad code often comes down to how you apply the established practices of the software development community. In Good Code, Bad Code you’ll learn how to boost your productivity and effectiveness with code development insights normally only learned through careful mentorship and hundreds of code reviews.
about the technology
Software development is a team sport. For an application to succeed, your code needs to be robust and easy for others to understand, maintain, and adapt. Whether you’re working on an enterprise team, contributing to an open source project, or bootstrapping a startup, it pays to know the difference between good code and bad code.
about the book
Good Code, Bad Code is a clear, practical introduction to writing code that’s a snap to read, apply, and remember. With dozens of instantly-useful techniques, you’ll find coding insights that normally take years of experience to master. In this fast-paced guide, Google software engineer Tom Long teaches you a host of rules to apply, along with advice on when to break them!
What's inside
• Write functions that read like sentences
• Ensure your code stays bug-free
• How to sniff out bad code
• Save time for yourself and your team
About the reader
For coders early in their careers who are familiar with an object-oriented language, such as Java or C#.
About the author
Tom Long is a software engineer at Google where he works as a tech lead. Among other tasks, he regularly mentors new software engineers in professional coding best practices.
Author(s): Tom Long
Edition: 1
Publisher: Manning Publications
Year: 2021
Language: English
Commentary: Vector PDF
Pages: 376
City: Shelter Island, NY
Tags: Software Engineering; Best Practices; Unit Testing; Error Handling; Comments; Abstraction; Code Readability; Code Reusability; Robustness; Code Quality; Code Contracts
Good Code, Bad Code
contents
preface
acknowledgments
about this book
Who should read this book
How this book is organized: A roadmap
About the code
liveBook discussion forum
How to use the advice in this book
Further reading
about the author
about the cover illustration
Part 1 In theory
1 Code quality
1.1 How code becomes software
1.2 The goals of code quality
1.2.1 Code should work
1.2.2 Code should keep working
1.2.3 Code should be adaptable to changing requirements
1.2.4 Code should not reinvent the wheel
1.3 The pillars of code quality
1.3.1 Make code readable
1.3.2 Avoid surprises
1.3.3 Make code hard to misuse
1.3.4 Make code modular
1.3.5 Make code reusable and generalizable
1.3.6 Make code testable and test it properly
1.4 Does writing high-quality code slow us down?
Summary
2 Layers of abstraction
2.1 Nulls and the pseudocode convention in this book
2.2 Why create layers of abstraction?
2.2.1 Layers of abstraction and the pillars of code quality
2.3 Layers of code
2.3.1 APIs and implementation details
2.3.2 Functions
2.3.3 Classes
2.3.4 Interfaces
2.3.5 When layers get too thin
2.4 What about microservices?
Summary
3 Other engineers and code contracts
3.1 Your code and other engineers’ code
3.1.1 Things that are obvious to you are not obvious to others
3.1.2 Other engineers will inadvertently try to break your code
3.1.3 In time, you will forget about your own code
3.2 How will others figure out how to use your code?
3.2.1 Looking at the names of things
3.2.2 Looking at the data types of things
3.2.3 Reading documentation
3.2.4 Asking you in person
3.2.5 Looking at your code
3.3 Code contracts
3.3.1 Small print in contracts
3.3.2 Don’t rely too much on small print
3.4 Checks and assertions
3.4.1 Checks
3.4.2 Assertions
Summary
4 Errors
4.1 Recoverability
4.1.1 Errors that can be recovered from
4.1.2 Errors that cannot be recovered from
4.1.3 Often only the caller knows if an error can be recovered from
4.1.4 Make callers aware of errors they might want to recover from
4.2 Robustness vs. failure
4.2.1 Fail fast
4.2.2 Fail loudly
4.2.3 Scope of recoverability
4.2.4 Don’t hide errors
4.3 Ways of signaling errors
4.3.1 Recap: Exceptions
4.3.2 Explicit: Checked exceptions
4.3.3 Implicit: Unchecked exceptions
4.3.4 Explicit: Nullable return type
4.3.5 Explicit: Result return type
4.3.6 Explicit: Outcome return type
4.3.7 Implicit: Promise or future
4.3.8 Implicit: Returning a magic value
4.4 Signaling errors that can’t be recovered from
4.5 Signaling errors that a caller might want to recover from
4.5.1 Arguments for using unchecked exceptions
4.5.2 Arguments for using explicit techniques
4.5.3 My opinion: Use an explicit technique
4.6 Don’t ignore compiler warnings
Summary
Part 2 In practice
5 Make code readable
5.1 Use descriptive names
5.1.1 Nondescriptive names make code hard to read
5.1.2 Comments are a poor substitute for descriptive names
5.1.3 Solution: Make names descriptive
5.2 Use comments appropriately
5.2.1 Redundant comments can be harmful
5.2.2 Comments are a poor substitute for readable code
5.2.3 Comments can be great for explaining why code exists
5.2.4 Comments can provide useful high-level summaries
5.3 Don’t fixate on number of lines of code
5.3.1 Avoid succinct but unreadable code
5.3.2 Solution: Make code readable, even if it requires more lines
5.4 Stick to a consistent coding style
5.4.1 An inconsistent coding style can cause confusion
5.4.2 Solution: Adopt and follow a style guide
5.5 Avoid deeply nesting code
5.5.1 Deeply nested code can be hard to read
5.5.2 Solution: Restructure to minimize nesting
5.5.3 Nesting is often a result of doing too much
5.5.4 Solution: Break code into smaller functions
5.6 Make function calls readable
5.6.1 Arguments can be hard to decipher
5.6.2 Solution: Use named arguments
5.6.3 Solution: Use descriptive types
5.6.4 Sometimes there’s no great solution
5.6.5 What about the IDE?
5.7 Avoid using unexplained values
5.7.1 Unexplained values can be confusing
5.7.2 Solution: Use a well-named constant
5.7.3 Solution: Use a well-named function
5.8 Use anonymous functions appropriately
5.8.1 Anonymous functions can be great for small things
5.8.2 Anonymous functions can be hard to read
5.8.3 Solution: Use named functions instead
5.8.4 Large anonymous functions can be problematic
5.8.5 Solution: Break large anonymous functions into named functions
5.9 Use shiny, new language features appropriately
5.9.1 New features can improve code
5.9.2 Obscure features can be confusing
5.9.3 Use the best tool for the job
Summary
6 Avoid surprises
6.1 Avoid returning magic values
6.1.1 Magic values can lead to bugs
6.1.2 Solution: Return null, an optional, or an error
6.1.3 Sometimes magic values can happen accidentally
6.2 Use the null object pattern appropriately
6.2.1 Returning an empty collection can improve code
6.2.2 Returning an empty string can sometimes be problematic
6.2.3 More complicated null objects can cause surprises
6.2.4 A null object implementation can cause surprises
6.3 Avoid causing unexpected side effects
6.3.1 Side effects that are obvious and intentional are fine
6.3.2 Unexpected side effects can be problematic
6.3.3 Solution: Avoid a side effect or make it obvious
6.4 Beware of mutating input parameters
6.4.1 Mutating an input parameter can lead to bugs
6.4.2 Solution: Copy things before mutating them
6.5 Avoid writing misleading functions
6.5.1 Doing nothing when a critical input is missing can cause surprises
6.5.2 Solution: Make critical inputs required
6.6 Future-proof enum handling
6.6.1 Implicitly handling future enum values can be problematic
6.6.2 Solution: Use an exhaustive switch statement
6.6.3 Beware of the default case
6.6.4 Caveat: Relying on another project’s enum
6.7 Can’t we just solve all this with testing?
Summary
7 Make code hard to misuse
7.1 Consider making things immutable
7.1.1 Mutable classes can be easy to misuse
7.1.2 Solution: Set values only at construction time
7.1.3 Solution: Use a design pattern for immutability
7.2 Consider making things deeply immutable
7.2.1 Deep mutability can lead to misuse
7.2.2 Solution: Defensively copy things
7.2.3 Solution: Use immutable data structures
7.3 Avoid overly general data types
7.3.1 Overly general types can be misused
7.3.2 Pair types are easy to misuse
7.3.3 Solution: Use a dedicated type
7.4 Dealing with time
7.4.1 Representing time with integers can be problematic
7.4.2 Solution: Use appropriate data structures for time
7.5 Have single sources of truth for data
7.5.1 Second sources of truth can lead to invalid states
7.5.2 Solution: Use primary data as the single source of truth
7.6 Have single sources of truth for logic
7.6.1 Multiple sources of truth for logic can lead to bugs
7.6.2 Solution: Have a single source of truth
Summary
8 Make code modular
8.1 Consider using dependency injection
8.1.1 Hard-coded dependencies can be problematic
8.1.2 Solution: Use dependency injection
8.1.3 Design code with dependency injection in mind
8.2 Prefer depending on interfaces
8.2.1 Depending on concrete implementations limits adaptability
8.2.2 Solution: Depend on interfaces where possible
8.3 Beware of class inheritance
8.3.1 Class inheritance can be problematic
8.3.2 Solution: Use composition
8.3.3 What about genuine is-a relationships?
8.4 Classes should care about themselves
8.4.1 Caring too much about other classes can be problematic
8.4.2 Solution: Make classes care about themselves
8.5 Encapsulate related data together
8.5.1 Unencapsulated data can be difficult to handle
8.5.2 Solution: Group related data into objects or classes
8.6 Beware of leaking implementation details in return types
8.6.1 Leaking implementation details in a return type can be problematic
8.6.2 Solution: Return a type appropriate to the layer of abstraction
8.7 Beware of leaking implementation details in exceptions
8.7.1 Leaking implementation details in exceptions can be problematic
8.7.2 Solution: Make exceptions appropriate to the layer of abstraction
Summary
9 Make code reusable and generalizable
9.1 Beware of assumptions
9.1.1 Assumptions can lead to bugs when code is reused
9.1.2 Solution: Avoid unnecessary assumptions
9.1.3 Solution: If an assumption is necessary, enforce it
9.2 Beware of global state
9.2.1 Global state can make reuse unsafe
9.2.2 Solution: Dependency-inject shared state
9.3 Use default return values appropriately
9.3.1 Default return values in low-level code can harm reusability
9.3.2 Solution: Provide defaults in higher level code
9.4 Keep function parameters focused
9.4.1 A function that takes more than it needs can be hard to reuse
9.4.2 Solution: Make functions take only what they need
9.5 Consider using generics
9.5.1 Depending on a specific type limits generalizability
9.5.2 Solution: Use generics
Summary
Part 3 Unit testing
10 Unit testing principles
10.1 Unit testing primer
10.2 What makes a good unit test?
10.2.1 Accurately detects breakages
10.2.2 Agnostic to implementation details
10.2.3 Well-explained failures
10.2.4 Understandable test code
10.2.5 Easy and quick to run
10.3 Focus on the public API but don’t ignore important behaviors
10.3.1 Important behaviors might be outside the public API
10.4 Test doubles
10.4.1 Reasons for using a test double
10.4.2 Mocks
10.4.3 Stubs
10.4.4 Mocks and stubs can be problematic
10.4.5 Fakes
10.4.6 Schools of thought on mocking
10.5 Pick and choose from testing philosophies
Summary
11 Unit testing practices
11.1 Test behaviors not just functions
11.1.1 One test case per function is often inadequate
11.1.2 Solution: Concentrate on testing each behavior
11.2 Avoid making things visible just for testing
11.2.1 Testing private functions is often a bad idea
11.2.2 Solution: Prefer testing via the public API
11.2.3 Solution: Split the code into smaller units
11.3 Test one behavior at a time
11.3.1 Testing multiple behaviors at once can lead to poor tests
11.3.2 Solution: Test each behavior in its own test case
11.3.3 Parameterized tests
11.4 Use shared test setup appropriately
11.4.1 Shared state can be problematic
11.4.2 Solution: Avoid sharing state or reset it
11.4.3 Shared configuration can be problematic
11.4.4 Solution: Define important configuration within test cases
11.4.5 When shared configuration is appropriate
11.5 Use appropriate assertion matchers
11.5.1 Inappropriate matchers can lead to poorly explained failures
11.5.2 Solution: Use an appropriate matcher
11.6 Use dependency injection to aid testability
11.6.1 Hard-coded dependencies can make code impossible to test
11.6.2 Solution: Use dependency injection
11.7 Some final words on testing
Summary
appendix A Chocolate brownie recipe
appendix B Null safety and optionals
B.1 Using null safety
B.1.1 Checking for nulls
B.2 Using optional
appendix C Extra code examples
C.1 The builder pattern
index
A
B
C
D
E
F
G
H
I
J
L
M
N
O
P
Q
R
S
T
U
V
Good Code, Bad Code - back