Posts

Optional Parameters and Method Overloads

The ability to declare optional parameters is another feature that will be introduced in C# 4.0. A natural complement to method overloads, it generally helps code to be more concise. Until now, method overloads have been used for the awkward responsibility of creating default values for what otherwise would be much better expressed as optional parameters. Many overloads have been created for which the sole responsibility is to call another overload with some default values for some parameters. public CreditScore CheckCredit() { return CheckCredit(false, true); } public CreditScore CheckCredit(bool useHistoricalData) { return CheckCredit(useHistoricalData, true); } public CreditScore CheckCredit( bool useHistoricalData, bool useStrongHeuristics) { // do the heavy-lifting ... } For this style of method, optional parameters let you create just one method, with default values set on its signature. public CreditScore CheckCredit( bool useHistoricalData = false, bool u...

Named Parameters Increase Readability

When reading C# code, sometimes there are method calls which aren't readily obvious. The method can have too many parameters, or a series of parameters of the same type. This can make things a little confusing. A more aggravating case is that of boolean parameters. It's hard to find out what true or false mean in some cases. var creditScore = customer.CheckCredit(false, true); Normally, we need to look at the method signature to understand how it's being called, and what each parameter represents. public CreditScore CheckCredit( bool useHistoricalData, bool useStrongHeuristics) { ... } Since code is more often read than it's written, any means to improve readability will have a direct effect on maintainability. Every line of code should clearly communicate its intent. No clever hacks or streams of unintelligible parameters. A good solution to the boolean case is to use enumerations. This way, we force the caller to use a well known name for the param...

F#: Why the rec?

I've been looking into F# for a while now, and I think it's an awesome addition to the .NET framework. It's more expressive and concise than C#. It's functional by heart, with discriminated unions, pattern matching, tail-call optimization, and it also supports object oriented programming (being a .NET language, it just had to) and imperative programming. Its type inference creates the feel of a dynamic language, but in a strongly-typed language (much like in Haskell and Scala). That said, one of the first things that I noticed is that in order to declare a recursive function you have to be explicit about it in the function definition. You have to declare it with the rec keyword. So, this is a non-recursive function: let add a b = a + b The function name is add, and it takes 2 (inferred) integer parameters, a and b, and returns their sum. You call it like this: add 1 2 // returns 3 Now, this is a recursive function: let rec add_all l = match l ...

Painful SQL Server Pagination

When I used MySQL back in 2000, I took for granted the ability to paginate result sets in the server with the simple LIMIT clause. (Other open source databases, like Firebird and PostgreSQL, have similar constructs.) Querying for a page of the result set is as simple as this: # Fetches rows 21-30 # 20 is an offset, 10 is the number of records to fetch select * from Users limit 20, 10; You should only return from the server the actual records that the application will use, and that normally means a small chunk of records at a time. To me, the ability to easily page through records is a fundamental requirement of a database server. I had a big surprise when I first used SQL Server 2000, because it didn't have a standard way of paginating records. To paginate on the server, there were all kinds of convoluted nested queries or costly temporary tables. These operations were expensive for the database to process and difficult for the developers to create and maintain. Assumin...

C# Extension Methods and Null References

With extension methods in C# 3.0, you can create methods that operate as if they were instance methods of other classes. You can "add" methods to any class, even to core classes like System.String and System.Object . This example creates two string extensions: public static class StringExtensions { public static string HtmlEncode(this string input) { return AntiXss.HtmlEncode(input); } public static int ToInt32(this string s) { return Int32.Parse(s); } } (I used the HtmlEncode method implemented in the Anti-Cross Site Scripting Library .) The way to use extension methods is to call them as if they were instances of the extended class itself (given as the first parameter to the extension methods): int input = "123".ToInt32(); ... string safeString = unsafeString.HtmlEncode(); Or you can use them as normal static methods (it has the same effect as above): int input = StringExtensions.ToInt32("123"); ... string safeStri...