Tomas Petricek
email: t.petricek@kent.ac.uk
twitter: @tomaspetricek
office: S129A
Software Maintenance
You spend more time reading and modifying code than writing it.
Investing into readability when writing pays off
Keeping code simple
Shared understanding in a team
Code for readability
Always code as if the person maintaining your code will be a violent psychopath who knows where you live.
The person maintaining it will likely be future you.
Hacker culture
Coding standards
Do programming languages matter?
[It] appear[s] that strong typing is modestly better than weak typing, and among functional languages, static typing is also somewhat better than dynamic typing.
Do programming languages matter?
Attempt to reproduce the study mostly failed
"I believe they do in my heart of hearts, but it's kind of an impossible experiment to run."
Aspects of code quality
Processes for keeping quality
Good naming is easy today!
Computers supports
lower case letters!
Long names fit in
computer memory
Auto-complete helps
you avoid typos
What is the following function doing?
1: 2: 3: |
|
Converting temperature from Celsius to Fahrenheit!
1: 2: 3: |
|
Was that descriptive enough?
1: 2: 3: 4: |
|
Can we improve this by using abbreviations?
1: 2: 3: |
|
Are all variable names in this example descriptive?
1: 2: 3: |
|
This is too much. Sensible exceptions allowed!
1: 2: 3: 4: |
|
Name should add semantic information
No need to say it's number, string, etc.
Longer names are good, but don't overdo it!
Follow common conventions where applicable
The more local, the less it matters
Combining words in names
PascalCase
camelCase
snake_case
Using spaces in a name
Is the following naming consistent or not?
1: 2: 3: 4: 5: 6: 7: 8: |
|
Pascal case Program
, camelCase adminMode
, main
Java standards use PascalCase
for class names
camelCase
is used for variables and method names
You should generally follow coding standards
Exceptions such as numerical code: P
or alpha
GOTO considered harmful
Can you tell what this does?
1: 2: 3: 4: |
|
And what about this?
1: 2: 3: |
|
Is there an easier way to write this?
1: 2: |
|
You do not need conditional statement at all!
1:
|
|
When does the following return a product page?
1: 2: 3: 4: 5: 6: 7: |
|
When product is available and not hidden!
Which branch is the "happy path" branch?
Keep the happy path branches consistent!
1: 2: 3: 4: 5: |
|
Reduce unnecessary nesting
1: 2: 3: |
|
Use appropriate language constructs
Make sure you're not complicating things
Keep "happy path" through code clear
Do not use unnecessary nesting of logic
What is the right spacing and line breaks?
1: 2: 3: 4: 5: 6: 7: 8: |
|
Compiler ignores whitespace, but humans do not!
Follow style of a project or a language; be consistent!
Indent nested code by the same number of spaces or tabs
1: 2: 3: 4: 5: 6: 7: 8: 9: |
|
Are you explaining something you cannot see from code?
1: 2: 3: |
|
Also applies to Java inline documentation for methods
1: 2: 3: 4: |
|
Don't use comments if you have nothing to add
Comments should add history, motivation, context
Use consistent spacing and indentation
Keep nesting and length of functions sensible
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: |
|
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: |
|
Social approaches
Technological solutions
Refactoring
Is a code transformation that
(i) Does not change meaning
(ii) Improves quality of code
Test-driven development
The name of the function and input argument are poor
1: 2: 3: |
|
Improve quality using rename refactoring
1: 2: 3: |
|
Note that we use new name in all calls to conv
What is wrong with this function?
Comment indicates it does two separate things
In reality it would be very long
1: 2: 3: 4: 5: 6: 7: 8: |
|
Improve quality using extract function refactoring
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: |
|
Note that comment becomes a function name!
What is code quality and why it matters
More time is spent reading code than writing it
Extreme programming - simplicity & shared understanding
Specific rules for keeping high code quality
Naming variables, upper/lower case, indentation
Keeping logic simple and comments meaningful
Maintaining and improving code quality
Social and technological approaches to quality
Refactoring and red-green-refactor methodology
What you should remember from this lecture
Tomas Petricek
t.petricek@kent.ac.uk | @tomaspetricek
Selected chapters from books
Papers and articles