Now that we have all our tooling set up for running tests and writing clean code, we are ready to continue with the design of our application. The next component that we should develop is the quiz component.
Exercise 1:
What should the "quiz component" do?
Using artifact, write down the high level design docs of the quiz component
Designing To Test
Before we continue too far, let's review what the quiz component does:
- it is given a list of questions
- it chooses a question at random
- it keeps track of right/wrong answers and assigns greater weight to the ones the user is getting wrong.
There are two common design patterns in software development:
- Object Oriented: the
Quiz
component is an object which exposes a method likeask_question()
, which returns a randomQuestion
. Each time a question is answered it records it in it's internalAnswered
object (it holds state) - Functional: the quiz component is composed of two functions -- one
for getting a question and one for asking a question.
get_question(questions, answered)
takes the list ofQuestion
objects and theAnswered
object and outputs aQuestion
randomly based on the weights.- The
ask_question(question)
takes in theQuestion
and outputs true if it was answered correctly and false otherwise (interfacing with the user). - The caller then updates the
Answered
object with the result before it asks another question.
These represent two design paradigms called "object oriented programming" and "functional programming". Let's take some observations:
- The Object oriented approach seems simpler -- all the caller has to
do is call
Quiz.ask_question()
over and over again. - The functional approach seems easier to test -- if you have the same inputs you will always have the same outputs (except when talking to the user!)
- The functional approach is easier to reason about -- there is no hidden state, everything happens in the open.
We will be using the functional programming style in this guide. In general, it is always good practice to think of ways in which you program in a functional style. Most find that as applications get large, a functional style is much easier to test and reason about.
Exercise 2:
Spend some time researching the differences between functional and object-oriented programming. What things strike you as useful to each style?