105 points · by Tomte · 7y ago · 25 comments
If you replace 100 lines of my code with 500 lines of buzzword-driven development that is 50% copied off of Stackoverflow, we're going to have a discussion.
if( $_GET["foo"] == "bar" ) {
//add_action( 'wp_loaded', 'do_foo' );
//add_action( 'init', 'do_foo' );
//add_action( 'admin_init', 'do_foo' );
do_foo();
}
which allowed easy launching of do_foo() by simply httping http://wordpress.tld/?foor=barto
add_action('rest_api_init', 'Company\PluginName\setup_foo_route');
and namespace Company\PluginName;
function setup_foo_route()
{
register_rest_route(
'company/v1',
'bar',
array(
'methods' => 'GET',
'callback' => 'Company\PluginName\do_foo',
)
);
}
which can be run by httping http://wordpress.tld/?rest_route=/company/barWhich one of us would like to talk to :) ?
What I noticed is that people following the first method ends up hacking core functionalities of WP because "it just works" but in the end that makes the code unstable and harder to maintain because it relies on undocumented stuff and a coding style that is very personal. It can also messes big time with stable things expected from the CMS (mangled taxonomies, hijacked hooks in the core instead of the WP pile that make it hard to debug or to play nice with other plugins/themes).
If I'm doing anything that actually requires sane PHP primitives (like the routing in your above example, and also notably DB) I steer clear from WordPress entirely. WordPress is a good CMS and makes for a great blogging platform but I have done my best to decouple any-and-all custom application development to it's own API.
WordPress and it's core simply don't offer good tools, and it's a VERY highly targeted platform in regards to security. I would suggest Symfony or Laravel for PHP frameworks that do APIs very well; or even AltoRouter + vanilla PHP for super light-weight stuff where you want minimal dependencies. Personally I think Laravel is the easiest, and a joy to learn.
Otherwise - kudos on ripping it out and replacing it with cleaner code!
I admit I am a bit nervous in regard to opening WP with REST endpoints considering its reputation.
Its hard because while we do recognize that the value and difficulty is in the problem-understanding, the code is the most tangible thing we produce and so it feels like the work.
In every shop I work at, I do ask WHY someone refactored something I have done. However, I do not do so with any malice.
As you point out above, sometimes, that first writeup is a working implementation, and while important, the first iteration is often not optimal.
Thus, asking why is useful because it helps us become better developers. Talking about the changes helps share knowledge, and, yes, when questionable changes are made, it can be a lesson for the refactor-er as well.
And when I am the refactor-er... I will try to explain my changes in the PR. Usually with a polite explanation of why I refactored.
I also find this very interesting. Many codebases decline in usage by not being actively developed. Other times, changing the way things are done goes against the core and is better off leaving it alone, and developing anew. Of course this is only clearly visible in retrospect. I'm generally against adapting to trends and wonder why perfectly complete and functioning frameworks need to be actively developed. This is why--we want to know that the framework will change with the users.
The second part of the trend to prefer modular libraries, I think was transitional as there was no one framework that had the best of breed components and using individual libraries produced a better ad-hoc framework. Now we have something in-between with coherent frameworks but still having replaceable parts.
I first used it to reduced the number of calls to my server by showing/hiding sections of a form and that made my apps way easier to use and much faster for the end user.
It was a little clunky (for me) to implement but it worked with most of the web browsers at the time and that made those features available across platforms. That was huge for me. I replaced it with jQuery later on and still use jQuery.
I have to credit Sam Stephenson with showing us (or at least me) the way towards full blown client side web apps. My latest version of that same app runs entirely in the user's web browser on or offline. Prototype was the first stepping stone I stood on to get here.
If someone goes about it the right way, the wrong decision in hindsight will feel right at the moment in time. Human matters oftentimes require a delicate touch, something that not many of us deeply involved in software pay attention to :)
This applies in dealing with others but perhaps even more so in dealing with yourself. If you don't tie your self worth to the work that you do, when the tides of time make your work irrelevant, it won't be so painful.