So there I was, sitting back and reading Doug Boude's recent post on The Model-Glue Event Lifecycle in Layman's Terms. Good article, I thought. Nice descriptions. Then one paragraph jumped out at me and I had a sudden realization: OMG, we've regressed back in time.
Here's the relevant portion of the paragraph: "The value “firstname” that we’re retrieving from ViewState is a value that was placed in the Event Bucket at some point previously in the event lifecycle, likely [sic] by one of our broadcasts. Are you seeing the beauty of the Event Object yet?"
And that's when it struck me. For all of our MVC object-oriented posturing, here all we've done is to go back in time. Back when the web didn't exist. Back when people programmed in arcane languages like BASIC and COBOL. And back when global variables ruled the land.
Because, when you get right down to it, that's exactly the pattern that's occurring in Doug's ViewState example. The view (a subroutine), depends on a global variable (firstname) that hopefully exists in the global space (ViewState). A variable that some other method (subroutine) promised to create and insert.
But remember why global variables were considered bad programming practice in the first place?
There's no guarantee that firstname was actually created. No guarantee that the code won't change and that the message that creates firstname won't be removed. No guarantee that the message sequence will never be changed. And no guarantee that some other message that's creating a sidebar or some other page entity won't decide to create a firstname of its own, stomping all over the original value.
That's a lot of promises and guarantees.
Further, even if the global is created there's no guarantee that it's going to be the right type. At least with a method invocation (function call) you can tell the system to type-check passed parameters and return values.
I suppose you could add code to do the same in each message handler, checking for the existence of needed globals and type-checking for safety... but that seems like a lot of work. Especially for a system that's supposed to make our lives easier.
Funny, isn't it? We create "objects", give them fancy names like ViewState and collection, and all the while we're simply passing around a mishmash of global variables to a bunch of subroutines that we hope will be nice and play well together.
Odd, how the more things change...