Solved

Design by Contract

Posted on 2014-11-03
3
100 Views
Last Modified: 2014-11-07
Hi,

I was reading as below



 defines the pre, post conditions of system
Code to interface as opposed to implementation.

I was not clear on explanation. What is design by contract and code to interface and implementation. Which one to use in which case. please advise
0
Comment
Question by:gudii9
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
Comment Utility
The basic idea is that you want to define:
a) what is required before you call to a system (method/class) - these are the pre conditions
and
b) what will be changed by this call - these are the post conditions

rather than relying on the details of the current implementation.

E.g. Say you had a method that computes the last day of the month.

The preconditions for the method might be that it is passed (a) the current date
The postconditions would be that it returns (b) the date of the day of the month

But maybe the method has a bug and on leap years it returns 28 days for February instead of 29.

So we have:
String endOfMonth = computeLastDayOfMonth("2015-02-01") ; // Returns 2015-02-28
String endOfMonth = computeLastDayOfMonth("2016-02-01") ; // Returns incorrectly 2016-02-28 should be 2016-02-29

If you are programming to the interface - to the definition of what this method should do, then you know that this is a bug and that "computeLastDayOfMonth" should return the correct date on a leap year.

If there is no "contract", no "pre and post conditions" then in theory you could say this method should ignore leap years and always return the same last day of the month - since it's not defined anywhere.  This is coding to the implementation (since that's what the method actually does).

Does that all make sense?

It basically means "define clearly what methods/systems do and code to that definition".

Doug
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
The basic idea is that you want to define:
a) what is required before you call to a system (method/class) - these are the pre conditions
and
b) what will be changed by this call - these are the post conditions

rather than relying on the details of the current implementation.

Assuming it is current application or system and we are about to do some enhancement to it or defect fixing to a production system right ?

The postconditions would be that it returns (b) the date of the day of the month

you mean end date of the month right?



If you are programming to the interface - to the definition of what this method should do, then you know that this is a bug and that "computeLastDayOfMonth" should return the correct date on a leap year.

If there is no "contract", no "pre and post conditions" then in theory you could say this method should ignore leap years and always return the same last day of the month - since it's not defined anywhere.  This is coding to the implementation (since that's what the method actually does).
Any real world examples, links, resources elaborating more on this differentiation between 'programing to the interface'(which i am thinking same as design by contract) vs design/programming to the implementation'
Please advise
0
 
LVL 26

Expert Comment

by:dpearson
Comment Utility
Assuming it is current application or system and we are about to do some enhancement to it or defect fixing to a production system right ?
Yes

you mean end date of the month right?
Oops - yes.

Any real world examples, links, resources elaborating more on this differentiation between 'programing to the interface'(which i am thinking same as design by contract) vs design/programming to the implementation'
Yes programming to the interface and design by contract are basically the same.

Here's some more links to elaborate on this:
http://www.javapractices.com/topic/TopicAction.do?Id=194
http://sett.ociweb.com/sett/settSep2011.html
http://svengrand.blogspot.com/2008/11/preconditions-postconditions-invariants.html

But please don't go through each and then ask me to explain specifics about what they're describing :)  Hopefully you can gain enough from the answer to this question from above and then fill in any gaps by reading those other articles.

Doug
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Passing list of object to Oracle Database Procedure 3 41
Change to event 1 72
WPF issue with Trigger 2 65
Problem to start Neon 20 49
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This video teaches viewers about errors in exception handling.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now