Skip to topic
|
Skip to bottom
Jump:
TModeling
TModeling Web
TModeling Web Home
Changes
Notify
Index
Search
Webs
BioGeometry
Main
TModeling
TWiki
Edit
Attach
Printable
TModeling.GoodExample
r1.1 - 16 Jun 2008 - 03:09 - Main.guest
topic end
Start of topic |
Skip to actions
*Good Example* --- Here is an example of the same code written with good style (and NO comments) in Java <verbatim> 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." ); } } </verbatim> 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 [[http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes][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. -- Main.ShawnDB - 16 Jun 2008
to top
End of topic
Skip to action links
|
Back to top
Edit
|
Attach image or document
|
Printable version
|
Raw text
|
More topic actions
Revisions: | r1.1
|
Total page history
|
Backlinks
You are here:
TModeling
>
GoodExample
to top
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback