Clean and
readable code
Denisa Trif
Why should it be clean and readable?
Because...
- ...Claudia said so
- ...Your audience isnt just a computer
- ...It proves good software design skills
- ...Clean code is about caring
Programming is the art of telling
another human what one wants the computer to
do.
Donald Knuth
Naming standards
Names should describe exactly what code does
o Classes substantives
o Methods verbs
Avoid misinformation
Use pronounceable names
Use searchable names
Meaningful distinctions
Methods
Methods should do ONE THING
Line limit
One level of abstraction / method
Number of arguments <= 3
Try to avoid methods with boolean arguments (they surely dont do
just ONE THING)
Have no side effects (ONE THING only)
Dont repeat yourself avoid duplication
Comments
GOOD
Informative comments
Explanation of will
Clarification
Warning of
consequences
TODOs
Javadocs for public APIs
BAD
Mumbling only if the best
comment you can write
Redundance with the code
(or name of the variable,
method etc.)
Misleading comments
Noise comments (e.g. for
constructors)
Comments pay attention to:
commented out comments and left
there
nonlocal information
too much information
inobvious connections
Methods + comments =>
Great code style and
easy readable code !!!
Error handling
- dont pass null
- dont return null
Classes
Order:
1. Public static constants
2. Private static variables
3. Private instance variables
4. Public functions
- private utilities called by a public function right after the
public function itself
Classes should be small
Class names including weasel names like Processor, Manager or Workflow
ofthen hint an unfortunate aggregation of responsibilities
- We should also be able to write a brief description of the class in about
25 words, without using the words if, and, or, or but.
No pressure...
Theres no problem so simple that a bad developer cant make it
complicated.
Imagine youre sitting in a technical interview. If youre asked about your code
quality, could you provide a professional and rational justification? Or would this be
you?
What gets measured, gets managed.
Clean code doesnt happen by accident.
However, writing code is relatively easy. Reading is hard.
We dont have time to be sloppy. Being sloppy ultimately slows
us down in the long-term due to the increased frequency of
bugs and the long-term costs of maintenance later. History has
shown its unlikely youll find time to clean it up later, so why
not do it right the first time?
Dont be a verb: Oh man, this code has been Jimmyd.
References
http://blog.pluralsight.com/7-reasons-clean-code-
matters
Clean Code, Robert C. Martin