Good Programming Practices, Writing Clean Code, and Winning Over Your Teammates
Are we losing the art of writing clean code? Programming is not just logic, it is an art form. Regardless of the industry we are coding in, code is often maintained and reused by multiple programmers. Heck, even your future self, 6 months down the line, might be left scratching your head wondering what you were thinking during development. I know I've written code (with comments) that probably made sense at the time, but struggled to re-interpret months or years later, ... guilty!
Code is hard to read and digest. It's even harder when we were not the original developers of the code and there are no comments. I don't know many programmers that enjoy taking over programs from others. If I had to guess, its likely due to the pain and agony of reading, deciphering, and understanding another's program. If we dig deeper, it is often because the code does not adhere to good programming practices. I work in the pharmaceutical industry where we write data set and output programs to create clinical trial summary reports. We often reuse programs from other clinical studies that were carried out in a similar manner or have collected similar data. Adhering to good programming practices is important and this article is a reminder to myself, as I've notice my own programming practices starting to slip.
For the pharmaceutical industry, Phuse has published a Good Programming Practices guidance here, https://www.phusewiki.org/wiki/images/d/d4/GPP_Guidance_Document_v1.1.docx
If I had to pick one above all others, it is the comments. Modularizing a program with section comments breaks up the code into smaller digestible segments. For example:
*************************************************; Reading in Source Data *************************************************; proc sql; ... run; *************************************************; Summarizing Data / Statistical Modeling *************************************************; proc means data=adsl; ... run; proc freq data=adsl; ... run; *************************************************; Combine Results *************************************************; data final; ... run; *************************************************; Produce Output File *************************************************; proc report data=final missing split="|"; ... run; *************************************************; End Program *************************************************;
It can greatly cut down the time one needs to locate the piece of code that needs updating. Adding additional, single line comments, for each PROC, DATA step, and complex code provides even more direction and guidance for the consumer of our programs.
When we write clean code, it is not just for us and the task at hand, it is for the person reusing and updating our code (or even your future self). We give that person confidence that we care about our art form (our coding craft). Not only does that signal we did our job to the best of our ability, but that we also cared enough about our teammates adopting and reusing our programs. I don't know about you, but that's someone I would like to work alongside.
I'd be interested to hear in the LinkedIn comments, what is your most admired good programming practice.
Agree - you’re spot-on. Portability, robustness, and longer-term/widespread usefulness need to be integral to code development since we can’t know who will be reviewing it, using it, and attempting to revise it in the future. Thanks
Absolutely spot on! For code that is not straight forward, a comment should encapsulate the code block with what it is intending to do, specifying the overall flow/steps along with the expected results. In-line comments help but don't give out the big picture. Completely agree with Pablo's comment👍
So I have a follow up to this one... Lets agree - we agree on the value of "clean code". I think a component of the hurdle is how a "team" conducts its Code Review and Work Flow. If teams devolve into a "criticism fest" - no one will find the CR process useful and it spells for trouble. Bad code will become code debt. A healthy code ecosystem is part of a healthy CR team process. One which the team strives to ensure the "quality" of the code being approved is met. And not bash the author. Just this week a conversation came up about this and the team in question admitted that its CR process is in disarray - and hadnt agreed why. The "standards" in shambles. Arguments crept up about which language/syntax was the driver of standards. I'm sorry - but if we focus simply one "the code" and not on the whole process - it is going to be tough. Its a matter of a healthy balanced approach. Code, Code Review, Minimum Standards, Syntax Agnostic Factors. One rule i've found myself preaching - "the person with the LEAST knowledge of a syntax - should be the important invitation to CR" - because his or her ability to grasp what the code is DOING - matters. If they approve the code - THEY will be responsible for maintaining it or refactoring it long after the author is GONE or done with it. But imagine if this person stops an approval. That is an important CR denial. It likely speaks to a code issue that needs solving. Could be comments, logic, business rule that doesnt make sense. Who knows. As teams rush to diversify in Syntax (py, r, sas, spss) - and argue about "who should" be on "whos" CR - ponder the strength in "the one with the LEAST knowledge of the syntax matters most". The cross pollination of that logic into more than one syntax. The true questions about "why is this..." that will come up from that dynamic. To me healthy code is part of a healthy team attitude. Have a great weekend everyone! Code great things! Ideally as a team!
Right on Chris!! You should be able to stand six feet away from a program and see the basic structure through proper one line statements and indenting. There are compelling reasons to break the rules every now and then but one of my betes noire is using the same dataset name in succession or names like dst1, etc. It takes seconds to write a descriptive name and can take hours to suss out a "worked for me in the moment" program--we've all been through those.
Zen of code comments- every comment you write is a failure, good code cannot be written without comments.