Categories
Uncategorized

Proper Programming

This post is dedicated to my shortcomings in coding in the years I’ve been pursuing a degree in computer science but also issues I still have today. First I want to talk about writing smaller and more concise code when dealing with functions. The end goal of a function is that it does the function, as in the one task you have coded it to do. I still struggle with making functions only handle one specific task and then moving onto another function for it to communicate between each other. Reading Robert Martin’s Clean Code, he explains how each function, class, or module should have only one reason to change. The issue arises when a function is taking on too many tasks your code becomes hard to test and maintain. I completely agree with his opinion on this, it is way harder to find bugs and issues with your code when everything is shoved into one or two functions. Here is a simple code example of having too many tasks in a function,

This function has too many tasks to complete and should be split up into multiple functions which would make it easier to work on in the future. This is just a simple case but if we were to add more to it this would get a lot harder to test.

Code smells are small issues while looking at code that lead you to a deeper problem within the code. Martin Fowler talks about the “Long method” I gave an example above but this is more about how to spot it and these small problems will lead you to discover the larger issue at hand. The long method is basically if a function is doing too much or not much of anything, the reason for this is because when you split up your functions instead of making a long method then it you are less likely to run into bugs that are hidden within the one really long function. Both of these issues, code smells and writing clean code have a big emphasis on writing code that is clear and concise, it is already hard enough to read someone else’s code and when it is not organized it makes it harder for yourself to work on, and these code smells eventually lead to even worse bugs down the road that could take days to figure out. The first bigger program I made was in python and it was for a bowling game in the command line. I made the whole program in one function, that it and let me tell you it came back to haunt me. I was so close to finishing it and then I got this one bug where the game was not able to keep score, it took me 3 days to figure out what was going on , it was an awful experience. I still today have issues with trying to fit everything into a few functions because I just get in the zone and don’t stop but it just makes coding way more difficult down the line, and what has helped me is slowing down and checking if this is my intention for this function to do this task.

Leave a Reply

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