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 7490171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:33:37+00:00 2026-05-29T15:33:37+00:00

The following is an extract from an interface contract. My question is: should the

  • 0

The following is an extract from an interface contract.

My question is: should the former method repeat the pre-conditions of the latter method? As the contracts are public, would it be acceptable to omit the repetition? In this case it’s simply a non-null check, but I can imagine the situation where a lot of code is repeated and performance suffers as identical checks are repeated at runtime.

public int CommandConsumerCount(IWriteCommand writeCommand)
{
    Contract.Requires(writeCommand != null); // redundant?
    Contract.Requires(this.IsOwnerOf(writeCommand));

    Contract.Ensures(Contract.Result<int>() >= 0);

    return default(int);
}

public bool IsOwnerOf(IWriteCommand writeCommand)
{
    Contract.Requires(writeCommand != null);

    return default(bool);
}
  • 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-29T15:33:38+00:00Added an answer on May 29, 2026 at 3:33 pm

    It kind of depends on whether that condition (writeCommand != null in this case) is a requirement for both the CommandConsumerCount method as well as the IsOwnerOf method, or if it is only a requirement for the IsOwnerOf method.

    If that condition is only really required by the IsOwnerOf method, then it would be ok to omit it from the CommandConsumerCount method.

    However, if that condition is required by both methods, then I stand by my original answer below:

    I think that because both of your methods are public that the contract requirements should be repeated. If you had a private method that did the actual work, which both the IsOwnerOf method and the CommandConsumerCount method called (instead of the CommandConsumerCount method calling the IsOwnerOf method), then it would be fine to omit the call to Contract.Requires inside of that private method.

    As far as performance goes…I wouldn’t be concerned about the performance impact of these checks, unless the logic for the checks themselves is very complicated. You can set up the compiler to exclude those calls to Contract.Requires from the compiled output in the “Code Contracts” section of the project properties (assuming you have the necessary plugin installed).

    Perhaps I’m missing the point of your question, though. Are you asking if it’s OK to completely omit the call to IsOwnerOf inside of the CommandConsumerCount method? In that case I would leave the call in place, but if it’s a performance concern, then I would configure the project to exclude that call for Release builds, assuming that I’ve done sufficient testing with a Debug build to ensure that this condition is satisfied.

    EDIT: after re-reading the question, it seems clear that you’re asking about the writeCommand != null check, so I’ve struck out the above paragraph.

    Code example below of adding a private method that performs the actual work of the IsOwnerOf method.

        // you may want to choose a different name for this method
        private bool _IsOwnerOf(IWriteCommand)
        {
            // actual work is done here in this private method
            return default(bool);
        }
    
        public bool IsOwnerOf(IWriteCommand writeCommand)
        {
            Contract.Requires(writeCommand != null);
    
            // call the private method to perform the actual work
            return _IsOwnerOf(writeCommand);
        }
    
        public int CommandConsumerCount(IWriteCommand writeCommand)
        {
            Contract.Requires(writeCommand != null);
            Contract.Requires(_IsOwnerOf(writeCommand)); // call the private _IsOwnerOf method instead of the public method
    
            Contract.Ensures(Contract.Result<int>() >= 0);
    
            return default(int);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following is an extract from API for collection_select method: collection_select(object, method, collection, value_method, text_method,
The following is an extract from my code: public class AllIntegerIDs { public AllIntegerIDs()
I'm having trouble following this guide to extract my interfaces and entities from my
How do I extract foo from the following URL and store it in a
How would I extract the dollar amount from the following string some text will
I am trying to extract the field 'name' or 'named-expr' from the following object:
This question is related to the this . I'm using the following to extract
I am trying to extract from a webpage which has the following markup <div
I can't figure out how to get the following routes. Here's an extract from
I am using the following code to extract data from my database using entities.

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.