O'Reilly logo

Microsoft Visual Studio 2015 Unleashed, Third Edition by Mike Snell, Lars Powers

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required