Say I have a list of numbers from 1 to MAGIC_NUMBER -- Is there a way I can declare this beforehand ?
The term magic number also refers to the bad programming practice of using numbers directly in source code without explanation. In most cases this makes programs harder to read, understand, and maintain.
A magic number is a number in the code that seems arbitrary and has no context or meaning. This is considered an anti-pattern because it makes code difficult to understand and maintain. One of the most important aspects of code quality is how it conveys intention. Magic numbers hide intention so they should be avoided.
0, 1, -1, 2 These are not generally considered magic numbers, and it's ok to have them in your code. However, it's often possible to add more context to explain why you're using these numbers. For example, you could introduce a new one-line function with a meaningful name.
A magic number is a numeric value that's encountered in the source but has no obvious meaning. This “anti-pattern” makes it harder to understand the program and refactor the code.
Sure. In fact, given that Haskell is purely functional, it's much easier to define a constant than a non-constant.
magicNumber = 42 magicList = [1..magicNumber]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With