Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use constants in haskell, to avoid magic numbers?

Say I have a list of numbers from 1 to MAGIC_NUMBER -- Is there a way I can declare this beforehand ?

like image 234
agam Avatar asked Jun 27 '10 06:06

agam


People also ask

What is a magic number and why is their use discouraged?

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.

What is a magic number why are magic numbers problematic?

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.

Is 0 a magic number?

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.

What is a magic number in Kotlin?

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.


1 Answers

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] 
like image 139
Chuck Avatar answered Oct 08 '22 09:10

Chuck