Unrepresentable states in Elm

32
Elm and unrepresentable states My Tailor-made Model Nicolas Umiastowski @n_umiastowski Remote meetup November 22nd 2016

Transcript of Unrepresentable states in Elm

Page 1: Unrepresentable states in Elm

Elm and unrepresentable states

My Tailor-made Model

Nicolas Umiastowski@n_umiastowski

Remote meetup November 22nd 2016

Page 2: Unrepresentable states in Elm
Page 3: Unrepresentable states in Elm
Page 4: Unrepresentable states in Elm

First attempt : make it workIn a very « non-functional » kind of way

Page 5: Unrepresentable states in Elm

13

2

1

0

Page 6: Unrepresentable states in Elm

Update

Page 7: Unrepresentable states in Elm

Answered questions

There questions are really answered?

Page 8: Unrepresentable states in Elm
Page 9: Unrepresentable states in Elm

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?

Page 10: Unrepresentable states in Elm

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

Page 11: Unrepresentable states in Elm

Making undesired state unrepresentable

Page 12: Unrepresentable states in Elm

Distinguish between answered and non answered questions.

But that is not a state, that is just two lists!

Page 13: Unrepresentable states in Elm

Unrepresentable states

I display only one list of questions

I check that there is no unanswered questions

Page 14: Unrepresentable states in Elm

What could happen… ?

Page 15: Unrepresentable states in Elm

I could have answered questions without an answerId

Page 16: Unrepresentable states in Elm

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?

Page 17: Unrepresentable states in Elm

Define a real state for the questions

Page 18: Unrepresentable states in Elm
Page 19: Unrepresentable states in Elm
Page 20: Unrepresentable states in Elm
Page 21: Unrepresentable states in Elm

Modify an existing answer

Answer a question

Page 22: Unrepresentable states in Elm

If there are only answered questions….

… then we display the messsage

Page 23: Unrepresentable states in Elm

All questions have only two possible states : • Answered• Unanswered

Invalid states?

Page 24: Unrepresentable states in Elm
Page 25: Unrepresentable states in Elm
Page 26: Unrepresentable states in Elm

3rd attempt of unrepresentable statesWhy not have a state for the answer, instead of an Id ?

Page 27: Unrepresentable states in Elm

The answers also have different states.

Page 28: Unrepresentable states in Elm

State of the question

State of the answer

Page 29: Unrepresentable states in Elm

By the way… unrepresentable states does not prevent all impossible states!

Page 30: Unrepresentable states in Elm

I could have two different questions with the same Id, one question with an empy text, etc…

Page 31: Unrepresentable states in Elm

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

Page 32: Unrepresentable states in Elm

Questions ?