Currently I have this code all throughout my program in many places such as form resizing events, splitter moving events, document size changes, et cetera:
hsc.Value = (int)MathHelper.Clamp(hsc.Value, 0, hsc.Maximum - hsc.LargeChange + 1);
vsc.Value = (int)MathHelper.Clamp(vsc.Value, 0, vsc.Maximum - vsc.LargeChange + 1);
I’m wondering whether or not it would be better to just put it in a main loop because my program has a drawing code that is called whenever the application goes idle (very often).
The disadvantage of having this code in events is because it is code repetition and I might miss an event. The disadvantage of having it in the loop is that it may not be needed each loop and it is wasted processor cycles. However, it may be premature optimization because it probably would not be noticeable to the end-user.
What framework are you using? (WinForms, WPF, Silverlight, etc)
If it’s in every Window / Form / View / UserControl then you might consider putting this functionality in a base class and pointing all these events to event handlers in the base class.
Neither of your solutions seem correct – you shouldn’t be redrawing in a ‘main loop’ if nothing has changed, and you shouldn’t be copy/pasting duplicated code throughout your app.