Code Complexity
Time
60 mins
Difficulty
Intermediate
Prerequisites
Thinking Procedurally
Departments
Science
Authors
Sandra Kuipers
Groupings
Individual
Pairs
Pairs
Minimum Year Group
None
Blurb
What causes code complexity, and how can we avoid it? This unit looks at the ways we can write code that is easier to read and maintain.
License
This work is shared under the following license: Creative Commons BY-SA-NC
Outline
Learner Outcomes Students will:
|
|
Competency Focus
|
Interdisciplinary Connections
|
Reflection What was successful? What needs changing? Alternative Assessments and Lesson Ideas? What other Differentiation Ideas/Plans could be used?
|
|
Credits Any CC attribution, thanks, credit, etc.
|
This page requires you to be logged in to access it. Please login and try again.
5 mins
Complexity
Getting Started
15 mins
Under the Hood
Digging In
- Before long, the Universal Paperclips game gets pretty complex.
- Wonder how it was programmed?
- Luckily, the code is available open source!
- Take a quick peek under the hood:
- Most of the game happens in two files:
- One called globals.js
- Another called main.js
- Spend a few minutes scrolling through the code. What do you see?
- Don't read it all (there's 5546 lines in main.js alone!)
- The idea is to get a sense of the functions, conditionals and variables used.
10 mins
Software Architecture
Digging In
- What did you think of the Universal Paperclips code?
- Easy to understand?
- Hard to understand?
- Don't worry if you had trouble reading it.
- I didn't share the code because it's a perfect example of how to write good code.
- But, its not all bad either. We can learn a lot by reading other people's code.
- The good:
- The game logic is separated out into functions.
- Nested only one or two levels deep.
- Well named variables and functions.
- For the most part we can understand what each function does.
- Many of the conditionals are easy to read, for example:
if(funds >= wireCost)
- The bad:
- It's HUGE! Generally something this big we would split into smaller files.
- There's conditionals, functions and variables mixed everywhere.
- We've played the game, so we know the code works, but it's borderline spaghetti code.
Image from The Evolution of Software Architecture by Benoit Hediard
- According to this chart, where would you classify the Universal Paperclips code?
- Perhaps somewhere between spaghetti and lasagna?
- Modern programming is all about the ravioli.
- Small, bite-sized, modular code.
30 mins
Writing Pseudocode
Evidence
Links
- spaghetti code
- globals.js
- code is available open source
- main.js
- The Evolution of Software Architecture
- Benoit Hediard
- how to write good code
Images
There are no records to display.