Mod 2 Blog 1 Clean Code and Bad Code Smells.

I noticed in the first reading the author mentions a quote by Michael Feathers that code is written by someone that cares (Martin, 2008). When I am in a hurry to code something or to automate a calculation, I tend to not care when I write the code. I know how to write it. If there are errors, I’ll just patch it quickly and not care I might need to know the meaning later. I wrote a code snippet as an example of what code looks like when someone doesn’t care. This is not clean code.

I learned from the first article with all the opinion of clean code, one thing I am going to start doing is making my code easier to read and follow. I am going to do this even when it is something I am only going to see. In the following cleaned up code, it is easier to add code to it. The output is also clean and easier to understand.

I also want to avoid bad code smells. Refactoring is important in avoiding code smell. Refactoring is mentioned by Fowler in the book (Fowler, 2018). In the above code, if I had a lot of numbers to test for error checking, I would stop and take time to refactor the code now to automate the process so I am able to test all the numbers at once. This way I am not adding a bunch of seperate calls from main and adding to the function to fix errors and formatting. I should add a for loop in main and increment and test numbers instead of just randomly choosing numbers to test. This is the best chance to keep the code clean and avoid bad code smells.

Sources:

Martin, R. C. (2008). Clean Code: A Handbook of Agile Software Craftsmanship. Pearson. Retrieved January 10, 2024, from https://github.com/dev-marko/clean-code-book

Fowler, M. (2018). Refactoring: Improving the Design of Existing Code (2nd ed.). Addison-Wesley Professional. Retrieved January 10, 2024, from https://silab.fon.bg.ac.rs/wp-content/uploads/2016/10/Refactoring-Improving-the-Design-of-Existing-Code-Addison-Wesley-Professional-1999.pdf