Click here to Skip to main content
15,917,709 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
QuestionCode contracts Pin
Super Lloyd25-Feb-16 15:52
Super Lloyd25-Feb-16 15:52 
GeneralRe: Code contracts Pin
PIEBALDconsult25-Feb-16 16:38
mvePIEBALDconsult25-Feb-16 16:38 
GeneralRe: Code contracts Pin
Super Lloyd25-Feb-16 16:59
Super Lloyd25-Feb-16 16:59 
AnswerRe: Code contracts Pin
Sander Rossel25-Feb-16 21:43
professionalSander Rossel25-Feb-16 21:43 
GeneralRe: Code contracts Pin
BillWoodruff25-Feb-16 23:03
professionalBillWoodruff25-Feb-16 23:03 
GeneralRe: Code contracts Pin
Sander Rossel26-Feb-16 1:11
professionalSander Rossel26-Feb-16 1:11 
GeneralRe: Code contracts Pin
Super Lloyd26-Feb-16 1:27
Super Lloyd26-Feb-16 1:27 
AnswerRe: Code contracts Pin
BillWoodruff26-Feb-16 0:54
professionalBillWoodruff26-Feb-16 0:54 
Hi: curiosity is an excellent attitude ! You got me curious, so I went to the VS Gallery, downloaded, installed CodeContracts ... only to realize it's already baked into .NET 4.5.

It was pretty easy to set up a test-class with the three key types of contract, Ensures, Requires, ObjectInvariant, and then observe what happened as I enabled, or disabled, the various tests (code at end).

The one thing that concerns me is that every time I compile the project with this test class (after making some change), my AV software is telling me that it is doing direct disk sector access using something called 'ccrewrite.exe, which is a digitally unsigned file:
C#
File information according to the publisher of the detected file (may be faked) (ccrewrite.exe):
Company: Microsoft Corporation
File description: CCRewrite
Copyright: (c) Microsoft Corporation.  All rights reserved.
File version: 1.9.10714.2
While PostSharp may be doing something similar to modify code, I never see notificaition like this (perhaps because PostSharp is using a signed something-or-other ?).

The test class code:
C#
public class testContract
{
    private int? _testInt;
    public int? testInt
    {
        get
        {
            //Contract.Ensures(Contract.Result<int?>() != null);
            return this._testInt;
        }

        set
        {
            Contract.Requires(value != null);
            _testInt = value;
        }
    }

    public testContract(int? testinput)
    {
        testInt = testinput;
    }

    [ContractInvariantMethod]
    void ObjectInvariant()
    {
        //Contract.Invariant(_testInt != null);
    }
}
I note that configuring how CodeContracts will handle validation fails involves a Panel in Project/Properties/Code Contracts that has a complex number of options.

I like the idea of catching errors before they occur ! There's still so much functionality in PostSharp I haven't explored; will be interesting to see what it might/doesn't offer in terms of constraints.

You may have seen my blurb about Jeremy Pinker's interesting 'FluentValidation open-source project here, recently [^], another interesting approach to bomb-proofing code. Of course, I am not implying direct comparison to 'CodeContracts here, since you use 'FluentValidation on 'instances of objects at run-time, not compile-time.

Take a look at this example of 'FluentValidations syntax" (example by the FV author, taken from here: [^]):
C#
public class MyClass {
  public string Id { get; set; }
  public string Name { get; set; }
  public string Address { get; set; }
  public string OldPassword { get; set; }
  public string NewPassword {get;set;}
  public string ConfirmPassword {get;set;}
}

public class MyValidator : AbstractValidator&lt;MyClass&gt; {
  public MyValidator() {
    RuleSet(&quot;NameAddressRuleset&quot;, () =&gt; {
      RuleFor(x =&gt; x.Id).NotEmpty();
      RuleFor(x =&gt; x.Name).NotEmpty();
      RuleFor(x =&gt; x.Address).NotEmpty();
    });

    RuleSet(&quot;PasswordRuleset&quot;, () =&gt; {
      RuleFor(x =&gt; x.OldPassword).NotEmpty();
      RuleFor(x =&gt; x.NewPassword).NotEmpty();
      RuleFor(x =&gt; x.NewPassword).Equal(x =&gt; x.ConfirmPassword);
    });
  }
}

«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”


modified 26-Feb-16 7:12am.

GeneralRe: Code contracts Pin
Super Lloyd26-Feb-16 1:24
Super Lloyd26-Feb-16 1:24 
GeneralRe: Code contracts Pin
BillWoodruff26-Feb-16 20:15
professionalBillWoodruff26-Feb-16 20:15 
GeneralRe: Code contracts Pin
Super Lloyd26-Feb-16 21:17
Super Lloyd26-Feb-16 21:17 
GeneralRe: Code contracts Pin
BillWoodruff26-Feb-16 22:55
professionalBillWoodruff26-Feb-16 22:55 
GeneralRe: Code contracts Pin
BillWoodruff27-Feb-16 4:38
professionalBillWoodruff27-Feb-16 4:38 
General12PM Pin
Kornfeld Eliyahu Peter25-Feb-16 9:46
professionalKornfeld Eliyahu Peter25-Feb-16 9:46 
GeneralRe: 12PM Pin
ZurdoDev25-Feb-16 10:01
professionalZurdoDev25-Feb-16 10:01 
GeneralRe: 12PM Pin
Kornfeld Eliyahu Peter25-Feb-16 10:08
professionalKornfeld Eliyahu Peter25-Feb-16 10:08 
GeneralRe: 12PM Pin
Kevin Marois25-Feb-16 10:41
professionalKevin Marois25-Feb-16 10:41 
GeneralRe: 12PM Pin
Kyle Moyer25-Feb-16 10:59
Kyle Moyer25-Feb-16 10:59 
GeneralRe: 12PM Pin
Foothill25-Feb-16 10:01
professionalFoothill25-Feb-16 10:01 
GeneralRe: 12PM Pin
Kornfeld Eliyahu Peter25-Feb-16 10:09
professionalKornfeld Eliyahu Peter25-Feb-16 10:09 
GeneralRe: 12PM Pin
Richard MacCutchan25-Feb-16 23:27
mveRichard MacCutchan25-Feb-16 23:27 
GeneralRe: 12PM Pin
Kyle Moyer25-Feb-16 10:55
Kyle Moyer25-Feb-16 10:55 
GeneralRe: 12PM Pin
Stefan_Lang25-Feb-16 22:17
Stefan_Lang25-Feb-16 22:17 
GeneralRe: 12PM Pin
Kyle Moyer26-Feb-16 8:15
Kyle Moyer26-Feb-16 8:15 
GeneralRe: 12PM Pin
Stefan_Lang29-Feb-16 0:33
Stefan_Lang29-Feb-16 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.