Whenever I want to add a new functionality, I introduce a new private variable i
ID: 647231 • Letter: W
Question
Whenever I want to add a new functionality, I introduce a new private variable into the class. Declare it just above the first method definition. leave a small comment. Set it in a certain method. And then use this set value in another method.
Now the possible problems which arise are, I dont cover all the possible cases when to modify the value of this variable and only notice a bug in it when during trying various possible user flows of the software. Is there any possible way to tie this value to some kinds of triggers so that I know all the possible situations when to modify its value? I am using Javascript.
For e.g. I create a status message for editing an existing DB record. If the updating is failed over REST HTTP I set the status. Now I forgot to reset the status when I dont hit update button and instead select another record for updating.
What is the best way to manage such new private variables? How to mange their state? what is the best software engineering way to prevent such mistakes?
Explanation / Answer
The best way is to follow separation of concerns, which means restricting the scope of your new variable to the smallest possible unit.
If the new information truly has to be a new class member, and the class is too big for you to read it through and easily find all other circumstances in which you have to deal with the new field, then the class is too big, period. Refactor it into more, smaller classes so that only one of them has to deal with the new field.
If you can find a way not to store the information indefinitely, even better is to treat it as a parameter, callback information, session attribute etc.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.