Best Practices in Programming
There is more to programming than getting your program to produce the desired outcome. As projects scale, their maintainability, streamline-ability of processes, and code modularity take the center stage. Herein we have compiled a list of best practices and common mistakes in programming, which we will be reactively topping up throughout the semester.
Best Practices
- Keep Code Consistent: In your (or your company's) codebase, always use the agreed upon naming conventions(camelCase, snake_case), brace style (K&R, Allman), whitespace rules, blank line usage, etc. Doing so improves code consistency and therefore navigation.
-
Use Descriptive Variable Names: The purpose of a variable should always become apparent from its name. It is best to avoid naming (global) variables something generic (
data,value,stuff), single letter (a,b,f), ambiguous or misleading (flag,status), and unprofessional names (asdaisdh,what). Using descriptive variable so improves code readability and comprehension. - Write Comments, Sparingly: If you will not be able to tell what the block of code you have written does in two-to-three years' time, you need to leave a few comments. The amount of comments should depend on the code block's complexity: a digital root may get away with none, but a fast inverse square root needs one for every line. Excessive use of comments is overwhelming, while not enough of them causes confusion. Find the right balance, and your codebase will be easier to work in.
-
Commit Regularly, Push When It Works: Make a habit of committing after implementing features* and pushing upon completion of all features. Nurturing this practice allows for progress tracking, enables per-feature debugging, and permits to restore earlier versions of your code.
*Your program should be in a compilable state. -
Be Descriptive In Commits: When committing, provide a descriptive commit message in the form of
<verb> <subject> [optional <details>]. Doing so simplifies code review.
Common Mistakes
-
Using "Magic Values": Try to avoid using magic values (values used in equations for unknown purposes) in your code, such as:
float offsetH = fmodf((0.24f * frameTime) * spacing, spacing);
Avoiding magic values improves code clarity.
To Be Extended...