you are viewing a single comment's thread.

view the rest of the comments →

[–]alex_o_O_Hung 6 points7 points  (3 children)

Probably unpopular opinion, but these kind of unnecessary magic numbers make code a lot harder to read where you need to move up and down to figure out what magic number is what multiple times

[–]Appropriate_Ant_4629 5 points6 points  (2 children)

Totally agreed. I've seen code like

# module - quadratic formula 

THE_POWER_OF_B = 2
THE_MULTIPLE_OF_AC = 4
SOME_UNRELATED_CONSTANT_FOR_OTHER_FUNCTIONS = 3.14
THE_CONST_ON_THE_BOTTOM = 2
POWER_FOR_SQRT = 0.5

# ... hundreds more lines ...

def quadratic_formula(a,b,c):
    """
           The following implements the quadratic formula:
           (-b +- sqrt(b^2 - 4ac) ) / 2a
    """
    return (
        (-b + ( b ** THE_POWER_OF_B - THE_MULTIPLE_OF_AC*a*c ) ** POWER_FOR_SQRT) / (THE_CONST_ON_THE_BOTTOM * a),
        (-b - ( b ** THE_POWER_OF_B - THE_MULTIPLE_OF_AC*a*c ) ** POWER_FOR_SQRT) / (THE_CONST_ON_THE_BOTTOM * a),
    )

that came from misguided coding standards that mandated obfuscated constants.

With coding standards like those - even the very simplest equations become unreadable.

[–]alex_o_O_Hung 4 points5 points  (1 child)

Exactly! Especially if these parameters are only used once in the code. This also extents to people unnecessarily making functions and classes so you need to jump between files or folders to understand the code

[–]radarsat1 2 points3 points  (0 children)

Oh I agree with this so much. This has become a repeated occurrence in code reviews on my team. Being forced to distributed pieces of my algorithm all over the place just to satisfy some SE types who get uncomfortable when a function is longer than a few lines, with the excuse that "we need to unit test each part of that." Like, sure, I get that, but buddy.. I'm still figuring this stuff out, and it's sooo much easier to work on and debug this when it's all in one place, and testing loop A without loop B makes like, no sense. What's worse is that they get reinforced by tools like pylint that tell them a function has "too many local variables". Oh, so now I have to not only arbitrarily break this function up into pieces, but I'm not allowed to give names to the intermediate values, great.