Winter – Blog 1


Clean Code

I would like to start keeping functions more easier to read, better commented, and shorter. I can think of many times where I have these long functions that are written out, that eventually do what they are supposed to, but are long and sometimes hard to read. We can see in this example below of a function that loads data from a url. 

Splitting up this function into smaller one and done tasks is a great way to increase readability and maintainability of said code. Looking at the beginning of the load_data function we could start by splitting up the download and unzip actions of the function.

Seen below we now have the download action of load_data() into its own single task function.

We could continue on with splitting load_data() into functions into smaller single task functions but you get the idea right? In the end, splitting up large functions like this example is an easy thing to do, and something that I will definitely start practicing!

DATA DUMps

One of the code smell examples was data clumping. Data clumping appears to be a common mistake, I sure know I do this! Clumping is when there are multiple method calls that access the same set of parameters or when related data is grouped together. An example of this code smell can be seen below.

Here we have an example of an object that represents a booking aspect of a hotel. There are two different time parameters that are needed for the complete booking time. Splitting these two parameters away from the booking class and into a class of their own would make the code easier to read and maintain.

Creating this new class for the time frame gives us a new entity that interacts with the booking class. The below “polished” example definitely provides us with a cleaner and more readable section of code!

I can already think of times that I’ve had overcrowded clumps of data throughout my code. So much that when trying to troubleshoot or read through my code becomes a tedious and confusing process. So remembering what I have learned today I hope this helps me to write cleaner, more readable, and less clumpy code.

Good luck out there!

Brahm

Sources – 

https://www.pluralsight.com/blog/software-development/10-steps-to-clean-code

https://www.servicenow.com/community/developer-articles/code-smells-refactoring-data-clump

s-including-parameter-lists/ta-p/2320891

Splitting functions up example – 

https://towardsdatascience.com/python-clean-code-6-best-practices-to-make-your-python-functions-more-readable-7ea4c6171d60

Data clump code smell example –

https://blog.jetbrains.com/dotnet/2018/07/02/join-data-items-want-go-together-code-smells-series/

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *