Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Design by Contract

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

721 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