var blog = new ThoughtStream(me); RSS 2.0
 Wednesday, September 24, 2008

I've seen this question a lot recently, and Scott Bellware asks it again in response to a post by Jimmy Bogard.

"Ok, but why are "contracts" important?  I can write concrete classes that interact with each other just fine without interfaces. And as Roy has already demonstrated, you don't need interfaces for test-focused SoC. To wit, any concrete class's signature is a contract with its user." -- Scott Bellware

First - a small psychology lesson in Cognitive Load theory and Chunking. A human brain can only hold so much information in it's short term memory. The "magic number" of 7 (+/- 2) has often been touted as the average number of concepts that a person can hold in short term memory, and still understand what's going on.

... and we need to recognize this in our code. If a developer who is reading the code has more than 7 (+/- 2) concepts, then that developer is not likely to understand the code they are reading. If you can't understand the code, you can't maintain the code. It's as simple as that. So why are contracts important? An intention-revealing interface, as a contract, can significantly reduce the cognitive load that is required to understand the code in question.

Abstraction and dependency inversion via interfaces help us achieve this understandability by letting a developer's mind chunk a process into what's really important - the "when" and "what" of the process, ignoring the "how". If you eliminated all abstractions and interfaces - even the interfaces that have only one implementation - you are telling a developer that they need to know the details of "how", not just the abstraction of "when" and "what". This puts an additional load on any persons' brain, and can quickly overload the person reading the code.

These theories go so far beyond just code, in software. When was the last time you saw a web site that had more than 8 or 9 buttons in it's navigation/menu, and you thought it was an intuitive and easy to use site? I'd be willing to bet that you thought the site was poorly organized and difficult to navigate. Interaction design is usually the first place that people apply cognitive load and chunking theories. Unfortunately, it's also usually the last. We need to break this cycle of overloading ourselves and our coworkers, and create proper abstractions in our code that fit easily within our own cognitive load, but more importantly in the cognitive load of other developers who have to read/maintain the code.

Wednesday, September 24, 2008 9:05:16 PM (Central Standard Time, UTC-06:00)  #    Comments [4]. Trackback 
Tags: .NET | Analysis and Design | Code Reviews | Design By Contract | Interaction Design | Principles and Patterns | Psychology Of Software
Tracked by:
"Dependency Inversion: 'Abstraction' Does Not Mean 'Interface'" (new ThoughtStre... [Trackback]
"Dependency Inversion: 'Abstraction' Does Not Mean 'Interface'" (Derick Bailey.c... [Trackback]
"http://www.chooseyourevent.com/MoreDetails.asp?ID=49911" (http://www.chooseyour... [Pingback]
"http://www.rlc.cc.il.us/pressroom/newsstory.php?id=2692" (http://www.rlc.cc.il.... [Pingback]
"http://www.google.com/search?q=tbahkowe" (http://www.google.com/search?q=tbahko... [Pingback]
"http://www.club14.com/page/club14/clubinfo_actuclub.asp?id=191" (http://www.clu... [Pingback]
"http://www.capgros.com/agenda/cicle.asp?id=377" (http://www.capgros.com/agenda/... [Pingback]
"http://www.zubiri.org/financial/profile.cfm?RecNum=416905198" (http://www.zubir... [Pingback]
"http://www.cosacnaify.com.br/loja/biografia.asp?IDAutor=525" (http://www.cosacn... [Pingback]
"http://www.fims.org/default.asp?PageID=540599787" (http://www.fims.org/default.... [Pingback]
"http://www.aiim.org.uk/news/db/newsmore.asp?id=561" (http://www.aiim.org.uk/new... [Pingback]
"http://intranet.asoex.cl/AsoexWeb/Menu.asp?Id_Menu=124" (http://intranet.asoex.... [Pingback]
"http://www.tips4me.com/forum/displayallmessagesNew.asp?messageid=997776664" (ht... [Pingback]
"Baby names search - Search for bailey" (Baby-Parenting.com) [Trackback]
"http://www.hhs-stat.net/bobby/scripts/notes.cfm?urlid=947597341" (http://www.hh... [Pingback]
"http://www.calis.edu.cn/CALIS/lhml/faqall.asp?id=100" (http://www.calis.edu.cn/... [Pingback]
"http://www.goleeflames.com/info/miscellaneous/box-score.asp?id=4628" (http://ww... [Pingback]
"http://www.arbetov.com/en/reply.asp?message_id=1883" (http://www.arbetov.com/en... [Pingback]

Thursday, September 25, 2008 3:08:20 AM (Central Standard Time, UTC-06:00)
I view these two articles as being important http://martinfowler.com/bliki/ImplicitInterfaceImplementation.html and http://martinfowler.com/bliki/RoleInterface.html.

So my view. If you go ahead and create a small cut-down client focussed interface then yes its a contract and has value as an abstraction. If you do Foo : IFoo then you have a header interface and unless you put a lot of work into the contract (pre-conditions/post-conditions/errors/side effects) people will just go to the iumplementation to understand the behavior. Plus how decoupled are you, change Foo and you have to change IFoo plus if you do have another class come along and want to have it replace Foo then you can be pretty sure the interface will need to change (in fact you'll want it to change).

So do I use Foo : IFoo, I sometimes do. IoC and mocking make it attractive and it doesn't do major harm other than hiding the true abstractions (which are far more exciting). However I do say that these header interfaces are not the big design advantage that some make it out to be, not by any stretch of the imagination. Also to me interface-implementation pairs are not what SOLID is all about (ISP).

"If you eliminated all abstractions and interfaces - even the interfaces that have only one implementation"

Don't think anyone is saying that we should eliminate them all, definitely not Roy/Bellware from what i've seen.
Thursday, September 25, 2008 7:18:26 AM (Central Standard Time, UTC-06:00)
@Colin,

excellent Fowler articles. thanks for the links and clarification on the names, etc.


"unless you put a lot of work into the contract (pre-conditions/post-conditions/errors/side effects) people will just go to the iumplementation to understand the behavior."

very true. how do you handle design by contract? i tend to do it through good unit tests and intention revealing abstractions.


"Plus how decoupled are you, change Foo and you have to change IFoo plus if you do have another class come along and want to have it replace Foo then you can be pretty sure the interface will need to change (in fact you'll want it to change)."

true again. this can be a sign of a poor / weak abstraction - not always, though. and as you say, there are some benefits when talking about IoC and other techniques, and sometimes you have to make the judgment call to put in the weak abstraction for these purposes.

header interfaces certainly aren't a big design advantage - but they can reduce the cognitive load when done correctly.

For a real world example, I have an "IMessageSender" interface with a "Send(Message message)" method. My runtime implementation is called "MessageSender" and has a lot of WebsphereMQ/XMS.NET specific code inside of it. There's only one method in the MessageSender class - the one method that was defined by the interface. Although this is technically a "header interface", I also consider it to be a good abstraction. I can easily replace the MessageSender object with something that uses WCF, web services, flat files, FTP, or anything else that I want, and add as many other methods and properties to the implementation as I need.

unfortunately, the line between strong/weak or good/bad abstraction does get blurry when we move out of service implementations and into abstractions within the domain, etc. the point is - just because it's a header interface, doesn't mean it's a bad abstraction.


"Don't think anyone is saying that we should eliminate them all, definitely not Roy/Bellware from what i've seen."

i didn't mean to say that scott/roy were saying we should get rid of all the header interfaces - sorry if it sounded that way. I think scott was asking more rhetorically, and i don't think he would say to do away with them.


"Also to me interface-implementation pairs are not what SOLID is all about (ISP)."

once again, I agree. Interface segregation is more along the lines of Role Interface as defined by Fowler in your links.

Dependency Inversion, on the other hand, gives us a good reason to use interface-implementation pairs; and can provide the additional benefit of lowering our cognitive load, thereby lowering the barrier to entry in understanding the code.
Thursday, September 25, 2008 3:24:07 PM (Central Standard Time, UTC-06:00)
Derick,

> An intention-revealing interface, as a contract, can
> significantly reduce the cognitive load that is required
> to understand the code in question

I think this belief is rooted in an assumption of what "interface" means as per your above point, and what the keyword "interface" is in C# or Java.

If we believe that this word means the same thing in different contexts, we might be shooting ourselves in the cognitive foot.

Any module's signature is an interface. If a module is already designed to be understood, adding an interface to it can add noise and cognitive dissonance.

Adding an intention-revealing abstraction (.NET interface) to a class that has been designed without consideration for humans and human cognition can be like putting lipstick on a pig.

It's not the intention of the interfaces that make software designs understandable, it's the intention of the software designer that makes software designs understandable.

The notion of interface types (IFoo, etc) as cognitive mechanisms is .NET lore. Consider languages that don't have interface types.
Friday, September 26, 2008 7:27:13 AM (Central Standard Time, UTC-06:00)
@Scott,

I do tend to speak from a C# perspective, since that's what I do all day (am actively trying to find time to change that, though). But I also understand that other languages don't have explicit "interface" constructs; c++ for example. whether or not it's an explicit construct, or simply the public signature of the API, the interface we design can, and should, reduce the cognitive load of the person reading the code.

"It's not the intention of the interfaces that make software designs understandable, it's the intention of the software designer that makes software designs understandable."

a chef employs the use of a pan to cook food. does that mean the pan's intention is to cook food? not a chance! the pan has no intention of it's own - it's merely a tool that the chef uses to fulfill his intention of cooking food. by the same notions, a software designer can employ interfaces to create a design that is understandable.

just because the tool does not have inherent "intentions", doesn't mean it can't be used to fulfill some intention of the user.
Comments are closed.
Navigation
About Me
View Derick Bailey's profile on LinkedIn

Send mail to the author(s) Contact Me
Archive
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Derick Bailey
Sign In
Statistics
Total Posts: 115
This Year: 0
This Month: 0
This Week: 0
Comments: 44
Themes
Pick a theme:
All Content © 2010, Derick Bailey
DasBlog theme 'Business' created by Christoph De Baene (delarou)