Professional Documents
Culture Documents
BAD CODE
by Tom Long
TABLE OF CONTENTS
01 03
Code quality Other engineers and code
contracts
02 04
Layers of Errors
abstraction
01
CODE QUALITY
High-quality Low-quality
code code
It should keep
It should work
working
It should be
adaptive to
It should not reinvent
changing
the wheel
requirements
The pillars of code quality
Functions
The code inside each function
should ideally read like a single,
short, well-written sentence.
API
Classes
application programming interface ● Number of lines
● Cohesion
● Separation of concerns
Interfaces
Use an interface where it provides an
appreciable benefit, but not to do it just
for the sake of doing it.
When layers get too thin
It’s hard to come up with a single rule or
piece of advice that will tell us definitively
whether a layer is too thick, as it will often
depend on the nature of the real-world problem
that we’re solving. The best advice is to use our
judgment and to think carefully about whether the
layers we have created will ensure that the code is
readable, reusable, generalizable, modular, and
testable.
03
OTHER ENGINEERS AND CODE
CONTRACTS
YOUR CODE AND OTHER
ENGINEERS’ CODE
Understanding Conflicts Your memory
Things that are obvious to you Other engineers will inadvertently try In time, you will forget
are not obvious to others. to break your code. about your own code.
How will others figure
out how to use your
code?
Look at the names of things (functions, classes,
enums etc.).
Look at the data types of things (function and
constructor parameter types and return value types).
Read any documentation or function-/class-level
comments.
Come and ask you in person, or over chat/email
PROGRAMMING BY CONTRACT
Everyone knows that they really should read the small print of
every contract they enter, but most people don’t. Using small print
is, therefore, not a reliable way to convey the contract of a piece of
code. Relying too much on small print is likely to produce fragile
code that is too easy to misuse, and that causes surprises
ALTERNATIVE METHODS
Checks Assertions
These are additional logic that When the code is compiled in
check that a code’s contract has a development mode, or when
been adhered to tests are run a loud error or
exception will be thrown if a
condition is broken.
The key difference between assertions and checks is that assertions are
normally compiled out once the code is built for release, meaning no loud failure
will happen when the code is being used in the wild.
04
ERRORS
There are broadly two types of error:
EXPLICIT IMPLICIT
Checked exceptions • Unchecked exceptions
Nullable return type • Promise of future
Result return type • Returning a magic value
Outcome return type
CODE THAT CAUSES A COMPILER WARNING
class UserInfo {
private final String realName;
private final String displayName;
String getRealName() {
return realName;
}
String getDisplayName() {
return realName;
}
}
“High-quality code helps create high-quality
software and identifying what we’re
fundamentally trying to achieve helps us
judge code quality more objectively.”
—TOM LONG
THANKS!
Do you have any questions?
barskyiandriy@gmail.com
+380 67 584 255 4