[wp-hackers] Coding Standards: Functions vs. Classes

Brad Touesnard brad at touesnard.com
Mon Mar 19 00:49:30 UTC 2012


>
> That also does not mean we're going to use classes everywhere. There are
> plenty of situations where procedural code is going to be better. One good
> example is to allow for easier barriers to entry (like template tags and
> conditional tags). I like classes and objects, but I don't drink the
> Kool-Aid.
>

Yes, functions will definitely be easier for the APIs. But shouldn't they
mostly be synonyms of object methods? Shouldn't the business logic live in
classes? Kind of like query_posts and WP_Query.


While it may be "coder's choice" for sets of functions versus classes, just
> understand we don't take those decisions lightly, and yes, there is a
> careful method to our madness. register_post_type() was 2.9, hence why it
> is a simple function that stores everything in a global. I have a feeling
> things would be different if it were coded today — but that is *not* a
> reason to jump up and go rewrite it.
>

Agreed, rewriting is not worth it unless you're looking to learn something (
http://www.codinghorror.com/blog/2009/02/dont-reinvent-the-wheel-unless-you-plan-on-learning-more-about-wheels.html
).


[2] http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/
> [3] http://misko.hevery.com/2008/08/25/root-cause-of-singletons/
> [4] http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/
> [5]
>
> http://googletesting.blogspot.com/2008/11/clean-code-talks-global-state-and.html
> [6]
>
> http://googletesting.blogspot.com/2008/12/static-methods-are-death-to-testability.html
>

Thanks for these. The WP core had me convinced that global scope wasn't
that bad, but after reading Miško's articles, the issues with global scope
and Singletons are clear and certainly worth avoiding. However, I'm having
trouble imagining how new core development would get away from the global
scope and begin to use a factory pattern to produce proper dependency
graphs. It seems to me that it would be difficult to manage the factory
pattern while the global scope is still being used. Is that something that
the core team is still figuring out?


I am at the point where I will fight hard to prevent any more globals be
> added to core; we've been rather good about this for at least a year now.


That's interesting. I'd bet not many WP developers know this. Then again,
it could just be me. :)


The health and future direction of the WordPress codebase is an exciting
> challenge. The end result is I'm constantly thinking about rewriting (and
> not rewriting) code, new architecture, design patterns, unit testing, and
> plugin interfaces. We all are.
>

Great to hear. I'm guessing most of the discussions and decisions on this
stuff happens in Trac tickets?


More information about the wp-hackers mailing list