Hey, long time since I've posted here.
Anyway, I'm taking Computer Science at the University of Calgary and my professor and TA both hate the idea of "magic numbers" in code. They say it would be best to assign a variable a number and use the variable inside my equations.
Anyway, I had nice Python code that looked really very clean for how complex my equations were (evaluating third degree polynomials at different values of x in a range) but I had to shift all my data 400 units in the X direction and 300 units in the Y direction. I simply used (value_a + 300) but my TA mentioned that I shouldn't have these magic numbers.
What I'm getting at is that I assigned a variable Y_SHIFT instead of 300, X_SHIFT instead of 400, and PIXEL_UNIT as 30. Now, my once simple code, is filled with these variables written in all caps and my equations don't fit on one or two lines anymore. My equations are broken up and difficult to read with all these variables replacing my "magic numbers". As it stands, I also have a few numbers still floating around as I want certain things to be shifted 402 units or 398 units instead of the standard 400. so I now have code that looks like "Y_SHIFT + 4" in the middle of my equations.
So I realize that if those numbers ever need to change, it will be very easy to look back at my code and change them, but right now that code is messy to read and work with, but still functions as it should.
I'm wondering if anyone here can attest to specific benefits of creating these variables, or if they use them at all to eliminate so called "magic numbers". Also, if you're all for it, should I create additional ones that have values of 398, 402, etc, or should I just keep my Y_SHIFT + 2 in my equations?
Thanks in advance.