Saturday, February 27, 2016

Single Responsibility or Changing Growing Responsibilities Principle

Architecture is full of examples of items designed to do more than one thing.  Your smart phone is a flash light.  From my last examples, a chair is also a clothes hanger.  A seat cushion is a flotation device. 

Robert Martin said in his book Agile Software Development, Principles, Patterns, and Practice (http://en.wikipedia.org/wiki/Robert_Cecil_Martin),  that every class should have a single responsibility over a part of the function.  The function-part should be encapsulated and the class should do that and only that. 

I understand the need for this as it makes the class easy to understand.  It also could reduce the introduction of bugs.  Have you ever worked with "spaghetti code"?  You make a change and it causes a bug in another area you couldn’t have imagined in your wildest dreams.   If a class has two different things it does, and you change one thing, it could affect the other piece and thus break something.  Martin gives an example a class that compiles AND prints a report.    If the content of the report changes, the class must change.  If the format of the print must change, then the class changes.  Martin argues that there is no reason for these two things to be coupled because if the format of the print changes, the content does not change and thus should not need to be recompiled and tested. Martin says a class should have only one reason to change (not two, like in this example).  


In an imperfect world, needs of software change over time.  An example: My manager is crazy over this particular module we have to is used to access and configure accounting data.  Over time it has evolved to access security data as well.  This drives him crazy because it violates the single responsibility principle (although he doesn't call it that and doesn't know that Robert Martin invented the term).  I however, think this is OK, and have argued this fact with him.   Given the schedule pressures we were under to combine these two pieces, which are closely related in our system, is not horrific.  Not perfect but not catastrophic either.  

Sunday, February 21, 2016

Form Follows Function and Function can also follow Form

We as engineers know this old saying and if you are an SDM graduate, you may remember hearing or saying it classes.   But where did that saying come from?  It can be attributed to a book by Peter Blake called "Form follows Fiasco".  It was a critique of modern architecture which the author said was too focused on form and that the function should be the utmost important in any design.  This is what I always thought "Form follows function" meant - but some interpretations say that if the function is met in the purest, most simple way, the form follows and it will be beautiful.  (1)  .  The chair designs in Denmark represent this principle.  Software, which can so easily get complex, is another example (see some actual code later). 

So if we look at the chair picture from my last blog post, in the last row, that chair looks uncomfortable to me.  If the purpose of the chair is comfort, then without a solid back and full arm rests, this chair doesn't fit the bill.  However, if the chair is to made to fit completely underneath a table, then the design goals are different and conflict with the comfort goal.  To make a chair fit underneath the table, some comfort will have to be sacrificed.

So depending on the goals of the design, what form  will meet the design criteria?  That is the question to ask.  Each design criteria might not be met perfectly but the stakeholders need to decide which one can be sacrificed.  It was more important for the chair to fit underneath the table then for it to be perfectly supportive of the back.  After all people will only be sitting here for short bouts at the dinner table. 

Also what is beautiful in software code? I had my 84 year old mother look at some code and I asked her if she thought it was beautiful. She thought all of it was … very strange.  She could see no beauty in it whatsoever.  (To be fair, she also never liked furniture from Denmark, even when it was all the rage here in the US in the late 1960s). 

Here's an example of some weather data I was working with recently from a web service that needed to be parsed:



The data was in JSON (Javascript Object Notation) format which is a widely used  by web services to return data described with text, and is human readable.  To parse this, we could created one massive class with all the items needed inside. 


However, one could break everything into subsclasses like this design, such as wind, coordinates, etc into separate classes.  Is that cleaner?  Easier to maintain?  Easier to understand?  Beautiful form?  I happen to like all in one, the first case, because I can see it all in one file.  But if I changed wind, I would end of changing the weather class.  That can be a problem to introduce bugs into a larger system. 


Back to my Danish furniture, I found a chair that was brilliantly designed as clothes hanger. 



Is this chair a good clothes hanger?  Yeah, it's pretty good.  It won't function as a full closet but it can hang a few things.  Is it the most comfortable chair in the world?  No but it is supportive and someone can sit in it, even when clothes are hanging on it.   And it also keeps beauty in the curved lines and rounded edges.  Notice that the form does indeed follow function.  This form is not just of a chair that you can sit on.  It's a chair that you sit on and hang clothes on. 

Another example is a seat cushion that serves as a flotation device.  This does two things.  It isn't the best seat cushion in town and it isn't the best flotation device in town, but it works as both.  Tradeoffs occur.  Can you imagine what would happen to this chair if the designer kept iterating to make the chair more comfortable and to hang more clothes?


So while I agree function dictates form, the simplest form and often the most beautiful form will come from meeting the a minimalist set of functions.