Difference between revisions of "Best Coding Practices"

From Coder Merlin
m (Editorial review and minor corrections)
m (Editorial review and minor corrections)
 
(20 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==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 web page.
Although different industries have their own standards, you should follow universal rules for documenting code 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?==
{{BestPractice|249|
{{BestPractice|249|
Flowcharts are your friend}}
Flowcharts depict the information flow of an algorithm. For planning multi-step functions, flowcharts are your friend.}}


{{BestPractice|657|
{{BestPractice|657|
Use appropriate capitalization
Use the appropriate capitalization on names:
* Names of ''types'' and ''protocols'' are UpperCamelCase (Pascal case)
* ''Types'' and ''protocols'' (for example, classes, objects) are UpperCamelCase (Pascal case)
* ''Everything else'' is lowerCamelCase
* ''Everything else'' is lowerCamelCase (for example, variable, function, method)
}}
}}


{{BestPractice|359|
{{BestPractice|359|
Clarity is more important than brevity
For code readability, clarity is more important than brevity:
* Avoid long lines at all costs.
* Can someone else understand what it does?
}}
}}


{{BestPractice|563|
{{BestPractice|563|
Preconditions are your friend
Preconditions are your friend:
* Include sufficient information for the ''message'' to be useful
* They assert the state of a program before an execution.
* Include sufficient information for the ''message'' to be useful.
}}
}}


{{BestPractice|174|
{{BestPractice|174|
Great functions exhibit these three vital properties:
Great functions exhibit three vital properties:
* Easy to read and comprehend
* They are easy to read and comprehend.
* Easy to debug
* They are easy to debug if something goes wrong.
* Easy to modify to solve a variation of the original task<ref>https://www.cs.mtsu.edu/~untch/karel/functions.html</ref>
* They are easy to modify for solving a variation of the original task<ref>https://www.cs.mtsu.edu/~untch/karel/functions.html</ref>.
}}
}}


{{BestPractice|057|
{{BestPractice|057|
No global variables
Don't declare variables outside of functions:
* Global variables are evil
* Global variables are evil. They cause unintentional changes to the program.
}}
}}


{{BestPractice|207|
{{BestPractice|207|
Always use appropriate and descriptive variable names
Always use appropriate and descriptive variable names:
* ''nouns'' are usually most appropriate
* ''Nouns'' are usually the most appropriate for defining variables.
}}
}}


{{BestPractice|208|
{{BestPractice|208|
Always use appropriate and descriptive function names
Always use appropriate and descriptive function names:
* ''verbs'' are usually most appropriate
* ''Verbs'' are usually the most appropriate for defining functions.
}}
}}


{{BestPractice|394|
{{BestPractice|394|
Orderly exits from functions
Exiting from a function in order:
* In general, avoid mid-function exits
* In general, don't exit a function in the middle.
* Return a function value only when the input satisfies a condition.
}}
}}


{{BestPractice|444|
{{BestPractice|444|
Avoid repetition
Avoid repetition whenever possible:
* DRY: Do Not Repeat Yourself
* '''DRY''' - Do Not Repeat Yourself
* DIE: Duplication is Evil
* '''DIE''' - Duplication is Evil
* Abstract duplicate code into a function
}}
}}


{{BestPractice|502|
{{BestPractice|502|
Scope appropriately
Determine the appropriate scope:
* Scope is no wider than absolutely necessary
* The scope should not be wider than absolutely necessary.
* Think about the context of your coding project.
* Narrow it down into specific goals.
}}
}}


{{BestPractice|523|
{{BestPractice|523|
Loop appropriately
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.
* Correct loop type
* In general, avoid mid-loop exits
}}
}}


{{BestPractice|029|
{{BestPractice|029|
Always use appropriate indentation
Always use the appropriate indentation:
* Helpful white space
* White space is helpful for organizing blocks of code.
* Appropriate placement of closing braces and parentheses
* Either 2 or 4 spaces are preferable.
* Use the proper closing braces and parentheses to contain multiple statements.
}}
}}


{{BestPractice|074|
{{BestPractice|074|
Use comments to advantage others
Use comments to help others update the code:
* Deliberate and helpful comments
* Don't leave them in the dark about how it functions.
* Avoid "obvious" comments
* Deliberate and helpful comments are ideal for explaining the logic.
* Avoid comments stating the ''obvious''.
}}
}}


{{BestPractice|617|
{{BestPractice|617|
Nest appropriately
Add the appropriate nesting to keep errors at bay:
* Avoid deep nesting
* Avoid deep nesting or the code will get buried.
* Avoid inappropriate nesting of functions
* Avoid inappropriate nesting of functions that could cause confusion.
* Indent correctly according to nested level
* Indent correctly according to the level of nesting.
}}
}}


{{BestPractice|831|
{{BestPractice|831|
Limit line length
Limit the horizontal line length. Vertical lines are easier to scan.
}}
}}


{{BestPractice|907|
{{BestPractice|907|
Organize files appropriately
Organize the class files appropriately. Generally, one class per file is sufficient.
* Generally one class per file
}}
}}


{{BestPractice|947|
{{BestPractice|947|
Hardcode only when there is no other choice
Hardcode only when there is no other choice:
* All other values should be one of the following:
* All other values should be one of the following:
** Calculated
** Calculated in advance
** Retrieved from configuration
** Retrieved from configuration
}}
}}


{{BestPractice|959|
{{BestPractice|959|
Do not place extraneous files into source control
Do not place extraneous files into the source control:
* Exclude all build artifacts
* Exclude all build artifacts.
* Exclude any "backup" files (e.g. main.swift~)
* Exclude all "backup" files (e.g., main.swift~).
}}
 
{{BestPractice|655|
Clearly define issues in the Project Management System:
* '''Specificity''': Ensure that the issue is well-defined and specific so it is clear to developers what needs to be done
* '''Context''': Ensure that the issue includes sufficient context (such as error messages, logs, stack traces, screen shots, sketches) so that developers can find, diagnose, and resolve the issue
* '''Testability''': Ensure that the issue can be tested so that QA personnel can verify its resolution
* '''Prioritization''': Ensure that the issue is prioritized correctly based on its effect, severity, and urgency so that resources are used effectively
}}
}}
When the best coding practices are followed, you'll be able to create uniform code that other engineers and developers can reuse sharing the same codebase. The last step is to test your code for unusual cases, so it can handle exceptions quickly and go over the rest of the syntax.
If you choose to input hundreds of commands from a script, the computer needs to understand every line before it can carry out the desired tasks. This includes actions like uploading a PDF document or displaying a user's chat history.
Learning to write cleaner code is an essential skill to master for every aspiring developer.


== Exercises ==
== Exercises ==
Line 111: Line 141:
* [https://swift.org/documentation/api-design-guidelines/ API Design Guidelines] (Swift Documentation)
* [https://swift.org/documentation/api-design-guidelines/ API Design Guidelines] (Swift Documentation)
* [https://google.github.io/swift/#apples-api-style-guidelines API Style Guidelines] (Google Documentation)
* [https://google.github.io/swift/#apples-api-style-guidelines API Style Guidelines] (Google Documentation)
* [https://www.educative.io/blog/coding-best-practices#comments 6 best practices for beginner programmers]
* [https://www.thinkful.com/blog/coding-best-practices/ Coding Best Practices] (Rules to format code)
<references/>
<references/>

Latest revision as of 16:03, 2 May 2023

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Introduction to General Coding Standards[edit]

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 web page.

Although different industries have their own standards, you should follow universal rules for documenting code 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?[edit]

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 (for example, classes, objects) are UpperCamelCase (Pascal case)
  • Everything else is lowerCamelCase (for example, 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.
  • Return a function value only when the input satisfies a condition.
Best PracticeHintIcon.png
👍 #444

Avoid repetition whenever possible:

  • DRY - Do Not Repeat Yourself
  • DIE - Duplication is Evil
  • Abstract duplicate code into a function
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 help others update 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 the code will get buried.
  • Avoid inappropriate nesting of functions that could 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~).
Best PracticeHintIcon.png
👍 #655

Clearly define issues in the Project Management System:

  • Specificity: Ensure that the issue is well-defined and specific so it is clear to developers what needs to be done
  • Context: Ensure that the issue includes sufficient context (such as error messages, logs, stack traces, screen shots, sketches) so that developers can find, diagnose, and resolve the issue
  • Testability: Ensure that the issue can be tested so that QA personnel can verify its resolution
  • Prioritization: Ensure that the issue is prioritized correctly based on its effect, severity, and urgency so that resources are used effectively

When the best coding practices are followed, you'll be able to create uniform code that other engineers and developers can reuse sharing the same codebase. The last step is to test your code for unusual cases, so it can handle exceptions quickly and go over the rest of the syntax.

If you choose to input hundreds of commands from a script, the computer needs to understand every line before it can carry out the desired tasks. This includes actions like uploading a PDF document or displaying a user's chat history.

Learning to write cleaner code is an essential skill to master for every aspiring developer.

Exercises[edit]

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


References[edit]