For this blog post assignment, I’ve read two articles — one which discusses clean code, and another that discusses common “code smells.”
Both articles were excellent. The article on common code smells from Pragmatic Ways seemed to be heavily inspired by the book The Pragmatic Programmer, which I also found to be very informative. While some ideas presented by these articles were review, others were new to me. In this post, I’ll discuss a couple of the concepts I’d like to apply in my code in the future.
One thing I’d like to do more often that was mentioned in both articles was limiting the number of parameters defined for a function. I sometimes find myself with unwieldy functions that, while only serving one person, accept 4 or more arguments. Instead, related arguments could be grouped together into an object to be passed in. See the following function signatures for examples:
def register_user(first_name: str, last_name: str, employment_date: datetime.date) -> None
vs.
def register_user(user_information: dict) -> None
The second example is easier to understand, less complex, and is also expandable in the future — what if the user’s position in the company needs to be added later? In the first case, the function signature and possibly any calls to it will need to be modified, but in the second only the data only needs to be added to the user_information dictionary.
One thing that I’d like to avoid are useless comments. I regularly find myself writing comments about “how” something works instead of “why” it exists. Clean code with clearly named variables, functions, classes, and methods will describe the “how” on their own. In the future, I’d like to avoid writing comments like this.