Normal
Part IIProgramming PracticesVisibilityDo not make any instance or class variable public, make them private. For private members prefer not using “private” as modifier just do write nothing. Private is the default case and every C# programmer should be aware of it.Use properties instead. You may use public static fields (or const) as an exception to this rule, but it should not be the rule.9.2 No 'magic' NumbersDon’t use magic numbers, i.e. place constant numerical values directly into the source code. Replacing these later on in case of changes (say, your application can now handle 3540 users instead of the 427 hardcoded into your code in 50 lines scattered troughout your 25000 LOC) is error-prone and unproductive. Instead declare a const variable which contains the number :[code]public class MyMath{ public const double PI = 3.14159...}[/code]Brace code example[code]namespace ShowMeTheBracket{ public enum Test { TestMe, TestYou } public class TestMeClass { Test test; public Test Test { get { return test; } set { test = value; } } void DoSomething() { if (test == Test.TestMe) { //...stuff gets done } else { //...other stuff gets done } } }}[/code]Variable naming example[code]for (int 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 += factor; }}for (int primeCandidate = 0; primeCandidate < num; ++primeCandidate){ if (isPrime[primeCandidate]) { Console.WriteLine(primeCandidate + " is prime."); }}[/code]
Part II
Programming Practices
Visibility
Do not make any instance or class variable public, make them private. For private members prefer not using “private” as modifier just do write nothing. Private is the default case and every C# programmer should be aware of it.
Use properties instead. You may use public static fields (or const) as an exception to this rule, but it should not be the rule.
9.2 No 'magic' Numbers
Don’t use magic numbers
, i.e. place constant numerical values directly into the source code. Replacing these later on in case of changes (say, your application can now handle 3540 users instead of the 427 hardcoded into your code in 50 lines scattered troughout your 25000 LOC) is error-prone and unproductive. Instead declare a const variable which contains the number :
[code]
public class MyMath
{
public const double PI = 3.14159...
}
[/code]
Brace code example
namespace ShowMeTheBracket
public enum Test
TestMe,
TestYou
public class TestMeClass
Test test;
public Test Test
get
return test;
set
test = value;
void DoSomething()
if (test == Test.TestMe)
//...stuff gets done
else
//...other stuff gets done
Variable naming example
for (int 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 += factor;
for (int primeCandidate = 0; primeCandidate < num; ++primeCandidate)
if (isPrime[primeCandidate])
Console.WriteLine(primeCandidate + " is prime.");