Cleaning up your files can be a daunting project. However, there are some easy ways to clean code. This is a list of smaller clean up tasks that can each be completed in a few minutes. Depending on your time constraints and the current state of your files, you can use this as a weekly, bi-weekly, or monthly checklist to ensure your code is readable and easier to maintain in the future.
Vertical Spacing Clean Up
Cleaning up your files’ vertical spacing is extremely mind-numbing, but it’s an easy way to make your files more presentable. All this entails is deciding how many line breaks should be between function declarations, conditional statement, HTML elements, etc. Then delete any extra line breaks or add breaks in where needed to differentiate between functionalities or sections of the file.
Tabs vs Spaces Clean Up
Do you use tabs? Do you use spaces? Your files probably have both! Do a search across your files and replace one with the other. (I prefer tabs, so my search & replace would look like the screenshot below.)
Delete Unused CSS Classes/IDs
This one takes a little more testing and diligence, but as your web projects progress, you will often end up having styles (or scripts referencing the classes/IDs) for elements that no longer exist in your markup. Take some time to comb through your files using a search across files/directories and find these unused lines of code to remove. Be sure to test these changes!
Standardize Commenting Format
There are so many ways to write comments. In PHP, CSS, JavaScript, the notation is //
for single-line comments and /* comment */
for multiple lines. Typically in PHP, they follow this format:
/**
* Comment
*/
For consistency’s sake, go through files and stick to a clear format for comments. For example, use DocBlock style (above) for function documentation and the single-line comment format sparingly where a variable declaration needs context. Try to remove any comments that don’t add anything meaningful to the code.
Remove Journal-Style Comments
If you have a version control program, you don’t have a need to keep comments like // Added 4/13/16 MM
. Version control tracks all your file changes, additions, and deletions. As long as you’re diligent in committing and pushing edits to your repositories, you don’t need journal comments. The same thing goes for commented-out code. It should not be on the master branch if it’s commented out. After you delete the commented-out code, you can always go back and reverse the hunk.
These ideas are largely impacted by my reading Clean Code. This book has changed my workflow and I highly suggest reading it.