Often times someone will ask me to view some code that they are written and it turns out that it’s a total mess. Here’s a few tips to keep your code clean.

-
Whitespace doesn’t hurt.
When I write code, I tend to put in a lot of whitespace. I find that it makes the code much easier to read and overall it makes it look cleaner. Here’s an example:
<?php if($someCondition==true){ doSomething(); if($otherCondition==true){ doSomethingElse($someVariable); }else{ doSomethingElse($otherVariable); } }?>Compared to:
<?php if( $someCondition == true ) { doSomething(); if( $otherCondition == true ) { doSomethingElse( $someVariable ); } else { doSomethingElse( $otherVariable ); } } ?>(thanks to anon and Chris for helping me fix these examples!)
In my opinion, and probably the opinion of most people, the second one is much more pleasing to read. -
Be consistent.
If you choose to use whitespace in certain ways, do so through out each script. If you have a certain style of naming variables, functions, script names, or just about anything else, be consistent with that style.
-
Refine your logic.
Split your methods down into smaller methods whenever possible. There are many benefits to doing this. First, it’s much easier to debug when you have small methods with specific functions. Second, code becomes much cleaner. Third, code becomes more dynamic and easier to reuse.
-
Having a lot of separate scripts isn’t a bad thing.
There’s more to creating scripts than just writing code. Organization is also a key part. As I said earlier, it’s important that you be consistent when organizing your scripts. It is also good to know that having large scripts with a lot of good isn’t always a good thing. It doesn’t hurt to have small files including only one or two functions.
-
Comment when necessary.
The key part of this tip is “when necessary”. If you follow all of the above tips and you have good coding habits, you probably won’t need comments. It’s when you have huge functions or huge files when you start needing to comment.
Do you have any others ideas on how to keep code clean? Do you have any questions? Please leave a comment.
Check our Robert Martin’s “Clean Code” – it will be one of the best books you read this year.
In every Programming Language there is a coding standards and I’m sure if you follow those standards your code will be clean
@AllanC
Thanks for the suggestion! I’ll check it out.
@Kaiser
That is true. Not all languages have strict standards, though. This was focused on PHP which pretty much let’s you do what you want.
one thumb rule I follow is : “if you have to scroll the page to see the entire of your function, the your function needs to be refactored” – this helps me to keep my code clean as much as possible.
#2 has way too much white space.
You can use it to break up if blocks, but doing using it for every other line is just too much. There’s no sense of coherency in the code.
2-5 sound good.
But, to be honest, I find your whitespace example pretty horrific. The first example is easier to read, not the second. A blank line indicates a break on concepts, like the breaks between two paragraphs in a book. Adding extraneous blank lines like that just makes it seem like nothing is a complete thought in the code. It feels fractured. What do you do between “chunks” of code, put 3 or 4 blank lines?
And, if you’re a brace on EOL kind of guy, why have a blank line afterwards? The main point I’ve ever heard for EOL braces is saving space. I’m a brace on the next line guy for because I find the visual brace matchup helpful, but I understand the EOL guy’s point about saving space. It might not match how my brain parses the text on screen, but I get why they like EOL braces. Your style is the worst of both worlds: no space is saved AND branches don’t match up.
Also try ‘Code Complete’ Second Edition.
@anon and Chris
I don’t put line breaks between every line. If I had more code, it would look something like this:
<?php if( $someCondition == true ) { doSomething(); if( $otherCondition == true ) { doSomethingElse( $someVariable ); doSomething( $someVariable ); } else { doSomethingElse( $otherVariable ); doSomething( $someVariable ); } } ?>I use the line breaks to break things up and make it easier to see where blocks of code in the conditional statement start and end.
I can see what you guys are saying about having too much whitespace, though. When I made that example I wasn’t really thinking. I’m going to update it to display what I think more accurately.
I was trying to convince programmers that don’t use whitespace to use whitespace, not programmers that do use whitespace to use more of it. I use many line breaks on my personal projects since it find that it works best for me, but if I was working on a project with others I would obvious opt to use whatever other members of the group choose to use.
I’ll update my examples and put an explanation thanking you guys. Thanks for your comments!
@dk
I’ll check that out too!
nice tips Austin…I’ll try that
@deerawan
Thanks for the comment! I’m glad that you enjoy my post.
Commenting in between the code and having Blank spaces make it easy to refine the code in future and also to understand what it actually does. Thanks for your tips!
@Tech-Freak Exactly! Thanks for your comment and I’m glad that you like the post!