Comments, naming conventions and white space have been taught to us ever since we began studying CS at Oregon State. Good coding habits have been a subject of every class, usually in the form of a course page describing code style expectations, just like an English class might describe what citation style to use. While I am good at reading those pages and writing out my comments, there are some more time-consuming habits I need to apply.
DRY is an acronym commonly used in this subject, and it stands for Don’t Repeat Yourself. In practice this involves writing reusable code. If there is a common operation in your code (or even a common value), it is important to create functions to perform this operation instead of writing it many times throughout your code (https://medium.com/@josueparra2892/20-best-programming-practices-407df688b96e item #3).
I recently violated this principle horribly in my Programming Language Fundamentals class. Because each assignment in this class asks the student to write a program in a different, obscure language, code cleanliness is a lower priority as I was just trying to get the foreign code to work. Before I submitted my assignment, I noticed about a dozen repeating lines throughout the 100 or so lines of code. I had taken an iterative approach to writing code, when I could have planned much better to organize my code into reusable chunks. This is a habit that I practice often, but even after a few years of coding I still slip.
While I write decently organized code with understandable variable names and comments, what don’t I do? While reading through “15 Bad coding practices you should avoid,” I was reminded about a gap in my skills: Error Handling. So much of the code I write is for academia, and therefore there are most often rubrics and automated tests in Gradescope with which I can evaluate my code. However, I am aware that moving into a career Software Development, it is important to bulletproof your code against edge-cases and unexpected errors. This means that instead of your program crashing, it will be able to handle these errors gracefully and provide a user with useful information to help them correct their actions (https://codedamn.com/news/programming/bad-coding-practices-examples-you-should-avoid line #5). Unit tests are useful for testing many possible inputs in a program and can help identify gaps in the software.