I don’t know if there’s a standard for what’s considered “clever code,” but I think that if you were to show various code samples to programmers, they’d be able to know it when they see it.
And there used to be a time in my career that I was far interested in writing clever code. But the older I get and the more work I do in maintaining projects, the less concerned I am about writing clever code and the more interested I am in writing code that’s clear and read and thus maintain.
When we work with WordPress, we’re likely dealing with arrays a lot especially given that so much of the internals of WordPress are built on them (yes, there are some objects too, but arrays are pervasive).
What, then, does clever code with arrays look like in WordPress versus less clever code? And furthermore, should we avoid aiming to write clever code?
Clever Code with Arrays
Array functions are arguably one of the largest set of functions in PHP.
Naturally, writing clever code with WordPress seems to fit part and partial with the environment, right? I’m not saying it’s bad at all. I’m just saying that when you have functions in the global namespace that work with arrays that return arrays, it’s far too easy to write nested calls that ultimately require a bit more mental work to parse what the code is doing.
Sure, writing about it is one thing, but maybe it makes sense to look at an example of what clever code with WordPress might look like and then how it can be refactored.
An Example
Say for example we have a post and we’re updating the post excerpt so that a name that’s contained within the excerpt needs to be removed. When this happens isn’t necessarily important (although delete_user isn’t hard to imagine, right?)
From the outset, we’re given:
- the post ID,
- the name of the person to remove.
One way to do this would be to use a combination of array, array_map, explode, array_diff, implode. All for this reason:
- array to create an array of the person (because it, as an array, is required later),
- array_map for trimming white space after exploding the excerpt into an array,
- array_diff for finding the strings that are left after removing a name,
- and implode to rebuilt the result back into a string for the post_excerpt.
Okay, so with that said, here’s what an example of what clever code in WordPress may look like:
But it’s a lot of nesting, and we usually have to start from the outside in and know what each function is doing, right?
To clean it up, we still have to deal with the functions that are listed above, but we can break things down into easier to read steps (along wth code comments) to make it easier for another developer to parse.
Perhaps it could look something like this:
Now, is this the way to do it? I don’t know. But it’s a way to do it. And it’s one of those situations that’s easier to read and to follow.
So perhaps it’s not writing clever code in WordPress, but I don’t know – nor think – that should be out goal.
Should We Aim To Write Clever Code?
The WordPress Handbook states the following:
In general, readability is more important than cleverness or brevity.
And then proceeds to give an example. At this point in my career, I tend to agree:
- clever code does not equate to more performant code,
- clever code often takes longer to jump through more mental hoops than verbose code,
- clever code is thus harder to maintain especially when jumping into an older codebase.
Finally, I think different people might consider some code more clever than others, but there’s also code that many of us would consider an attempt to be more clever than not.
Ultimately, aim to write code however it is you want to write but write with another developer in mind: If you’ve ever complained about a piece of code being tough to decipher at first glance, odds are it was poorly written or it was an attempt to be clever. So don’t be that guy or girl who’s passing the buck to the next developer.
Instead, aim to write clear code and use comments when necessary.
Leave a Reply
You must be logged in to post a comment.