|
Excellent info Sander, thank you and Peter!
Gotta have a look!
|
|
|
|
|
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:
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:
public class testContract
{
private int? _testInt;
public int? testInt
{
get
{
?>() != 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: [^]):
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<MyClass> {
public MyValidator() {
RuleSet("NameAddressRuleset", () => {
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.Address).NotEmpty();
});
RuleSet("PasswordRuleset", () => {
RuleFor(x => x.OldPassword).NotEmpty();
RuleFor(x => x.NewPassword).NotEmpty();
RuleFor(x => x.NewPassword).Equal(x => 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.
|
|
|
|
|
Thanks for your pioneering work Bill!!!
During work today I used lull to read about code contract. They do support both static and runtime checking! (although I guess you have to run with the compilation attribute to keep them hey?!)
I am still not entirely convince... I will have to see if static compilation has any goodies!
|
|
|
|
|
Hi Super Lloyd,
Perhaps it is less appropriate to give details of a commercial software tool here, given our collective (and valued) "sensitivity" to hidden agendas of "promotion;" but, fyi PostSharp offers its own CodeContracts implementation [^] which includes these validation attributes: (note: the list here may not be complete since it's based on PostSharp version 3)
NotNull Requires a non-null value
NotEmpty Requires a non-null and non-empty string or collection
Required Requires a non-null object or a non-whitespace string
CreditCard Requires a valid credit card number
EmailAddress Requires a valid email address
Phone Requires a valid phone number
RegularExpression Requires a match of a regular expression
StringLength Requires a string of a given length
EnumDataType Requires a valid value for an enumeration (can be applied to strings and integers)
GreaterThan Requires a value greater than a threshold.
LessThan Requires a value less than a threshold.
Positive Requires a value greater or equal to 0.
StrictlyPositive Requires a value strictly greater than 0.
Range Requires a value within a range.
High Performance and Flexibility [^].
PostSharp is expensive, but, imho, a very powerful tool whose facilities go far beyond validation.
cheers, Bill
«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.”
|
|
|
|
|
Hi Bill!
Since I am trying to stir in my careless soul some traces of interest for Contracts..
I am very far away from spending money on it!
|
|
|
|
|
I doubt your soul is "careless," and I bet you do have a concern for validation ... which is what this is all about for we mere mortals who have ... errors ... to deal with
cheers, Bill
«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.”
|
|
|
|
|
Trying to discover what CodeContracts (from MS) can and cannot do when the 'static compile option is set is not easy: [^].
«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.”
|
|
|
|
|
I'm not used to AM/PM notation in time and tried to understand when 27th, February 12PM is in Israel...
One says 12PM is midnight at the END of the day, while other says it is midday...
So between EST and IST there are 7 hours...does it mean that 12PM is 19:00 or 7:00 of the next day?
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
Kornfeld Eliyahu Peter wrote: 12PM 12PM is always noon in the US, or midday. 12 AM is midnight. I've never heard of people refer to it backwards but perhaps some places do.
So, 12:00 PM EST is 7:00 PM in Israel.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
|
RyanDev wrote: 12PM is always noon in the US everywhere
FTFY
If it's not broken, fix it until it is
|
|
|
|
|
RyanDev wrote: I've never heard of people refer to it backwards but perhaps some places do.
Children sometimes do this. I know I did. When you have a limited understanding of time, you tend to think of AM as "Day" and PM as "Night" (obviously it isn't so cut and dry.) Thus, noon becomes AM, and midnight is PM!
|
|
|
|
|
If you're used to using 24 hour time, AM is 0:00 to 11:59 and PM is from 12:00 to 23:59. When thinking time differences, to the east is ahead, so add hours, to the west is behind, so subtract hours. But even that is confusing depending on what point on the globe you are making the comparison from.
if (Object.DividedByZero == true) { Universe.Implode(); }
|
|
|
|
|
Thank you!
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
And if you are on the International Date Line it's tomorrow or yesterday, depending which way you are facing.
|
|
|
|
|
To supplement what others have said:
AM and PM are derived from latin. AM being Ante Meridiem or before mid-day, and PM being Post Meridiem or after mid-day.
If you take that 12:00 midnight is considered the first minute of the day, that makes midnight 12AM, as it is occurring before mid-day. 12:00 noon therefore must be the opposite, so it is 12PM. (Mid-day itself is technically the instant between 11:59:59AM and 12:00:00PM, and doesn't have an actual specific time value.)
Hope that helps clear things up.
|
|
|
|
|
Kyle Moyer wrote: If you take that 12:00 midnight is considered the first minute of the day, that makes midnight 12AM
Doesn't make sense to me: why am I supposed to assume that 12 indicates the first hour of the day? Why not call it 0:00 AM? Think about it: 10 AM is the 11th hour of the morning, 11 AM is the 12th hour, so a consistent interpretation of 12 AM would be the 13th hour. Likewise, 2 AM is the third hour, 1 AM the second, so a consistent naming scheme would call the first hour 0 AM.
Unfortunately that is not how AM/PM works.
The truth is that the numerical and the AM/PM part are referring to different time ranges, and that is what is causing the confusion. It becomes clearer when you visualize the actual time intervals of the 12h clock alongside the 24h clock and the AM/PM indicator range:
hour: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
24h: |<--------------------------------------------------------------------->|
12h(1): |<---------------------------------->|
12h(2): -->| |<-------------------------------
AM: |<---------------AM---------------->|
PM: |<---------------PM---------------->|
There is a 1 hour offset between the frames of reference for the numerical part and the AM/PM indicator. It doesn't make sense however you try to explain it, but as long as you keep that image in your mind you should be able to correctly interpret an AM/PM time reference.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Stefan_Lang wrote: Doesn't make sense to me: why am I supposed to assume that 12 indicates the first hour of the day? Why not call it 0:00 AM?
For consistency. Both the 24 hour clock and the 12 hour clock tell you how much time has passed since some event. So at 1:00AM, one hour has passed since the day began. If you split the day in half to make a clock easier to read, then you get 12 hours per half. So this means mid-day occurs once 12 hours have elapsed since the day began. You call that 12:00, and then start counting again. Since a 12 hour clock doesn't have a 13 on it, 13 hours past the start of the day is 1:00PM, or 1 hour past mid-day. Once you reach midnight again, you have 12 hours since mid-day, so you start over just the same. You could use 0:00AM and 0:00PM, but all natural counting systems start at 1, so zero is skipped.
Stefan_Lang wrote: Think about it: 10 AM is the 11th hour of the morning, 11 AM is the 12th hour, so a consistent interpretation of 12 AM would be the 13th hour.
10:00AM is the beginning of the eleventh hour of the day, but 11 hours have not elapsed. The clock measures elapsed time.
Stefan_Lang wrote: so a consistent naming scheme would call the first hour 0 AM.
I agree that it should be consistent. But since we are measuring elapsed time, it is more natural to say that 12 hours have elapsed since mid-day or mid-night, than it is to say zero hours have. Natural counting systems start at 1 for a reason; if there is zero of something, you have no reason to count it at all.
The reason that 0:00 is used for the 24 hour system is much the same as why 12:00 is used for the 12 hour system. You're still counting elapsed time since an event, but here everything is relative to start of the day. So saying 24:00 (AM) is 24 hours elapsed since yesterday make you look rather foolish. Of course 24 hours have elapsed since yesterday, one day has 24 hours!
Your diagram also over generalizes things, and makes it look like mid-day is both AM and PM. It isn't. Mid-day isn't even a time (on a clock, it has no numbers.) It's an instantaneous event that occurs once half of the time in the day has elapsed. 12 hours. This occurs in the moment between 11:59:59(.999...)AM and 12:00:00PM. So really you need a different diagram for AM/PM, and should look like this:
Time: 0:00:00 -------------------- 11:59:59 !Mid-Day! 12:00:00 ------------------- 23:59:59
AM: |<---------------AM---------------->|
PM: |<---------------PM---------------->|
Stefan_Lang wrote: It doesn't make sense however you try to explain it
It makes perfect sense to me. You just have to understand that the clock is measuring elapsed time, and that humans don't start naturally counting at zero.
|
|
|
|
|
Kyle Moyer wrote: 10:00AM is the beginning of the eleventh hour of the day, but 11 hours have not elapsed. The clock measures elapsed time.
Yup. I have considered that. That makes perfect sense: in the 24h clock, "01:00" means "1 hour elapsed since the start of the day", so does it in the 12h clock notation.
But, unfortunately, it breaks down during the first hour after mid-night and mid-day: "00:43" does mean "43 minutes elapsed since the start of the day" in the 24h clock notation - the 12h clock expresses this time as "12:43 AM" though.
You can interpret this as "12h and 43m elapsed since mid-day of the previous day", but 'AM' means "this is a time before mid-day of the current day". So, the notation "12:43 AM" mixes two different frames of reference.
Kyle Moyer wrote: You just have to understand that [...] humans don't start naturally counting at zero.
I get your point, but when I think about it, there's more than that: the original 12h clock notation probably comes from a time when we were hardly able to accurately tell the hour of the day, much less the minute. Also, people talked of the current time in terms of 'this is the third hour', referring, vaguely, to some point in time after the second, but before the (end of the) third hour had passed. Back in that time, you only ever referred to the 1st through 12th hour before mid-day (AM) or after mid-day (PM) - there was no inconsistency. Yet.
The 'problem' with the 2x12h notation probably started arising when people began measuring time between hours: to express that half an hour has passed since mid-day, in a time when '0' wasn't used or even invented, what people said instead is that 'half an hour has passed since the twelfth hour (of the morning)'. This little extension seemed natural enough, so nobody spotted (or cared about) the resulting inconsistency.
At least that's how the current notation might have come about - just a theory...
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
AM comes before PM in the alphabet and also in the day
|
|
|
|
|
But at midnight AM comes after PM.
|
|
|
|
|
Only if you count over days, which you shouldn't
My logic is infallible
|
|
|
|
|
Sander Rossel wrote: My logic is infallible Well at least it's logical, Captain.
|
|
|
|
|
I just really wanted to use that word
|
|
|
|
|
Just looking at one of your blogs at random and found a nice piece of LINQ that I actually understood.
|
|
|
|
|