Any problem in computer science can be solved with one additional layer of indirection. But that usually will create another problem. - David Wheeler
Any sufficiently advanced technology is indistinguishable from magic. - Arthur C. Clarke.
These are two classic truisms. Recently while staring at some code trying to figure out what the heck was going on, I realized the obvious.
One additional layer of indirection is indistinguishable from magic. - Scott Hanselman, this morning in a rare moment of clarity while hopped up on Diet Coke.
In recent talk at Mix on ASP.NET MVC (there's video there also) I mentioned that a certain line of code was magic:
public void Update(int id) { try { viewData.Product = db.Products.Single(p => p.ProductID == id); //MAGIC STARTS HERE Binding.UpdateFrom(viewData.Product, Request.Form); //END MAGIC db.SubmitChanges(); RedirectToAction("List"); } catch (InvalidOperationException err) { viewData.ErrorMessage = err.Message; RenderView("edit", viewData); } }
Why did it feel like magic? A few reasons.
All of this adds up to an impedance mismatch, IMHO. It's too confusing and I'm just a caveman (video). As such, I declare it as black magic.
This magic was brought up to the team (I think I remember hitting someone, there may have been some swearing. ;) ) and Rob changed it in a number of good ways.
//FEELS LESS MAGICAL Request.DeserializeTo(viewData.Product);
Jeff Moser's (see I told you I'd mention him twice) fine post talks about how cognitive "chunks" that represent solutions to problems can be more easily stored in our brains if they flow. Scott Bellware has called this concept "solubility." Code that uses frameworks with some balanced between focus on aesthetic and usability is more easily grokked.
I don't know if this is the final method, it's probably not, but I really enjoy discussions like this that aim to make things less like magic and more obvious and discoverable.
Request.ApplyFormDataTo(viewData.Product)
Request.DeserializeInto(viewData.Product)
Request.ApplyFormValuesTo(viewData.Product)
"but even then it doesn't feel write"
Remember Me
a@href@title, b, blockquote@cite, em, i, pre, strike, strong, sub, super, u