Introduce Constant

The new code editor allows you to do away with magic numbers passed as parameters, in conditional statements, or lurking elsewhere in your code. It allows you to at least push these numbers into constant variables. That way if the number changes, you only need to change the constant and not hope you find all occurrences of the magic number in your code. (Of course, it would be even better if you externalized the value so you did not have to recompile your code to change a number.)

Let’s look at an example. Figure 9.10 shows code for a discount rule on an invoice. The rule states that if the order is greater than $100, then a 10% discount should be applied. However, both 100 and .10 are represented as magic numbers in the code. ...

Get Microsoft Visual Studio 2015 Unleashed, Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.