Time 60 mins
Prerequisites Thinking Procedurally
Authors Sandra Kuipers
Minimum Year Group None
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.
This work is shared under the following license: Creative Commons BY-SA-NC
What was successful? What needs changing? Alternative Assessments and Lesson Ideas? What other Differentiation Ideas/Plans could be used?
Any CC attribution, thanks, credit, etc.
This page requires you to be logged in to access it. Please login and try again.
Under the Hood
- 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.
- 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.
- spaghetti code
- code is available open source
- The Evolution of Software Architecture
- Benoit Hediard
- how to write good code
There are no records to display.