Solved

Design by Contract

Posted on 2014-11-03
3
106 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
dpearson earned 500 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 27

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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 …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

729 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