Good Example
Here is an example of the same code written with good style (and NO comments) in Java
for ( primeCandidate = 2; primeCandidate <= num; primeCandidate++ ) {
isPrime[ primeCandidate ] = true;
}
for ( int factor = 2; factor = (num / 2); factor++ ) {
int factorableNumber = factor + factor;
while ( factorableNumber <= num ) {
isPrime[ factorableNumber ] = false;
factorableNumber = factorableNumber + factor;
}
}
for ( primeCandidate = 1; primeCandidate <= num; primeCandidate++ ) {
if ( isPrime[ primeCandidate ] = 1 ) {
System.out.println( primeCandidate + " is prime." );
}
}
In this 2nd fragment, it is immediately obvious that this code has something to do with prime numbers and factors and after only a little examination. It becomes obvious that this is a fairly standard implementation of the
Sieve of Erathosthenes algorithm for finding prime numbers. Notice that the difference between the two code fragments has nothing to do with comments, As there were none in either example. However, the 2nd fragment is much more readable. So readable in fact that we could almost get away without using comments at all.
--
ShawnDB - 16 Jun 2008
Revision: r1.1 - 16 Jun 2008 - 03:09 - Main.guest