As I approach the point in my professional development where my code is going to have to be presented, it’s best that I have my code prepared for the eyes of other people. This means having clean code, and as mentioned in my previous blog, I am guilty of not using functions to their fullest potential. To fix that, I will look into refactoring my code with the help of the blog Code Refactoring: Meaning, Benefits and Best Practices By Nuradil Alymkulov (https://maddevs.io/blog/code-refactoring/). Alymkulov covers refactoring in depth by explaining what refactoring is, why it’s important, how to refactor, when to refactor, different methods of refactoring, and best practices.
What is Refactoring and Why Should You Do It?
Writing code is much like writing an essay. Essays require going back and rereading the content to ensure it is written correctly and that it flows in an organized manner. When writing code the first attempt at solving a problem or adding a feature may be functional but, like an essay, could do with revisiting for organization and cleaning up. Refactoring is revisiting old code and making edits that do not alter functionality but, instead, are meant to help with readability and restructuring. Having readable code will significantly help in finding bugs. Doing this will encourage programmers to be more thoughtful with their future code, including these refactoring practices in the initial code.
What About My Code Needs Refactoring?
Code blocks should be easily visible, so a good place to start is proper indentation and formatting. Another large part of refactoring is removing unused or duplicate code. Renaming your variables and functions may make unused variables or duplicate code more noticeable. Variable and function names should make sense and form pieces of a sentence when put together. Reducing the size of each function will encourage developers to name functions in a way that helps form these readable segments of code. Once the code is restructured, a final clean up of comments that were needed in that past may be self explanatory due to the renaming of functions.
Refactoring Code Moving Forward
There have been countless projects that I would think about an additional feature I could add. Only to never revisit them due to the idea of having to reread my old code before I could even begin working. After learning the importance of refactoring code I can approach these projects with the mentality that not only will I clean up my code, but I will learn better practices for future code. Refactoring my old code will prepare it for future features and give me the tools to write better code.
From the blog CS@Worcester – CS Learning by kbourassa18 and used with permission of the author. All other rights reserved by the author.