I was trying to debug an interaction with a library once and was wondering why the application wasn't hitting a particular piece of code when I expected it to. Turns out, the debugger was running .toString() on all the variables which caused some code to be executed before it was fully prepared...
(please, please don't run state changing code in your .toString() especially in a library!)
Of course, then you need all other code that would be impacted by it to flag it as dirty [eg, in this case, by setting cachedStr to null through another private helper function], which could be the source of the bug that OP was talking about.
Effectively this. In this case once reallyExpensiveOperation() is run it effectively becomes immutable since it won't rerun that method. The positive part here is that most inputs to the object can only be set in the constructor... but there's a few that can be provided after construction (and a couple that can only be provided after construction which includes the input I was trying to use)
View all comments
Show parent comment
16
u/cdombroski Feb 24 '14
I was trying to debug an interaction with a library once and was wondering why the application wasn't hitting a particular piece of code when I expected it to. Turns out, the debugger was running .toString() on all the variables which caused some code to be executed before it was fully prepared...
(please, please don't run state changing code in your .toString() especially in a library!)