Difference between revisions of "Best Coding Practices"
Line 1: | Line 1: | ||
=== ''' | === '''What are the 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. | |||
{{BestPractice|249| | {{BestPractice|249| |
Revision as of 16:55, 17 February 2023
What are the 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 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.
Flowcharts are your friend
Use appropriate capitalization
- Names of types and protocols are UpperCamelCase (Pascal case)
- Everything else is lowerCamelCase
Clarity is more important than brevity
Preconditions are your friend
- Include sufficient information for the message to be useful
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]
No global variables
- Global variables are evil
Always use appropriate and descriptive variable names
- nouns are usually most appropriate
Always use appropriate and descriptive function names
- verbs are usually most appropriate
Orderly exits from functions
- In general, avoid mid-function exits
Avoid repetition
- DRY: Do Not Repeat Yourself
- DIE: Duplication is Evil
Scope appropriately
- Scope is no wider than absolutely necessary
Loop appropriately
- Correct loop type
- In general, avoid mid-loop exits
Always use appropriate indentation
- Helpful white space
- Appropriate placement of closing braces and parentheses
Use comments to advantage others
- Deliberate and helpful comments
- Avoid "obvious" comments
Nest appropriately
- Avoid deep nesting
- Avoid inappropriate nesting of functions
- Indent correctly according to nested level
Limit line length
Organize files appropriately
- Generally one class per file
Hardcode only when there is no other choice
- All other values should be one of the following:
- Calculated
- Retrieved from configuration
Do not place extraneous files into source control
- Exclude all build artifacts
- Exclude any "backup" files (e.g. main.swift~)
Exercises[edit]
- M1297-28 Complete Merlin Mission Manager Mission M1297-28.
References[edit]
- API Design Guidelines (Swift Documentation)
- API Style Guidelines (Google Documentation)