10 Tips for Writing Code Like a Pro

Unless you’re Bill Gates or Mark Zuckerberg, chances are there was (or still is) a point in your life where you were writing bad code. When I say bad code, I don’t mean code that won’t run, I mean code that is unreadable, repetitive, or just plain ugly. I know, I’ve been there– looking back at my code from my freshman year C++ class leaves me cringing every time.
The learning curve on how to program varies by person- some people are great at thinking in functions, some people only like to look at the bigger picture. Some people love writing documentation while others dread it. We are all different, which is why learning how to program comes from forming good habits and practicing.
My name is Emily, and I’m here to give you 10 tips that I’ve learned to help clean up your code and bring it up to industry standards.
- Writing comments/documentation
Comments that only say what the code does, but not why you chose to do it that way are not helpful
People who have to look over your code or collaborate with you need to understand your code. If the only comment you leave over a for loop is “this is a for loop that loops through the variables”, not only is that redundant, because everyone knows that a loop loops, the people reading still will have no clue why you chose that solution. Not only do other people need to be able to read your comments and understand, you may also need them to review your own code if you come back to it after a long time and can’t remember what you wrote!
Many languages have built in documentation aid for developers. Java has Javadocs, Python has Docstrings, etc. Look into each programming language you know to figure out how to make your documentation more readable and standardized
- Variable conventions
Knowing variable conventions is another step towards making your code readable for everyone, including yourself
Use standard naming conventions like camel case, Pascal case, snake case, upper case, etc.
Give your variables self-explanatory names. If you have a variable for the number of apples, don’t call it “x”, call it “numApples”. This will help others know exactly what the variable stands for, and avoid excessive commenting
Be careful when choosing whether to make a variable singular or plural. If you have an array or collection, it makes sense to use plural but be careful as always
- Style Guides
While this one may seem boring, it will definitely help you in the long run
Every programming language has conventions and style guides online, if you have one primary programming language that you use frequently I would suggest digging into some documentation and learning how to optimize your code for specific languages
There are tools you can find online to help you conform to certain coding styles
- Write code in small pieces
Breaking up your code and writing it incrementally can help improve your understanding of what you’re actually writing and doing in your program
Learning Agile and other incremental programming methods now will help you adjust to professional development standards
Writing code in smaller pieces will help with testing– it is easier to debug only one problem at a time within a smaller piece of code than to write the whole program and be faced with multiple errors
- Testing
This goes hand in hand with tip #4
Testing is extremely important as a developer– think about how it would feel to be a user who was given a faulty app, its frustrating
When you write tests, try to think of as many cases as you can where inputs can go wrong, and then think of ways to safeguard against that
Learn to use your IDE’s debugger tools, often it will help when you can’t find errors on your own
In addition to debuggers, many programming languages have functions built in to help with testing. When all else fails, inserting print statements to your code is a major help– you don’t have to be fancy with your testing, the important part is ensuring functionality across all functions within your code
- Robust Error Checking
This is important because with software, pretty much anything that can go wrong will go wrong
Input validation checks (try/catch blocks, for example) are important for handling unexpected input, whether on purpose or accidental
Learn about fuzzing, a technique that can help you catch unexpected input
- Beware of repetitive code
Sometimes it can be hard to realize when you’ve written a piece of code multiple times, but in doing so you are wasting time
Use ctrl + f to search for specific key word repetition if you suspect you may have written the same code more than once
Being able to write reusable functions is the mark of a professional developer– the less lines you have to write, the better!
- Learn about version control
Git and GitHub are your best friends when it comes to version control, there are command line and GUI options
They will help you track changes in your code, and backtrack in case you write a feature that does not work with the code you already have
Version control tools are a great resource when you code collaboratively on projects
Online repositories make your code more accessible, once again helping with team projects
- Review old code
While it may be embarrassing, reviewing your old code can help you become a better developer
Here are some questions to ask yourself while you review: Did I comment the code in a way where I can look back and understand it? Are there repetitive lines/functions I can refactor to make this code more efficient? Do I understand all of the variable names I used?
- Ask for feedback
It is always ok to ask for help!
Ask peers for their perspective, or ask someone with a little more experience like a professor or mentor
Keep an open mind, when people give you critiques don’t take them personally, use them to help you improve your code
Celebrate when you get compliments about solutions you chose, affirmation helps you know what you did right
Review others’ code as well, try to find solutions that are efficient and learn how to mimic that in your own projects
Conclusion
Being a great programmer takes practice, but hopefully with these tips you’ll be writing efficient, elegant solutions in no time!
Links for in depth explanations:
https://antonz.org/git-by-example/ (Guide to learning Git)
https://education.github.com/git-cheat-sheet-education.pdf (Git cheat sheet)
https://www.youtube.com/watch?v=ZpFwlwt7PNo (Commenting)
https://www.youtube.com/watch?v=aId-WLZnvkw (How to write unit tests)
https://youtu.be/vYI7-UD9tEQ?si=r1xauv_pSr-iPgE9 (Agile explanation video)

