Difference between revisions of "Best Coding Practices"

From Coder Merlin
Line 1: Line 1:
{{BestPractice|261|
{{BestPractice|249|
Flowcharts are your friend}}
Flowcharts are your friend}}


Line 28: Line 28:
}}
}}


* No global variables
{{BestPractice|057|
* Appropriate capitalization
No global variables
* Appropriate and descriptive variable names
* Global variables are evil
** ''nouns'' are usually most appropriate
}}
* Appropriate and descriptive function names
 
** ''verbs'' are usually most appropriate
{{BestPractice|207|
* Appropriate use of functions
Always use appropriate and descriptive variable names
** In general, avoid mid-function exits
* ''nouns'' are usually most appropriate
* Avoid repetition
}}
** DRY: Do Not Repeat Yourself
 
** DIE: Duplication is Evil
{{BestPractice|208|
* Appropriate scoping
Always use appropriate and descriptive function names
** Scope is no wider than absolutely necessary
* ''verbs'' are usually most appropriate
* Appropriate loops
}}
** Correct loop type
 
** In general, avoid mid-loop exits
{{BestPractice|394|
* Appropriate indentation
Orderly exits from functions
* In general, avoid mid-function exits
}}
 
{{BestPractice|444|
Avoid repetition
* DRY: Do Not Repeat Yourself
* DIE: Duplication is Evil
}}
 
{{BestPractice|502|
Scope appropriately
* Scope is no wider than absolutely necessary
}}
 
{{BestPractice|523|
Loop appropriately
* Correct loop type
* In general, avoid mid-loop exits
}}
 
{{BestPractice|029|
Always use appropriate indentation
* Helpful whitespace
* Helpful whitespace
* Appropriate placement of closing braces and parentheses
* Appropriate placement of closing braces and parentheses
}}
{{BestPractice|074|
Use comments to advantage others
* Deliberate and helpful comments
* Deliberate and helpful comments
** Avoid "obvious" comments
* Avoid "obvious" comments
}}
 
{{BestPractice|617|
Nest appropriately
* Avoid deep nesting
* Avoid deep nesting
* Avoid inappropriate nesting of functions
* Avoid inappropriate nesting of functions
* Limit line length
* Indent correctly according to nested level
* Appropriate file organization
}}
** Generally one class per file
 
* Code is clear and concise
{{BestPractice|831|
Limit line length
}}
 
{{BestPractice|907|
Organize files appropriately
* Generally one class per file
}}
 
{{BestPractice|947|
Hardcode only when there is no other choice
* All other values should be:
** calculated, or
** retrieved from configuration
}}


== Exercises ==
== Exercises ==

Revision as of 18:17, 13 November 2022

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Best PracticeHintIcon.png
👍 #249

Flowcharts are your friend


Best PracticeHintIcon.png
👍 #657

Use appropriate capitalization

  • Names of types and protocols are UpperCamelCase (Pascal case)
  • Everything else is lowerCamelCase


Best PracticeHintIcon.png
👍 #359

Clarity is more important than brevity


Best PracticeHintIcon.png
👍 #563

Preconditions are your friend

  • Include sufficient information for the message to be useful


Best PracticeHintIcon.png
👍 #174

Great functions exhibit these three vital properties:

  • Easy to read and comprehend
  • Easy to debug
  • Easy to modify to solve a variation of the original task

[1]

Best PracticeHintIcon.png
👍 #057

No global variables

  • Global variables are evil
Best PracticeHintIcon.png
👍 #207

Always use appropriate and descriptive variable names

  • nouns are usually most appropriate
Best PracticeHintIcon.png
👍 #208

Always use appropriate and descriptive function names

  • verbs are usually most appropriate
Best PracticeHintIcon.png
👍 #394

Orderly exits from functions

  • In general, avoid mid-function exits
Best PracticeHintIcon.png
👍 #444

Avoid repetition

  • DRY: Do Not Repeat Yourself
  • DIE: Duplication is Evil
Best PracticeHintIcon.png
👍 #502

Scope appropriately

  • Scope is no wider than absolutely necessary
Best PracticeHintIcon.png
👍 #523

Loop appropriately

  • Correct loop type
  • In general, avoid mid-loop exits
Best PracticeHintIcon.png
👍 #029

Always use appropriate indentation

  • Helpful whitespace
  • Appropriate placement of closing braces and parentheses
Best PracticeHintIcon.png
👍 #074

Use comments to advantage others

  • Deliberate and helpful comments
  • Avoid "obvious" comments
Best PracticeHintIcon.png
👍 #617

Nest appropriately

  • Avoid deep nesting
  • Avoid inappropriate nesting of functions
  • Indent correctly according to nested level
Best PracticeHintIcon.png
👍 #831

Limit line length

Best PracticeHintIcon.png
👍 #907

Organize files appropriately

  • Generally one class per file
Best PracticeHintIcon.png
👍 #947

Hardcode only when there is no other choice

  • All other values should be:
    • calculated, or
    • retrieved from configuration

Exercises[edit]

ExercisesExercisesIcon.png
  •  M1297-28  Complete  Merlin Mission Manager  Mission M1297-28.


References[edit]