Putting Constants Into Action
00:00 Putting Constants Into Action
00:03 So far, you’ve learned about constants and their role and importance in programming. You’ve also learned that Python doesn’t support strict constants. That’s why you can think of constants as variables that never change. In programming, the term magic number refers to any number that appears directly in your code without any explanation.
00:23 It’s a value that comes out of the blue, making your programs less readable and more difficult to maintain and update.
00:31 Let’s say you have the function seen on screen. Can you tell upfront what the meaning of each number in this computation is? Probably not. The different numbers in the function are magic numbers because you can’t reliably infer their meanings from the numbers themselves.
00:47 Now take a look at a refactored version of the function.
00:51 With these minor updates, the new function now reads easily. You and any other developers reading the code can tell what the function does because you’ve replaced the magic numbers with appropriately named constants. The name of each constant clearly explains its meaning.
01:08 Every time you find yourself using a magic number, take the time to replace it with a constant. The constant’s name must be descriptive and unambiguously explain the meaning of the target magic number.
01:19 This practice will automatically improve the readability of your code.
01:25 Another everyday use case of constants is when you have a given value repeatedly appearing in different parts of your code. If you insert the concrete value into the code at every required place, then you’ll be in trouble if you ever need to change the value for any reason.
01:40 In this situation, you’d need to replace the value everywhere. Changing the target value in multiple places is error-prone. If you rely on your editor’s find and replace feature, you can leave some values unchanged or change values you didn’t intend to, which can lead to unexpected bugs and weird behavior.
01:58 To prevent these problems, you can replace the value with a properly named constant. This will allow you to set the value once and repeat it in as many locations as needed.
02:09
If you ever need to change the constant’s value, then you just have to change it in one place–the constant definition. On screen, you can see a Circle
class, which needs methods to compute the circle’s area, perimeter, and so on.
02:39 This example uncovers how the approximate value of Pi has been written as a magic number in several methods. Why is this practice a problem? Well, let’s say you need to increase the precision of Pi.
02:50 Then you’ll have to manually change the value in at least three different places, which as we’ve already seen is tedious and error-prone. Using a named constant to store the value of Pi is an excellent approach to solving these issues.
03:03 On screen, you can see the original code being updated to make use of a constant.
03:18
This version of Circle
uses the global constant Pi to replace the magic number. This code has several advantages compared to the original. If you need to increase the precision of Pi, then you just have to update the Pi constant’s value at the beginning of the file.
03:34 This update will immediately reflect on the rest of the code without requiring any additional action.
03:42
A constant shouldn’t change during the code’s execution, but during development, you can change and tweak your constants according to your needs. Updating the precision of Pi in the Circle
class is a good example of why you may need to change the value of a constant during the development of your code.
03:58 Another advantage is that now your code is more readable and easier to understand. The constant’s name is self-explanatory and reflects the accepted math terminology.
04:08 Declaring a constant once and then reusing it represents a significant maintainability improvement. If you have a need to update the constant’s value, then you’ll update it in a single place rather than in multiple places, which is less effort and lower risk.
04:26 In the next section of the course, you’ll take a look at using constants to provide default argument values.
Become a Member to join the conversation.