Wednesday 4 November 2015

UPDATE is evil...

The UPDATE keyword broke RDBMS's (Relational Database Management Systems).

That's a somewhat crazy assertion to make. Or is it?

The thought occurred to me a little earlier & I'm having a tough time trying to think of a good reason for this particular keyword to exist in modern software engineering. As a construct, it may be the root of all database evils.

I'm thinking of this from the perspective of moderate to huge applications/platforms. Smaller apps would be immune to evils of UPDATE. Although I'm picking on SQL Server in this post, the concept could probably apply to any row-based RDBMS which supports the UPDATE construct.

Databases are great. What they're great at is the storage and retrieval of information. What they're not great at is logic.Of course you *can* introduce logic in a database through the use of stored procedures, etc. but should you? My response is "No, you should not.

UPDATE as a construct gives rise to the possibility of introducing logic to a database. As a result, we've had to introduce more & more constructs to be able to handle the fallout from having this ability, handy though it may be.

Think about it, once we had the ability to update one of more records, we found new & inventive, although ultimately destructive ways of manipulating the data at our disposal. This lead to us grouping statements together in reusable blocks which could be called at will - the stored procedure.

Stored procedures are great for exposing only the data you want exposed, isolating the source of the data from the consumer of it. A code contract of sorts. With stored procedures it became possible to alter the underlying schema of data without breaking a dependency on the data. Combine a stored procedure with UPDATE and you have the means of introducing logic branches in the database.

There's a few reasons off the top of my head why this is bad in today's world. I'm sure there a many more, but these spring to mind immediately.

  • Logic trapped in databases introduces a limitation to scale as your data can no longer be distributed across multiple hosts/geographic locations. Yes, there may be solutions to the shared hosts/location problem so that your database(s) appear to be a single instance to your procedures but this is, essentially, a workaround to the problem that your logic is in the wrong place.

    The shared/hosts location problem is one in it's own right, with it's own solutions. Your logic should not add to this problem.
  • Having logic trapped in the database means your logic can't be shared quite as easily between applications without introducing more dependency on the database, invariably leading to brittle software. 
  • Security - introducing logic in the database means that the data it acts upon can't easily be encrypted. Logic depends on conditions, conditions depend on data. While the query engine may not care whether the data is encrypted or not, the author of the procedure almost certainly will. 
If we didn't have the power to update records, introducing logic would be significantly more challenging. Sharing data between multiple hosts/locations becomes easier (as we don't have to worry about how that affects logic). It would promote moving logic into libraries which are more easily shared (not to mention unit-tested) and encrypting/decrypting the data falls then to the application (a separate discussion). In this age of frequent security breaches, keeping data encrypted may be safer for everyone.

As if this wasn't enough, it became apparent that simply updating a record was not enough. Preserving the history of the update, or the value of the field before the update, became necessary to maintain an audit trail. Enter, the humble trigger. While useful, it opens the door to additional logic branches or crazy workflows which aren't easy to diagnose or debug.

I propose we use only the CRD (Create, Retrieve, Delete) constructs in RDBMS's. Not having UPDATE forces a re-think in how we design data dependant software, possibly with some very positive side effects. Of course, it could backfire entirely... I haven't tried it yet but I'm giving it some serious thought.

I might go into more detail on this subject in the future. It's late at the moment, I'm tired & want to publish this :) While procedures & triggers are really only the most visible but there are other areas as well. Still, I hope this has helped you question along the same lines I have.

Thursday 8 October 2015

A Reply To A Rugby Troll

This was in response to a comment on this article.

Something which our American friends may not quite understand is that rugby - like NFL - is a "generational" sport. You don't become great at a sport relatively overnight.

A Quick History Lesson
Even though rugby was introduced in the mid 19th century, it dropped in popularity and only experienced a resurgence in the late 1960's/early 1970's (or thereabouts according to some sources). The governing body for rugby in the US, now called USA Rugby, was formed in 1975.

Compare that to South Africa where rugby has been played since around 1860, with rugby union being played from around 1875. The governing body for rugby in South Africa, now called the South African Rugby Union (SARU) being formed in 1889. That's nearly a century apart.

Generational Sport
Disclaimer: Although I have very little context of life in the US (I'm South African, living in Ireland) I presume what I am about to describe holds true for NFL as well.

Kids in SA play rugby from a very young age. Although I'm not sure if it's still the case, it was not uncommon for rugby to be a compulsory sport at school. If your parents like rugby it's almost certain that you will play as well. And if your father/grandfather/etc. played for the Springboks then it's pretty much a given that you will play. A number of our current Springboks had fathers/grandfathers who were Springboks - Schalk Burger, Ruan Pienaar, Cobus Reinach (although not selected for the RWC) are just a few that spring to mind.

If you're identified as having talent - even at a very young age (under 9/10) - you will usually get a chance to try out for one of the junior academies of one of the larger franchises at provincial level. This offers them the chance to be coached at a much higher standard than they'd get at school (where it's usually a teacher or two with a keen interest in rugby but no real coaching experience or training).

Kids that continue to perform will remain in the academy & continue getting access to better quality coaching than they would at school. Some kids may earn a scholarship to one of the top rugby schools in the country. This way, they get coaching at school and at the academy.

From there onwards, they'll make their senior debut at a domestic tournament & the rest is pretty self-explanatory.

Of course, mileage may vary, but this is a generalized, grossly over-simplified gist.

The Point
The point is that kids in SA have rugby drilled into them from the womb. We watch it with our parents, we play it at school, play it with our friends, etc. We learn the intricacies of the sport and understanding it becomes second nature for those who play (& for most of the supporters as well).

Despite it's brutality, rugby is actually a very technical and tactical sport. It's not just about speed or size or stamina - these attributes are, of course, desirable in a rugby player but what's more important is situational/spacial awareness in the context of what's happening in front of and around you on the field. Split second decisions matter. You could be the biggest, fastest guy on the planet but if you don't have an innate understanding of how to read a game of rugby to be able to make the right decision in that split second you will not succeed in the sport.

Rugby is a developing sport in the US, it's a religion in SA. According to Mike Tolkin (head coach for Team USA), it's the fastest growing team sport in the US. When Americans lose a rugby match, they chalk it up to experience and hope to improve in the next match. The vast majority of the American public wouldn't even know that they'd played.

Completely different when the Springboks lose. When we lost to Japan earlier in the tournament it was considered a travesty - a national catastrophe! It made headlines throughout the country and dominated media coverage. It was spoken about at length & prodded from every possible angle.

The USA may be a Tier 2 nation at the moment but that's not to say they will remain that way. To be truly competitive at the elite level will take generations. The good news is that YOU can do something about it - support your national team - win or lose. Be proud that they qualified to compete in the tournament so that they could get the opportunity to play against two-time world champions.

Find out about and support your domestic league. Tell your friends about the sport, tell your kids, go to matches & support your local teams. It's not your athletes which will grow your team to elite level - it's the everyday American. It's you.