home
products
contribute
download
documentation
forum
Home
Forums
New posts
Search forums
What's new
New posts
All posts
Latest activity
Members
Registered members
Current visitors
Donate
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Search titles only
By:
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
MediaPortal 1
Development
General Development (no feature request here!)
Mediaportal C# programming guidelines
Contact us
RSS
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Frodo" data-source="post: 6038" data-attributes="member: 10277"><p><strong>Part II</strong></p><p></p><p><strong>Programming Practices</strong></p><p>Visibility</p><p></p><p>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.</p><p></p><p>Use properties instead. You may use public static fields (or const) as an exception to this rule, but it should not be the rule.</p><p>9.2 No 'magic' Numbers</p><p></p><p><strong>Don’t use magic numbers</strong></p><p>, 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 :</p><p></p><p>[code]</p><p>public class MyMath</p><p>{</p><p> public const double PI = 3.14159...</p><p>}</p><p>[/code]</p><p></p><p><strong>Brace code example</strong></p><p>[code]</p><p>namespace ShowMeTheBracket</p><p>{</p><p> public enum Test </p><p> {</p><p> TestMe,</p><p> TestYou</p><p> }</p><p></p><p> public class TestMeClass</p><p> {</p><p> Test test;</p><p> public Test Test </p><p> {</p><p> get </p><p> {</p><p> return test;</p><p> }</p><p> set </p><p> {</p><p> test = value;</p><p> }</p><p> }</p><p></p><p> void DoSomething()</p><p> {</p><p> if (test == Test.TestMe) </p><p> {</p><p> //...stuff gets done</p><p> } </p><p> else </p><p> {</p><p> //...other stuff gets done</p><p> }</p><p> }</p><p> }</p><p>}</p><p>[/code]</p><p></p><p><strong>Variable naming example</strong></p><p></p><p>[code]</p><p>for (int primeCandidate = 1; primeCandidate < num; ++primeCandidate)</p><p>{</p><p> isPrime[primeCandidate] = true;</p><p>}</p><p>for (int factor = 2; factor < num / 2; ++factor) </p><p>{</p><p> int factorableNumber = factor + factor;</p><p> while (factorableNumber <= num) </p><p> {</p><p> isPrime[factorableNumber] = false;</p><p> factorableNumber += factor;</p><p> }</p><p>}</p><p>for (int primeCandidate = 0; primeCandidate < num; ++primeCandidate)</p><p>{</p><p> if (isPrime[primeCandidate]) </p><p> {</p><p> Console.WriteLine(primeCandidate + " is prime.");</p><p> }</p><p>}</p><p>[/code]</p></blockquote><p></p>
[QUOTE="Frodo, post: 6038, member: 10277"] [b]Part II[/b] [b]Programming Practices[/b] 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 [b]Don’t use magic numbers[/b] , 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] [b]Brace code example[/b] [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] [b]Variable naming example[/b] [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] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
MediaPortal 1
Development
General Development (no feature request here!)
Mediaportal C# programming guidelines
Contact us
RSS
Top
Bottom