[wp-trac] [WordPress Trac] #37699: Death to Globals Episode #1: A Registry, A Pattern

WordPress Trac noreply at wordpress.org
Thu Sep 8 07:39:11 UTC 2016


#37699: Death to Globals Episode #1: A Registry, A Pattern
----------------------------+------------------
 Reporter:  wonderboymusic  |       Owner:
     Type:  enhancement     |      Status:  new
 Priority:  normal          |   Milestone:  4.7
Component:  General         |     Version:
 Severity:  normal          |  Resolution:
 Keywords:                  |     Focuses:
----------------------------+------------------

Comment (by jacobsantos):

 Replying to [comment:85 dd32]:
 > Replying to [comment:84 jacobsantos]:
 > > How is increased data on the debug output a valid argument?
 >
 > Anything can be a legitimate argument if it affects ones workflows.


 So, a training issue? This is a training issue. It is also a manager's
 excuse. "Why did you change a perfectly functional UI? Now I have to spend
 $xx,xxx retraining all of my employee's because you moved a button from
 location x to location y."

 To be honest, I never thought I would hear this from a developer. I guess
 it makes sense. We developers like to argue over white space and that is
 one of the most pointless arguments a developer could have. Well, besides
 text editor. We are fond of our holy wars.

 My reasoning is that things change and people either change with it or
 they don't. Those that don't survive, certainly, but they are stuck. You
 mentioned using a debugger and I will say that PHP debugger has come a
 long way. It is unfortunate that there aren't a lot of free choices that
 make it easy to use xdebug to step through code and set breakpoints. You
 could look into paying money for a professional license of an IDE that
 does have good support? There might be a vim plugin or emacs. I don't use
 emacs, so I wouldn't know.

 Actually, the point is that your decision to partially reject progress
 based on your selfish desire to not have to scroll a little more is really
 a little disappointing to be honest. I would make a star wars quote, but I
 have been told it is a terrible movie. Pretty awesome last few minutes, up
 until the "NOOOOOO!" and "She died of a broken heart" parts. Dat action
 dough.

 >
 > > Moving a global to a protected attribute is a valid refactoring
 technique recommended by experts. Furthermore allows for dependency
 injection at a later date. Which is considered a good thing.
 >
 > Yes, It's a valid refactoring technique, which adds zero benefit for
 WordPress. Referencing outside objects via a class method does nothing for
 code clarity or improving future code. All it does is hide the instance
 behind yet another layer of abstraction, something that doesn't give us
 any major benefits. Dependency injection can be done at a later date when
 it's viable regardless of this change.


 Nah, that sounds a lot like micro-optimizations to me. I hear that
 argument for Python, "Make class attributes local, so that you gain a
 boost in performance." You could do it, certainly, then after you look at
 the awful mess you created, you'll go back to doing it the proper way.

 I love your argument by the way. The beauty of it is that it can be used
 for anything in OOP and one of the many, many reasons classes are a mess
 in WordPress. I do agree that if you don't want to do OOP right, that you
 shouldn't do it at all, but that ship has already sailed. Classes are
 terrible in WordPress and it looks like no one knows what they are doing.

 This argument, is part of the reason why. I can't believe I have to write
 this. I know you know already know what I am going to write next, but I'm
 going to write it anyway. I hope you are trolling. If you are, good show.
 OOP is nothing, if not abstractions. That is all it is. All you are doing
 is passing one reference of an abstraction to another concrete example of
 another abstraction. It is all abstract, until it is not. It is one of
 those awful paintings that people say is brilliant, but just looks like
 something a child would finger paint.

 I don't know what you mean by an class attribute being an abstraction. I
 guess in the purely technical sense it is merely the language restricting
 access to some arbitrary address offset in memory, but from a OOP
 perspective, it is probably the most concrete thing you can get, other
 than classes themselves. I would say that attributes would be purely
 concrete, if you want to go down to the machine layer and think of them as
 structs. In that sense, you have functions abstracted as methods acting on
 structs holding data.

 Sure going from arbitrary heap location to an arbitrary heap location is
 probably not that impressive. I would add that the protected variable and
 the global are both pointing to the same location in memory, until which
 time something changes. It is harder to reason the copy-on-write rules in
 PHP as they are slightly different than other languages that actually make
 sense and actually use pointers. I always forget in PHP5, if you change a
 member in a class if it holds true for other references to the same
 object. I could 3v4l that question, but meh that isn't the point.

 The point is more that the benefit will come later when you have a nicely
 abstracted class and everything fits in its place and you don't have to
 worry about some implementation changing on your, because you don't care
 about the implementation, you only care that it returns what it should.

 >
 > > How is it still a global? If the global changes, the internal object
 reference will not.
 > A reference to a global, verses a direct global import is ultimately the
 same object, sure, you're accessing it differently, you're creating an
 extra memory reference to it, but it's still a global object which you're
 pulling in to every function you use it within.
 >
 > All this does is give you a fuzzy feeling of "Oh, Look at this,
 WordPress suddenly has 30 less uses of a global!" when in reality nothing
 actually changed, and it only added an extra layer in front of the
 developer for no benefit.

 https://github.com/MimicCMS/mimiccms-
 library/tree/master/src/MimicCMS/WordPress/AdminBar - I'll just put this
 here. My point is that, I get a warm fuzzy feeling when OOP is done right.
 Or at least as right as I can attempt to do it.

 I don't get warm fuzzy feelings when an application has 30 less uses of a
 global, because as far as I am concerned with regards to PHP, having 1
 global is bad. 30 less of something that could potentially blow up and
 take the entire application down is 30 less of something I have to worry
 about. You understand that I could just create a plugin and make `$wpdb` a
 global and accidentally create a local variable with the same name and
 destroy the reference in $wpdb? PHP won't tell you and the condition may
 not occur but one in a million. Imagine that day when one in a million
 happens. Imagine that one in a million becomes 1 in 1.

 The goal of programming should be to minimize risk. Globals are a huge
 risk. A protected member is less than a risk. Only a risk for children
 that inherit from the parent. A global is inherited by the entire
 application, you change it when it should not be changed and the
 application is done.

 It is less about warm fuzzy feeling than, "Oh dear god! Don't touch the
 giant red button!" and going around screaming, "You didn't touch the giant
 red button did you?" Why just not have a giant red button in the first
 place?

 -----------

 As an aside, I understand that in some languages, globals are a thing and
 they are fine. Go uses globals and they are great. Part of the reason they
 are fine is because they are per file. I don't believe you can have a
 global that is entire application wide, unless you started playing with
 memory management. Other languages have likewise gotten passed the global
 issue by not allowing variables to be accessed passed the module barrier.
 If you access a module global, then you meant to access it.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37699#comment:86>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list