Solved

Design by Contract

Posted on 2014-11-03
3
102 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
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 26

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JAVA part two 5 57
Basic Java Case or If-Else statement... 3 43
windows explorer path to command prompt 5 33
eclipse with Angularjs2.0 3 17
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

920 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

15 Experts available now in Live!

Get 1:1 Help Now