CS Fundamentals
Learn essential coding best practices — writing clean, readable, maintainable code that others (and your future self) can understand and build upon.
Introduction
Here is a truth that surprises many beginner programmers: writing code that works is only half the job. The other half — equally important — is writing code that humans can read and understand. You might think, "But code is for computers to execute, right?" Yes, but code is read far more often than it is written. Other developers need to understand your code to fix bugs, add features, or learn from it. And the person most likely to read your code six months from now — and be completely confused by it — is you.
Coding best practices are guidelines that help you write code that is not just functional, but clear, maintainable, and professional. Following these practices from the very beginning of your programming journey will make you a significantly better developer and a much more valuable team member.
Meaningful Names
The single most impactful best practice is choosing good names for variables, functions, and classes. A name should clearly communicate what the thing is or what it does, without needing a comment to explain.
Bad naming: x = 5, temp = calculate(a, b), func1(), data, result. These names tell you nothing about purpose. Reading code full of such names is like reading a novel where every character is named "Person A," "Person B," "Person C."
Good naming: student_age = 5, total_marks = calculate_sum(english_marks, math_marks), calculate_average(), student_records, formatted_report. These names immediately tell you what the variable holds or what the function does.
Rules for naming: use descriptive names that reveal intent, use consistent naming conventions (snake_case in Python, camelCase in JavaScript), avoid single-letter variables except in very short loops, name functions with verbs (calculate_total, send_email, validate_input), and name variables with nouns (student_name, file_path, error_message).
Keep Functions Small and Focused
Each function should do exactly one thing and do it well. If you can describe what a function does and you need to use the word "and," it probably does too much and should be split into two functions. A function called "validate_and_save_user_data" should probably be two functions: "validate_user_data" and "save_user_data."
Small functions are easier to understand (you can read them in a few seconds), easier to test (fewer paths to check), easier to reuse (specific functions are more reusable than monolithic ones), and easier to name (a function that does one thing is easy to name clearly).
As a guideline, if a function is longer than about 20-30 lines, consider whether it can be broken into smaller helper functions. This is not a hard rule, but it is a useful indicator.
Comments — When and How
Good comments explain why, not what. If your code needs a comment to explain what it does, the code itself is probably unclear and should be rewritten with better names and structure. But comments explaining why a decision was made, why a non-obvious approach was chosen, or what business rule a piece of logic implements are valuable.
Bad comment: i = i + 1 // increment i by 1 — this tells you nothing you cannot already see from the code.
Good comment: // Use binary search instead of linear because the dataset exceeds 10 million records — this explains a decision that is not obvious from the code alone.
Do not write comments that will become outdated when code changes. A comment that contradicts the code is worse than no comment at all because it actively misleads. If you change code, update or remove associated comments.
Indentation and Formatting
Consistent formatting makes code readable at a glance. Use consistent indentation (pick either spaces or tabs and stick with one throughout a project — most modern teams use 4 spaces). Keep line lengths reasonable (under 80-100 characters). Use blank lines to separate logical groups of code. Align similar structures for visual clarity.
Most modern code editors and IDEs have automatic formatting features — use them. Tools like Prettier (JavaScript), Black (Python), and clang-format (C/C++) automatically format your code to consistent standards. Configure them and let them do the work.
Avoid Magic Numbers
A "magic number" is a numeric literal in code with no obvious meaning. For example: if (score > 33) — what is 33? Is it a passing percentage? A threshold? Without context, readers have to guess.
Instead, define a named constant: PASSING_SCORE = 33, then write: if (score > PASSING_SCORE). Now the code is self-documenting — anyone reading it immediately understands the intent without needing to know the specific value.
This applies to strings too. Instead of writing if (status == "A"), define ACTIVE_STATUS = "A" and write if (status == ACTIVE_STATUS).
Error Handling
Good code anticipates what can go wrong and handles it gracefully. When reading a file, what if the file does not exist? When making a network request, what if the server is unreachable? When dividing numbers, what if the divisor is zero?
Never ignore errors silently. At minimum, log the error so that when something goes wrong in production, you can diagnose it. Provide helpful error messages that tell the user what happened and what they can do about it. Handle errors at the appropriate level — catch specific exceptions, not generic "catch everything" blocks.
Do Not Repeat Yourself (DRY)
If you find yourself copying and pasting the same code in multiple places, extract it into a function and call that function from each location. Duplicated code means that when you fix a bug or add a feature, you need to make the change in multiple places — and you will inevitably forget one, creating inconsistent behavior.
However, do not over-abstract too early. If two pieces of code look similar but serve fundamentally different purposes, forcing them into a shared function can make the code harder to understand and maintain. The rule is: if code is duplicated and serves the same purpose, extract it; if it just looks similar but serves different goals, leave it separate.
Version Control
Use a version control system (Git) from day one of any project, no matter how small. Version control lets you track every change you make, revert mistakes, work on experimental features without breaking the main code, and collaborate with others without conflicts.
Commit frequently with clear, descriptive commit messages. Each commit should represent one logical change. A message like "Fixed login validation bug where empty passwords were accepted" is infinitely more useful than "Fixed stuff" or "Updates."
Testing Your Code
Untested code is broken code — you just do not know it yet. Write tests for your functions that verify they produce correct output for known inputs. Test normal cases (typical input), edge cases (empty input, maximum values, minimum values), and error cases (invalid input).
Even simple tests catch bugs early, before they reach users. Testing also gives you confidence to change code — if tests still pass after your modification, you know you did not break existing functionality.
Key Takeaways
- Code is read far more often than it is written — prioritize readability
- Use descriptive, meaningful names for all variables, functions, and classes
- Keep functions small — each should do one thing well
- Comments should explain why, not what — let the code speak for itself
- Be consistent with formatting and use automatic formatting tools
- Replace magic numbers with named constants
- Handle errors gracefully — never silently ignore them
- Follow DRY — extract duplicated code into reusable functions
- Use version control (Git) from the very beginning of every project
- Write tests — they catch bugs early and give you confidence to change code
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Coding Best Practices.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Computer Fundamentals topic.
Search Terms
computer-fundamentals, computer fundamentals, computer, fundamentals, programming, coding, best, practices
Related Computer Fundamentals Topics