Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Design by Contract

Posted on 2014-11-03
3
Medium Priority
?
110 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 28

Accepted Solution

by:
dpearson earned 2000 total points
ID: 40420674
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
ID: 40424226
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 28

Expert Comment

by:dpearson
ID: 40425568
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

916 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