-2

I know when building an Angular app (or other frontend framework with auto UI update by change detection), using function in a template is not recommended since it will result in the function being called every time when change detection runs. This means if we want to display a value derived from another value, we have to explicitly create another field to store that derived value. For example, if I want to display a temperature in both C and F and my model only stores the value of C, I have to compute the value of F from C, then store in a separate field, and display both fields in the template. Dose this considered breaking Single Source Of Truth? Are we essentially trading for performance, in the cost of having code smell?

0

Yes, thats how it is.

Software Development is the Art of Trade-Offs.

You could display the content of variables, and update those by hooking into the life cycle of the framework and there do a check if an update is necessary and then call your update methods.
If your method is a pure function you could easily implement a memorisation (store input/output combinations and reuse them instead of recalculating the values each time).

But its a trade-off.
You can choose between a not optimal performance, a code smell or complicated code.

Therefore it totally depends on the context which one is the "best" solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.