Code Complexity
Time 60 mins

Difficulty Intermediate
Prerequisites Thinking Procedurally
Departments Science
Authors Sandra Kuipers
Groupings Individual
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.

You do not have access to this action.
5 mins
Complexity
Getting Started
15 mins
Under the Hood
Digging In
  • Before long, the Universal Paperclips game gets pretty complex.

  • Most of the game happens in two files:
  • 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
There are no records to display.
Powered by Gibbon v24.0.00dev | © Ross Parker 2010-2022
Created under the GNU GPL at ICHK | Credits | Translators