This is a nice question I kept asking myself. 
I came to the conclusion that it is best to keep your model and your update 
function as simple as possible.
In the model, there shouldn't be more than one representation of the same 
datum.
The calculations should be made inside the view function, unless you 
encounter performance issues.
Here are two reasons:
- This way, I understand the program better. When I look at an elm-program, 
I first look at the model. So the model should be comprehensible. If you 
have two different representations of the same datum, I think you should 
write a comment and make that clear that it is for performance reasons. 
Performance should be the only reason for breaking this rule. As long as 
you don't encounter performance problems, keep the model simple, even if 
this makes you calculate things more than once. 
- It also prevents bugs because if you have two fields holding different 
representations of the same thing, every time you update one field you also 
should update the other. Sometimes, you forget that and then you lose your 
time to find out where you made the mistake.

On Tuesday, July 25, 2017 at 3:23:44 AM UTC+2, Ray Toal wrote:
>
> This might be an opinion-based question so I can't ask it on 
> StackOverflow. :-)
>
> I have a trivial beginnerProgram using the Elm Architecture. It has two 
> text fields (HTML input elements), one for weight and one for height. The 
> onInput attributes of each generate a message. The update function accepts 
> the message and produces the new model.
>
> The question is: Should the model (a) consist of the width and height 
> only, or (b) also include the computed BMI (width / height^2)? If the 
> former, I compute the BMI in the view function; if the latter, I would 
> compute it in the update function.
>
> Does anyone have a lot of experience with Elm applications that would lead 
> them to believe that keeping models as small as possible and computing 
> derived data in the view function is better than making rich models? I 
> don't have enough experience with Elm to know. I sense that for a problem 
> as simple as mine it really doesn't matter (and I in fact got it working 
> both ways), but as I start scaling up my apps it would be nice to know if 
> there is best practice here (or not).
>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to