More About Code Contracts

In a recent post I wrote about Code Contracts in .NET. Now you can find a more detailed article on this topic at sharparena.com. In this article I’m providing more information and examples on: pre-conditions post-conditions object invariants asserts and assumptions quantifiers In additions, you should check the official user documentation, which can be found…

Code Contracts in Visual Studio 2010

Visual Studio 2010 has support for code contracts that allow to express pre-, post-conditions and invariants to your .NET code. Let’ say you want to create a function to return a random value in a range. This could look like it:

However, at a rough analysis one can find two problems: Second call to…

Avoid using directives in header files

It is often that I see people using namespace directives in header files. This is a bad practice maybe not enought explained, so I will try to clarify why one should always avoid this. When you are using a using directive (such as using namespace std) or using declarations (such as using std::cout) you are…

Defining One Entry Point to Write a Variable

I’ve found myself in situations when I spent lots of time debugging because of some variables declared in a base class were written in tens or maybe hundreds of places in the whole hierarchy, across one or multiple projects even. How could you find the right place where the value of such a variable changes?…

Task Dialog in MFC

The new MFC library from Visual Studo 2010 supports another Vista specific feature, the task dialog. This is meant as a replacement for the classic dialog box, and can display command links, customized buttons, customized icons, and a footer. Here is an example of such a dialog (from Windows Server 2008). MFC contains a new…

Restart Manager in MFC

One of the new features available in MFC in the Visual Studio 2010 CTP is the Restart Manager. This was introduced with Windows Vista to offer support for restarting application when a crash occurs or when an automatic update needs to close and then restart an application. When you create a new MFC project in…

BigInteger Is Back

One of the new features in .NET 4.0 is the BigInteger class. This was suppose to be part of 3.5 framework, but after the CTP it was gone. Now it’s back. This big integer is a really large integer representation. However, I was not able to find the precision documented anywhere. The class is available…

C++ static_assert, a niche feature

The new C++ standard defines a new keyword, static_assert, that is already available in Visual Studio 2010 CTP. This new feature allows introducing compile time asserts. It takes an expression that can evaluate to bool and a string. If the expression evaluates to false, the compiler issues an error with the given string literal. If…

Lambdas in C++

The new C++0x standard adds lambda expressions to the language. Visual Studio 2010 CTP is already supporting this new feature that brings functional techniques to C++ too. What is a lambda expression? It’s basically a function. In F# it’s an anonymous function, in C# it’s an anonymous delegate; in C++ it’s in fact an anonymous…

Optional and Named Parameters in C# 4.0

So far, C#, unlike C++, did not support optional arguments. For instance, suppose you need a function to print log a message, that can add a new line or not after writing the message. Most of the times you want a new line, so you don’t want to specify that for most of the calls….