0

Imagine I have updated a user interface label with a value. Since this is a label, end user cannot update the value directly and can only be changed through code.

Lets say the value is X. X could be a result of a calculation or retrieved from db. So now the label text is X.

If I need this X value for another method,

  • I can use the value from the label text

or

  • First I can store the X in a variable, update the label text from the variable and then use the variable for the method.

or

  • I can ignore the label value and freshly calculate X or retrieve X from the db and use it for the method.

My question is What is the best practice and why?

Are there better alternatives?

EDIT :

Apart from the excellent answer of Robert Harvey, I found this as an alternative way.

  • Thanks for the nod, but why does that other answer have anything to do with your question? – Robert Harvey Jul 4 '18 at 14:29
  • @RobertHarvey Actually I was searching for best practices. Since the other answer suggests to use a separate class which implements the interface (IView) of the form, I think it also encourages not to follow the "value from the label text" approach as you mentioned. Also approach of that answer separates the user interface and the user interface logic to further and testing can be easily done passing a fake IView. By asking for better alternatives I meant some other ways of doing things, like that approach. I'm sorry if it is not clear enough in the questions. :) – Sherantha Jul 5 '18 at 4:50
5

Which approach is "best practice" depends on what your specific requirements are.

The "value from the label text" approach is probably the least desirable, because the data is not being stored in its native form (it is a display representation, not a storage representation).

Whether you always calculate the label or store it in a variable depends on whether or not performance is more important than a consistently-durable calculation.

If the time between calling the two methods is inconsequential, then it may not matter; use the variable because you save a trip to the database.

If there is a non-trivial chance that the data being displayed may change between the two method invocations (and accuracy is paramount), then look it up from the database every time.

If both methods must always be presented with the same database result, use the variable.

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.