Best Coding Practices

From Coder Merlin
Revision as of 17:27, 17 February 2023 by Wenxi-chu (talk | contribs)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Introduction to General Coding Standards

Coding standards are the rules and techniques for writing clean, more readable, and maintainable code in a programming language.

Think of it like casting a spell inscribed on a magic book. The book represents a style guide on formatting the code to make sure it is efficient and free of errors. The spell is the operation being executed by the interpreter according to a set of instructions. Test it out for yourself by right-clicking on β€œinspect” to catch a glimpse of the markup on a webpage.

While different industries have their own standards, there are universal rules for documenting code you should follow to preserve its integrity and scalability. When learning to code, the key concepts are naming, commenting, indenting, debugging, scoping, and testing.

What are the most important coding conventions?

Best PracticeHintIcon.png
πŸ‘ #249

Flowcharts depict the information flow of an algorithm. For planning multi-step functions, flowcharts are your friend.

Best PracticeHintIcon.png
πŸ‘ #657

Use the appropriate capitalization on names:

  • Types and protocols (Ex: classes, objects) are UpperCamelCase (Pascal case)
  • Everything else is lowerCamelCase (Ex: variable, function, method)
Best PracticeHintIcon.png
πŸ‘ #359

For code readability, clarity is more important than brevity

  • Avoid long lines at all costs
  • Can someone else understand what it does?
Best PracticeHintIcon.png
πŸ‘ #563

Preconditions are your friend:

  • They assert the state of a program before an execution
  • Include sufficient information for the message to be useful
Best PracticeHintIcon.png
πŸ‘ #174

Great functions exhibit three vital properties:

  • They are easy to read and comprehend
  • They are easy to debug if something goes wrong
  • They are easy to modify for solving a variation of the original task[1]
Best PracticeHintIcon.png
πŸ‘ #057

Don’t declare variables outside of functions

  • Global variables are evil: They cause unintentional changes to the program.
Best PracticeHintIcon.png
πŸ‘ #207

Always use appropriate and descriptive variable names

  • Nouns are usually the most appropriate for defining variables
Best PracticeHintIcon.png
πŸ‘ #208

Always use appropriate and descriptive function names

  • verbs are usually the most appropriate for defining functions
Best PracticeHintIcon.png
πŸ‘ #394

Exiting from a function in order

  • In general, don’t exit a function in the middle
  • Only return a function value when the input satisfies a condition
Best PracticeHintIcon.png
πŸ‘ #444
Best PracticeHintIcon.png
πŸ‘ #502

Determine the appropriate scope

  • The scope should not be wider than absolutely necessary
  • Think about the context of your coding project
  • Narrow it down into specific goals
Best PracticeHintIcon.png
πŸ‘ #523

Select the appropriate loop (for loop, while loop) to iterate over a sequence or run a block of code repeatedly. Avoid breaking out of a loop midway.

Best PracticeHintIcon.png
πŸ‘ #029

Always use the appropriate indentation

  • White space is helpful for organizing blocks of code
  • Either 2 or 4 spaces are preferable
  • Use the proper closing braces and parentheses to contain multiple statements
Best PracticeHintIcon.png
πŸ‘ #074

Use comments to assist others in updating the code

  • Don’t leave them in the dark about how it functions
  • Deliberate and helpful comments are ideal for explaining the logic
  • Avoid comments stating the obvious
Best PracticeHintIcon.png
πŸ‘ #617

Add the appropriate nesting to keep errors at bay

  • Avoid deep nesting or else the code will get buried
  • Avoid inappropriate nesting of functions that may cause confusion
  • Indent correctly according to the level of nesting
Best PracticeHintIcon.png
πŸ‘ #831

Limit the horizontal line length. Vertical lines are easier to scan.

Best PracticeHintIcon.png
πŸ‘ #907

Organize the class files appropriately. Generally, one class per file is sufficient.

Best PracticeHintIcon.png
πŸ‘ #947

Hardcode only when there is no other choice

  • All other values should be one of the following:
    • Calculated in advance
    • Retrieved from configuration
Best PracticeHintIcon.png
πŸ‘ #959

Do not place extraneous files into the source control

  • Exclude all build artifacts
  • Exclude all "backup" files (e.g. main.swift~)

Exercises[edit]

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


References[edit]