The primary purpose of the DATA statement is to give names to
constants; instead of referring to pi as 3.141592653589793 at every
appearance, the variable PI can be given that value with a DATA
statement and used instead of the longer form of the constant. This
also simplifies modifying the program, should the value of pi change.
Xerox Basic FORTRAN and Basic FORTRAN IV Manual, attributed to David H. Owens. Source: https://en.wikiquote.org/wiki/Fortran
Of course, commonly used constant values should be used via constant declarations which make it plainly evident to whoever is looking at the code what the value stands for, and to make it easy to change the value in the future, if need be. However, this is not to be viewed as some absolutely binding dictum that should be followed with dogmatic devotion.
Consider the following implementation of a hashCode()
function:
/**
* Computes the FNV hash code of all elements in this unmodifiable enumerable.
*/
static <E> int hashCode( UnmodifiableEnumerable<E> self )
{
int hash = 17;
for( E element : self )
hash = 31 * hash + Objects.hashCode( element );
return hash;
}
As the comment says, this is the (most common simplified version of the) standard Fowler–Noll–Vo hash function that most programmers are familiar with. There is no point in creating constants for 17 and 31, since that would only further complicate things. They are just two prime numbers, their use is highly localized, (only within this function,) they are highly unlikely to ever change, and if there is ever a need to change them, the person who will decide to change them better know very well what they are doing, and therefore better know very well what they stand for.
Other times, there exist numbers that are so permanently cast in stone, and so very well known by everyone, that constants are sometimes not necessary. The hilarious quote from the Basic FORTRAN IV Manual illustrates this nicely, but here is another example: A week has seven days everywhere around the world, and this is very highly unlikely to ever change, so a simple expression which involves existing identifiers that already have meaningful names does not have anything to benefit from replacing a hard-coded literal 7
with a constant. Who really needs to see days = weeks * DAYS_PER_WEEK
instead of days = weeks * 7
?
(Of course, if the expression is more complicated, or if the other identifiers that take part in the expression do not have such self-explanatory names, it may still be better to use DAYS_PER_WEEK
rather than 7
, for the purpose of documenting what you are doing.)
That having been said, you have to be careful with cultural issues.
On page 300 of the book "Clean Code" by Prentice Hall, author Robert C. Martin says:
And in the FEET_PER_MILE case, the number 5280 is so very well known and so unique a constant that readers would recognize it even if it stood alone on a page with no context surrounding it.
To which I have to say (excerpt from my blog)
The world is not the USA. Most of the world uses the metric system, and knows very little about miles and feet. When given a number in "United States customary units", many people outside the USA know how to convert it to metric, but it is rare to meet someone who is proficient in converting from metric to US units, and even more rare to meet someone who has the slightest clue, or the slightest interest, in converting between US units. Therefore, the number 5280 means absolutely nothing to the vast majority of the population of the planet. As a matter of fact, there are so many non-USAians working as software engineers in the USA, that even in the USA it would be a bad idea to assume that anyone who sees the number 5280 in a piece of code will know what it stands for.
Finally, magic numbers in their true sense (of having some secret meaning) should nearly always be declared with a constant, otherwise their occurrence in the middle of source code can be very puzzling, but even this rule has an exception. If you are only going to use a particular magic number once in your entire code base, and if it is to appear in a piece of code like the following, then it is okay, because what is going on is very obvious:
if( fileObject.getMagicNumber() == 0xbadbeef ) { ... }