Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4338804
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:07:51+00:00 2026-05-21T11:07:51+00:00

I am learning about preconditions and when to use them. I have been told

  • 0

I am learning about preconditions and when to use them. I have been told
that the precondition

@pre fileName must be the name of a valid file

does not suit in the following code:

/**
Creates a new FileReader, given the name of file to read from.
@param fileName- the name of file to read from
@throw FileNotFoundException - if the named file does not exist,
is a directory rather than a regular file, or for some other reason cannot
be opened for reading.
*/
public FileReader readFile(String fileName) throws FileNotFoundException {
. . .
}//readFile

Why is this?

Edit: Another example

We are assuming that the following, as an example, is done the “right” way.
Note the IllegalArgumentException and the precondition. Note how the behavior
is well defined, and how the throws declaration is made even though
a precondition is set. Most importantly, notice how it doesn’t contain
a precondition for the NullPointerException. Once again, why doesn’t it?

/**
* @param start the beginning of the period
* @param end the end of the period; must not precede start
* @pre start <= end
* @post The time span of the returned period is positive.
* @throws IllegalArgumentException if start is after end
* @throws NullPointerException if start or end is null
*/
public Period(Date start, Date end) f

Are these examples avoiding use of extra preconditions? One could
argue that if we are avoiding preconditions, then why have them at all?
That is, why not replace all preconditions with @throws declarations (if
avoiding them is what is done here)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:07:52+00:00Added an answer on May 21, 2026 at 11:07 am

    Ok, so this is what I’ve found out:

    Background

    Based on the following principles, as described in Bertrand Meyer’s book
    Object Oriented Software Construction:

    "Non-Redundancy principle Under no
    circumstances shall the body of a
    routine ever test for the routine’s
    precondition." – Bertrand Meyer

    "Precondition Availability rule Every
    feature appearing in the precondition
    of a routine must be available to
    every client to which the routine is
    available." – Bertrand Meyer

    , these two points answer this question:

    1. For preconditions to be useful, the client (user of method) has to be able to test them.
    2. The server should never test the precondition, because this will add complexity to the
      system. Although, assertions are turned on to do this testing when debugging the system.

    More on when, why, and how to use preconditions:

    "Central to Design by Contract is the
    idea, expressed as the Non-Redundancy
    principle, that for any consistency
    condition that could jeopardize a
    routine’s proper functioning you
    should assign enforcement of this
    condition to only one of the two
    partners in the contract. Which one?
    In each case you have two
    possibilities: • Either you assign the
    responsibility to clients, in which
    case the condition will appear as part
    of the routine’s precondition. • Or
    you appoint the supplier, in which
    case the condition will appear in a
    conditional instruction of the form if
    condition then …, or an equivalent
    control structure, in the routine’s
    body.

    We can call the first attitude
    demanding and the second one
    tolerant." – Bertrand Meyer

    So a precondition should only exist if it is decided that the client holds the responsibility.
    Since the server should not test the precondition, the behaviour becomes undefined (as also stated on Wikipedia).

    Answers

    • The first point answers the first example.
    • As for the second example, it is probably not done the right way. This is because
      the first @throws declaration implies that the method has (other than an assertion)
      tested the precondition. This violates the second point.

    As for the null pointer; this shows that the null pointer responsibility is assigned
    to the server. That is, using a "tolerant attitude", as opposed to a "demanding attitude".
    This is perfectly OK. If one chose to implement a demanding attitude, one would
    remove the throws declaration (but more importantly; not test for it), and add
    a precondition declaration (and perhaps an assertion).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been learning about classes in JavaScript, the use of prototype and finally how
I have been learning about collision in phisics programming , and i have one
I have recently been learning about doctypes, and was I wondering what the differences
I have been learning about the basics of C# but haven't come across a
I am learning about how to use JUnit but there is something that I
I have been learning about Delegation and Data Sources for iOS programming and need
After learning about the Action delegate in C# I've been looking for ways I
I'm learning about namespaces in PHP 5.3 and I would like to use Namespaces
I'm learning about how to manipulate the Membership class. I have added the Membership
learning about loops (still a beginner) in VB.net. I have got the below code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.