Concrete suggestions for software developers
Here are some concrete guidelines I would offer developers so they
can recognize the problem we've been discussing, and so they can avoid
creating it.
- Keep your variables as "tightly scoped" as possible. Don't use an
instance variable where a local variable will do. Don't use a class
(static) variable where an instance variable will do. Failure to
observe this guideline makes it more likely that you'll create
threadsafety issues for yourself, and also more likely that somebody
else will write code that prevents you from changing yours (because
their code depends on something you've exposed).
- If you see a service object being created on a
per-method-invocation basis or a per-HTTP-request basis, be
skeptical. Not saying that it's always wrong, but you should
immediately ask whether the service is supposed to be stateless and
also how expensive the construction is.
- When you diagnose some issue, avoid the temptation to apply the
first fix that comes to your mind. Share the diagnosis with a
colleague and get an opinion. In particular, distinguish between fixes
that improve the way you use a piece of code from fixes that make it
harder to misuse that code, and prefer the latter. That's a root cause
fix and it will save other developers from misusing the code.
Suggestions on mentoring software developers
For people who've been writing software for many years, the
foregoing engineering discussion is trivial: of course we want to keep
variables as tightly scoped as possible, and of course we don't want
to keep instantiating stateless service objects. But my points aren't
engineering points. They're points about mentoring software
developers:
- Experienced developers often lose sight of the misunderstandings
that more junior developers have, and can't mentor effectively without
that visibility.
- There's a difference between a Band-Aid and a root cause fix, and
it's important that our developers understand code problems deeply so
that root cause fixes are applied. Just because production issues are
getting fixed doesn't mean that the fixes are "right".
- Look at the code itself from time to time and use it as a
launching pad for software engineering discussions. It usually doesn't
take much effort to find interesting topics for discussion lurking in
the code.