Massive Code Cleanup

Massive Code Cleanup is my latest WordPress-related project. Basically, I’m going through the files in WordPress’ 1.6 version, which is still in development, and I’m cleaning up the files. That is, I’m not changing any features… I’m just making the code more uniform and easier to read. If you’ve ever seen me drink coffee, you’d know that I can be highly compulsive. If only I could be that compulsive about keeping my apartment neat, but I digress.

I’ve fixed many files, going through thousands of lines of code one by one, and applying some search/replace macros to get the code under control. One of the side effects of having a project where multiple people contribute code with multiple development tools and habits is that the code becomes somewhat… soupy. My goal is to get things to be more uniform, which will increase readability, and fits in very well with WordPress’ “Code is Poetry” motto.

Here’s some of the things I’m changing. They are things that are mostly used by Ryan and Matt when they write new code, but I thought some other people could benefit from the structure tips. In my example screenshots, I turned on display of tab characters (as a rightward arrow) and line-break characters (which look like a sideways backwards “L”).

First, use tabs instead of spacing for indending loop structures, like if ( ) { }:

if loop indentation

Each additional if ( ) { } level should have its own level of tabbing. The curly braces should match up so that the “i” of “if” is on the same column as the ending curly brace. This also applies to for and foreach loops. Some more examples:

foreach loop indentation

Also note that functions should have their own level of indentation.

function indentation

Next, loop structures like if, for and foreach should maintain proper spacing, which you’ve already seen demonstrated. There should be a space inbetween if and the opening parenthesis, and between the ending parenthesis and the opening curly brace, as well as between the opening and closing parenthesis and their internals (like ( $a as $b ) or ( $condition > 3 )).

Next, if an if/else loop structure has only one statement per section, the curly braces should be omitted. For example:

curly brace omission

Next, you should search for “\t\n” (tab, followed by a line break) as well as ” \n” (space followed by a line break). These are remnants of your text-editing program, and just contribute size to the file without any visual benefit.

Your functions should be separated appropriately. I prefer to use 2 blank lines in between the end of the previous function and the start of the next one, as this increases readability.

Finally, here is a bad example followed by its corrected version.

bad code

And the corrected version:

good code

So, if code is poetry, I’m just helping with the punctuation and the metering. 🙂

16 thoughts on “Massive Code Cleanup

  1. Oh, thank goodness! 😀
    A mammoth task, but someone’s finally observing the guidelines on Codex, and enforcing it. No, that wasn’t flamebait.

  2. I don’t begrudge those who are submitting code that doesn’t follow the guidelines. I wouldn’t want to do anything to discourage contributions! So I have no problem with periodic code auditing and cleanup runs.

  3. I remember spending an entire morning cleaning up all files in the wp-includes directory; the patch never got applied.

    I hope your enhancements do get included!

  4. Pingback: dai craps online
  5. Pingback: cigna healthcare
  6. Pingback: ddf6c27f15b6
  7. Pingback: wwwcard pokr

Comments are closed.