Tomas Petricek
email: t.petricek@kent.ac.uk
twitter: @tomaspetricek
office: S129A
GOTO considered harmful
Can you tell what this does?
1: 2: 3: 4: |
|
And what about this?
1: 2: 3: |
|
David Parnas (1972)
Conventional
Subroutines follow the order of execution
Unconventional
Modules abstract some aspects of implementation
Procedural approach
Logical abstraction
Barbara Liskov (1974)
Abstract data types
Programming language mechanism for abstraction
Origins of objects
Object-oriented abstraction
Alan Kay et al. (1972)
Smalltalk
"Actually I made up the term "object-oriented", and I can tell you I did not have C++ in mind."
Galactic scale
Computer made of small computers that exchange messages
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
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: |
|
Any product that is sufficiently big or urgent to require the effort of many minds thus encounters a peculiar difficulty: the result must be conceptually coherent to the single mind of the user and at the same time designed by many minds.
Fred Brooks (1995 [1975])
DEMO: Fun 3D
www.fun3d.net
Functional programming abstractions
Domain-specific languages
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: |
|
Object-oriented class hierarchy
Functional algebraic data type
1: 2: 3: 4: |
|
Object-oriented class hierarchy
Functional algebraic data type
Motivation: When do we need this?
Solution: Build a language for a given domain!
Domain Specific Languages (DSLs)
Model
What we work with? How does it compose?
Syntax
How can we write it in a human-friendly way?
Functional languages
Custom operators and function composition
1: 2: |
|
Object-oriented languages
Fluent interfaces and the builder pattern
1: 2: |
|
Object-oriented - decompose into entities
Functional - separate data and functions
Domain specific languages - for a specific problem
Event sourcing - state as a list of events
Microservices - system as independent servers
Social approaches
Technological solutions
Long parameter list (bloaters)
Switch statement (OO abuse)
Speculative generality (dispensables)
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
Understandability on small and large scale
Keeping the happy path clear
Conceptually coherent code structure
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
Academic papers
Tools, books and articles