public static List<int> FindPrimes(int start, int end)
{
// Create new list of integers
List<int> primesList = new List<int>();
// Perform a loop from start to end
for (int num = start; num <= end; num++)
{
// Declare boolean variable, initially true
bool prime = true;
// Perform loop from 2 to sqrt(num)
for (int div = 2; div <= Math.Sqrt(num); div++)
{
// Check if div divides num with no remainder
if (num % div == 0)
{
// We found a divider -> the number is not prime
prime = false;
// Exit from the loop
break;
}
// Continue with the next loop value
}
// Check if the number is prime
if (prime)
{
// Add the number to the list of primes
primesList.Add(num);
}
}
// Return the list of primes
return primesList;
}
public static List<int> FindPrimes(int start, int end)
{
List<int> primesList = new List<int>();
for (int num = start; num <= end; num++)
{
bool isPrime = IsPrime(num);
if (isPrime)
{
primesList.Add(num);
}
}
return primesList;
}
Good code does not need comments. It is self-explaining.
private static bool IsPrime(int num)
{
bool isPrime = true;
int maxDivider = Math.Sqrt(num);
for (int div = 2; div <= maxDivider; div++)
{
if (num % div == 0)
{
// We found a divider -> the number is not prime
isPrime = false;
break;
}
}
return isPrime;
}
Good methods have good name and are easy to read and understand.
This comment explain non-obvious details. It does not repeat the code.
for (i = 1; i <= num; i++)
{
meetsCriteria[i] = true;
}
for (i = 2; i <= num / 2; i++)
{
j = i + i;
while (j <= num)
{
meetsCriteria[j] = false;
j = j + i;
}
}
for (i = 1; i <= num; i++)
{
if (meetsCriteria[i])
{
Console.WriteLine(i + " meets criteria.");
}
}
Uninformative variable names. Crude layout.
for (primeCandidate = 1; 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])
{
Console.WriteLine(primeCandidate + " is prime.");
}
}
The best documentation is the code itself.
Make the code self-explainable and self-documenting, easy to read and understand.
Do not document bad code, rewrite it!
// write out the sums 1..n for all n from 1 to num
current = 1;
previous = 0;
sum = 1;
for (int i = 0; i < num; i++)
{
Console.WriteLine( "Sum = " + sum );
sum = current + previous;
previous = current;
current = sum;
}
What problem does this algorithm solve?
Can you guess that sum is equal to the ith Fibonacci number?
// set product to "base"
product = base;
// loop from 2 to "num"
for ( int i = 2; i <= num; i++ )
{
// multiply "base" by "product"
product = product * base;
}
Console.WriteLine( "Product = " + product );
// compute the square root of Num using
// the Newton-Raphson approximation
r = num / 2;
while (abs(r - (num/r)) > TOLERANCE)
{
r = 0.5 * (r + (num/r) );
}
Console.WriteLine( "r = " + r );
// Variable Meaning
// -------- -------
// xPos .............. X coordinate position (in meters)
// yPos .............. Y coordinate position (in meters)
// zPos .............. Z coordinate position (in meters)
// radius ............ The radius of the sphere where the
battle ship is located (in meters)
// distance .......... The distance from the start position
(in meters)
// Scan char by char to find the command-word terminator ($)
done = false;
maxLen = inputString.Length;
i = 0;
while (!done && (i < maxLen))
{
if (inputString[i] == '$')
{
done = true;
}
else
{
i++;
}
}
// Find the command-word terminator
foundTheTerminator = false;
maxCommandLength = inputString.Length();
testCharPosition = 0;
while (!foundTheTerminator &&
(testCharPosition < maxCommandLength))
{
if (inputString[testCharPosition] == COMMAND_WORD_TERMINATOR)
{
foundTheTerminator = true;
terminatorPosition = testCharPosition;
}
else
{
testCharPosition = testCharPosition + 1;
}
}
Better code → less comments
// Establish a new account
if (accountType == AccountType.NewAccount)
{
…
}
/// <summary>
/// This class performs an important function.
/// </summary>
public class MyClass { }
<param name="name">description</param>
<seealso cref="TestClass.Main"/>
<exception cref="type">description</exception>
/// <summary>
/// The GetZero method. Always returns zero.
/// </summary>
/// <example>
/// This sample shows how to call the <see cref="GetZero"/> method.
/// <code>
/// class TestClass
/// {
/// static int Main()
/// {
/// return GetZero();
/// }
/// }
/// </code>
/// </example>
public static int GetZero()
{
return 0;
}