unrepresentable states in elm
Post on 13-Apr-2017
90 Views
Preview:
TRANSCRIPT
Elm and unrepresentable states
My Tailor-made Model
Nicolas Umiastowski@n_umiastowski
Remote meetup November 22nd 2016
First attempt : make it workIn a very « non-functional » kind of way
13
2
1
0
Update
Answered questions
There questions are really answered?
I want to be able to make sure that:
If one question is answered, then it has an answerId
Answered question that does not have an answerId : Possible
Invalid states?
Unrepresentable state:
Encoding the answered state of a question in the constructor means that I can’t write code that doesn’t check for whether a question is
answered or not
Making undesired state unrepresentable
Distinguish between answered and non answered questions.
But that is not a state, that is just two lists!
Unrepresentable states
I display only one list of questions
I check that there is no unanswered questions
What could happen… ?
I could have answered questions without an answerId
We can check if a questions has an answerId or not, but we cannot check that it has an “answered” or “unanswered” state.
Invalid states?
Define a real state for the questions
Modify an existing answer
Answer a question
If there are only answered questions….
… then we display the messsage
All questions have only two possible states : • Answered• Unanswered
Invalid states?
3rd attempt of unrepresentable statesWhy not have a state for the answer, instead of an Id ?
The answers also have different states.
State of the question
State of the answer
By the way… unrepresentable states does not prevent all impossible states!
I could have two different questions with the same Id, one question with an empy text, etc…
ConclusionAlways try to see if a good pattern can be appliedIt will help you learn and get betterTakes more time at the beginning, but then we go faster and bugs are avoided
Questions ?
top related