How bad are calculated values really?

3
I've always been taught, as I guess most of you are, that calculated attributes should not be used in Mendix. There is supposed to be a performance hit for a couple of reasons: - it will be calculated each time it is read instead of only once when it's written - when retrieving records from the database where the entity has a calculated attribute, all attributes will always be retrieved instead of just the needed attributes The most important advantage of ca's I think is simple: - No need for complicated and error prone code to make sure the value is always kept up to date   What do you think? Should we still avoid ca's, or is it fine to use them. Should you using them only in specific cases? If so, Which?
asked
2 answers
4

The case where you need to use calculated fields is when you want to translate data in multiple languages. Due to the fact that you only retrieve the value of the same language as the user it does not suffer from the performance hit. See the post about multi lingual environments in the forum on how to set up your domain model.

Otherwise you have to think calculated fields really trough because indeed recalculation 2.000.000 rows of data each time you show a grid is not a good sollution.

Regards,

Ronald

 

answered
4

Hi Nikel,
I must admit that I have been taught the same about calculated variables, and until recently I believed it blindly. However, I found that Mendix has made a number of improvements that diminishes the impact of some disadvantages.

For example, calculated attributes seem to be calculated only if they are really needed i.e. they are displayed to the user or are used in a MF. (probably there are exceptions to this) but it is definitely not the case that they are recalculated all the time when you do an operation on the object.

I completely agree with your remark that the alternative is sometimes too error prone as you try to catch all the places where the calculated attribute needs to be recalculated. This can lead to either unnecessary recalculations, or forgetting to recalculate when it is really needed.

All in all, I think calculated attributes are not as bad as we all believe them to be. That being said, the calculations should be lightweight. I personally avoid DB retrieves in calculated attributes.

Those are my personal thoughts, I would love to hear what other users think about calculated attributes.
-Andrej

answered