Using Variables vs. Literals in a strategy


In another post I mentioned suggestions for naming conventions for variables, such as using all caps for a constant like: MAX_TIME_TO_WAIT_IN_SECONDS.

But why not just use a literal, which will never vary, instead of a variable?


A variable can be changed on-the-fly from PAC Display or PAC Control’s debugger, perhaps while troubleshooting another part of the logic or making the final decision on what the constant will be.

Even though you think it should be 3 now, you might find later that 5 works better.

Most important[B] for maintainability[/B]: If you’d used a literal in several places you’ll have to find and change each instance – without benefit of PAC Control’s search feature. That’s because you can search a strategy for a variable like MAX_TIME_TO_WAIT_IN_SECONDS while you cannot search for a literal like 3.

Minor consideration: The same literal used several times could take more of the controller’s memory than a reused variable (perhaps you are using PI, or 0 vs. ZERO, for example).

Write on!