Posts

Showing posts from October, 2012

Code Contracts by Example

So how does one get his feet wet with Design-by-Contract? I remember when I first started using Microsoft Code Contracts I immediately made a huge mess of my projects from which it took several weeks to fully recover. After a while I figured out what worked and what didn't and I've developed a habitual way of doing things that seems to work okay. I won't go so far as to say they are best practices, but they keep me out of trouble. So without delving too deep into the theory of DbC, here are a few of the patterns I use. Constructor-based Dependency Injection While property-based DI is possible with code contracts, it gets ugly really quickly. The only way I've been able to make it work is with a backing field and explicit null checks in the getter. Basically, if you have a public setter then all bets are off as far as invariants are concerned. Constructor-based injection, on the other hand, is very clean and straightforward, and allows you to offload some of

More on Non-nullable Reference Types

I was thinking about this some more today, and it occurred to me that a very simple construct could achieve what is being asked for without making any changes to the language or tools. Nullable value types (structs) were first implemented with an explicit wrapper class; it wasn't until later that the ValueType ? shorthand appeared. With implicit operators, we could basically do the same for non-nullable reference types: public struct NotNull<T> : IEquatable<T>, IEquatable<NotNull&ltT>> where T: class { private T _value; public NotNull(T value) { if (value == null) { throw new ArgumentNullException("value"); } _value = value; } public T Dereference() { if (_value == null) { throw new NullReferenceException(); } return _value; } public override bool Equals(object other) { if (ReferenceEquals(null, other))

Non-nullable Reference Types

A few days ago on InfoQ I saw a link to this blog post offering a solution to "The Billion Dollar Mistake" : allowing null references. The proposal, non-nullable reference types, is designed to replace this: /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="argument"/> is null. /// </exception> public void Method(SomeType argument) { if (argument == null) { throw ArgumentNullException("argument"); } // Do stuff... } With something like this: public void Method(SomeType! argument) { // Do stuff... } Of course complications arise with arrays of non-nullable reference types and with the methods that feature non-nullable output parameters. There are workarounds for those issues, but the result is a convoluted syntax and unnecessary overhead. But I think the main problem is that we're looking at the problem from the wrong direction. I think we have to accept

Code Formatting in Blogger

I've several hours of research, trial, and error, I've finally come across a workable solution for formatting code snippets in Blogger. Here are the basic steps: From your dashboard view, click "Template" and then "Edit HTML".  A modal confirmation dialog will appear.  Click "Proceed". Find the <head> tag and insert the following on the next line: <link href='https://sites.google.com/site/itswadesh/codehighlighter/shCore.css' rel='stylesheet' type='text/css' /> <link href='http://sites.google.com/site/itswadesh/codehighlighter/shThemeDefault.css' rel='stylesheet' type='text/css' /> <script src='http://sites.google.com/site/itswadesh/codehighlighter/shCore.js' type='text/javascript' /> <script src='http://sites.google.com/site/itswadesh/codehighlighter/shBrushCSharp.js' type='text/javascript' /> <script src='http: