Writing beautiful code in WordPress is a topic that any programmer, at some point in his or her career, is going to consider. I say this if for no other reason because it’s something that’s almost a right of passage for any programmer.

When it comes to writing code, there are some who get into it because they have a real love for solving problems using computers. Others get into it for others reasons. Maybe it’s because someone urged them to do so, maybe it’s because technology is going to continue to be ever present in our lives.

If you’re of the former, then I think your appreciation for code isn’t as high as for those who get into it for a different reason. I’m not trying to generalize, but I’m speaking from observation.

So anyway, when it comes to writing beautiful code in WordPress, how do we even go about doing that?

Beautiful Code in WordPress

Early in my career, I used to be somewhat pre-occupied with the idea of writing beautiful code. That is, I wanted to write beautiful code, but I didn’t know what that really meant.

  • Does it mean code that legitimately looks good within the context of an IDE?
  • Does it mean code that performs well in the system in which it’s running?
  • Does it mean code that elegantly solves a problem at the expense of being easy to read?
  • Does it mean code that elegant solves a problem and is simultaneously easy to read?
  • …and so on.

Even now, I think there’s a level of subjectivity as to what it means. Maybe it’s all of the above, maybe it’s some of the above, maybe it’s none of the above.

But here’s one thing I do know:

Part of writing beautiful code is adhering to a standard such that the code looks like it could have been written by any of the developers working on a given team or at a given firm and so on.

This doesn’t hit on all of the points mentioned above, but it does hit on the aspect of readability and clarity, and that’s something that matters.

Beautiful Code in WordPress

If you’re looking for ways to at least start writing beautiful code in WordPress (for what it’s worth, I think it’s a career-long journey versus actually arriving at a destination), then why not start with the Coding Standards?

They exist for a number of reasons, but one of them is to help us write code that follows a convention allowing all of us to both write and read code that looks a certain way, reads a certain way, and is easier to follow.

Category:
Articles
Tags:

Join the conversation! 11 Comments

  1. I really dislike the WordPress Coding Standards. They are the opposite of “beautiful”, and go against what the rest of the PHP world have been doing with PSR-1 and PSR-2.

    But, I guess it makes sense to have all plugins/themes use a single source of truth for styling. And at the end of the day, it really doesn’t matter what style you code in. If you’re using a good IDE or PHP CodeSniffer, you can easily reformat your code to whichever standard you prefer. PHP CS Fixer is a nice tool as well: https://github.com/FriendsOfPHP/PHP-CS-Fixer

    • I really dislike the WordPress Coding Standards. They are the opposite of “ eautiful”, and go against what the rest of the PHP world have been doing with PSR-1 and PSR-2.

      I don’t know if I like or dislike them, per se, I just tend to accept them for what they are and try to adhere to them.

      I absolutely get the frustration of them differing from the PSRs, though, and I do with there was more harmony between the two =T.

      But, I guess it makes sense to have all plugins/themes use a single source of truth for styling.

      Because of the open source nature of it, I tend to favor it that way. And because I’ve worked in environments where standards have not been enforced and it gets more frustrating the longer you’re there working on those projects.

      I mean, that’s just my experience though. You – or anyone – may have a totally different experience, too :).

      PHP CS Fixer is a nice tool as well: https://github.com/ FriendsOfPHP/PHP-CS-Fixer

      Oh this is neat — thanks for the link to this. Keeping this in close company with the code sniffer.

      Thanks Dan!

  2. I still see a lot of CSS code without the proper quotations marks, and it drives me nuts. I think it may be that people are just impatient when coding. Still, I figure it was like unwritten law that you need to have your code structured a certain way, almost like having proper grammar.

    • Sometimes I think background plays a role in it. Like if you’re “classically trained” in development, then you’re going to approach it one way.

      If you’re someone who has picked things up along the way, then you’re going to have habits form in a different way than may need adjusting.

      Still, I figure it was like unwritten law that you need to have your code structured a certain way, almost like having proper grammar.

      I don’t disagree that it should be structured a certain way, but it’s relative to the environment in which you’re working and it assumes a person is familiar with that idea to begin with and that’s where the challenge comes, IMHO.

    • Sometimes I think background plays a role in it. Like if you’re “classically trained” in development, then you’re going to approach it one way.

      If you’re someone who has picked things up along the way, then you’re going to have habits form in a different way than may need adjusting.

      Still, I figure it was like unwritten law that you need to have your code structured a certain way, almost like having proper grammar.

      I don’t disagree that it should be structured a certain way, but it’s relative to the environment in which you’re working and it assumes a person is familiar with that idea to begin with and that’s where the challenge comes, IMHO.

  3. Agree with Don on not following PSR’s, but at the end of the day I’m glad that our community has a set of standards, even if they currently differ from the greater PHP community. That can be remedied over time, and what we have is a great starting point to build and iterate from, and point others to. Life is easier when we all speak the same dialect. The accent may not be ‘beautiful’, but if it’s understood by all, then it’s functional, and that wins in most of our books any day of the week :) Nice post Tom.

    • Thanks Tom!

      I think my post shows that I basically agree. Though it’d be nice to have closer alignment with the PSRs, I’m also happy to have a set of standards off of which to base our code.

      We could be doing much worse ;).

  4. I was trained as a classicist, which meant learning a number of human languages, one of them with a different alphabet and punctuation. (And I am busy telling myself that if I can learn Greek, I can learn JavaScript. And also that if I can get English punctuation correct instinctively, I can remember to put a semicolon at the end of every “sentence”.)

    Coding standards seem to be the equivalent of style guides for publication. The Chicago Manual of Style, the MLA Handbook, and the AP Stylebook have different “rules” for things like citations and the serial comma. None of those rules is “right” in an absolute sense, but if you are writing for a newspaper, you use AP, and if you are publishing a book, you use Chicago. (In the US. Other English-speaking nations have their own style guides.)

    It’s often difficult for people whose main experience of the “rules” of grammar was Strunk and White to get to grips with the fact that there are several sets of “rules” as elaborated in these style guides. I think it’s the same kind of inexperience that makes people insist one set of coding standards is good and another bad.

    • None of those rules is “right” in an absolute sense, but if you are writing for a newspaper, you use AP, and if you are publishing a book, you use Chicago. (In the US. Other English-speaking nations have their own style guides.)

      I like this analogy.

      I think it’s the same kind of inexperience that makes people insist one set of coding standards is good and another bad.

      Agreed — I’d also say that sometimes I think we get “stuck in our ways” and want our new set of code to match whatever we were used to in a previous job.

      I know from experience that when I moved from one gig to another, learning a new set of standards was a bit difficult. I wanted to revert to writing code how I was used to writing it not necessarily because I thought it was better, but because it was what I was used to (though I think I could’ve rationalized I thought it was better, but I digress :).

  5. For our young Russian friends I have translated these standards into Russian))

    http://web2033.com/wordpress-best-practices-php/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.