Got more questions? Find advice on: SQL | XML | Regular Expressions | Windows
in Search
Welcome to AspAdvice Sign in | Join | Help

C# Nuggets

  • Must have SOLID experience. SOLID essential

    There is a massive and growing industry trend to use a set of principles called the SOLID principles. If you haven't come across these, go and read up on them. These principles were put together by Robert C. Martin (aka Uncle Bob) and popularized in his book called Agile PPP.

    Now, Uncle Bob isn't one for being shy or self-deprecating. His presentation style is in your face. This is what makes him immensely entertaining. It is also what makes his audience believe his philosophy is black-and-white without a hint of pragmatism. They believe the SOLID principles should be used EVERYWHERE. That the abstraction promoted by SOLID is a noble goal. That the SOLID principles are the pinnacle of Object Oriented Design.

    They are wrong.

    They have either not read, or mis-read his Agile PPP book. They have not read the numerous insights Uncle Bob has written on the subject. So let me help by quoting Uncle Bob. If these are a surprise to you, PLEASE temper your love of SOLID and go read up on Primitive Obsession.

    I quite agree that a dogmatic or religious application of the SOLID principles is neither realistic nor beneficial. If you had read through any of the articles and/or books I’ve written on these principles over the last 15 years, you’d have found that I don’t recommend the religious or dogmatic approach you blamed me for. In short, you jumped to a erroneous conclusion about me, and about the principles, because you weren’t familiar with the material.

    An axis of change is an axis of change only if the changes occur. It is not wise to apply SRP - or any other principle, for that matter - if there is no symptom.
    Page 118, Agile Principles, Patterns and Practices in C#

    Clearly, we want to limit the application of OCP to changes that are likely.

    Page 129, Agile Principles, Patterns and Practices in C#

    Nor is it a good idea to apply rampant abstraction to every part of the application. Rather, it requires a dedication on the part of the developers to apply abstraction only to those parts of the program that exhibit frequent change. Resisting premature abstraction is as important as abstraction itself
    Page 132, Agile Principles, Patterns and Practices in C#
  • Concurrency bugs in the CLR JIT

    A recent paper by Microsoft research has discovered a concurrency bug in the .Net JIT. They used the F# theorem prover to analyze the JIT il->x86 transformations which were previously thought to comply with the .Net memory model. These bugs show that writing correct concurrent (multi-threaded) code is hard - and in a multi-core world we need to find a way to provably write correct code.

    "This discovery means that the current CLR JIT compiler for the x86 platform is not correct; it will be fixed in the future by strictly emitting locked instructions for all volatile stores"


    via LtU


  • JavaScript performance

    Recently, the browser vendors have been making serious gains in JavaScript performance. A week ago, Google launched their web browser called Chrome with much fanfare. Chrome is based on the WebKit rendering engine which underpins Apple's Safari browser and the iPhone browser, but using a brand new Just-in-time (JIT) compiling JavaScript virtual machine. This JIT virtual machine is called V8 and was created over the past 18 months by a team led by Lars Bak who created the Java Hotspot virtual machine. The Chrome browser is Open Source in the form of code going by the name Chromium.

    Interestingly, it turns out that Apple and Mozilla have spent the past few months building JITs for their JavaScript engines. Apple have an engine called JavaScriptCore, while Mozilla have an engine called SpiderMonkey. The JITs they have built are based on these existing engines but have been given new names. Apple's is called Squirrelfish-Extreme (SFX) while Mozilla's is called TraceMonkey.

    Each of these engines seem to have unique features. V8 has what are called 'hidden class transitions' which are simply class definitions inferred at runtime. As class definitions change infrequently, this means that V8 has very fast class member lookup. TraceMonkey uses a novel technique called Tracing. Instead of compiling a method 'just in time' and then executing the compiled machine code, TraceMonkey interprets everything up until it detects a loop. At this point, it will compile the loop. So whereas traditional JIT compiling will compile code which potentially never gets run, Tracing will only ever compile code that gets run. Webkit's SFX has borrowed a trick from .Net and will compile any regular expressions into machine code. Their Webkit Regular Expression Compiler (WREC) is why SFX beats everyone else hands down on regular expression performance.

    It has been a hectic 2 weeks. In that time, there have been various claims that first TraceMonkey was the fastest. Then V8 claimed they were the fastest. And over the past couple of days there have been claims that Webkit's SFX is the fastest. So who is right? Well, many factors are involved, and all three engines are still very much in active development. So the lead could shift around yet again.

    To try and get a handle on the current state, I ran a benchmark called SunSpider. This is a JavaScript benchmark which does not test HTML DOM performance, therefore giving a true picture of pure JavaScript virtual machine speed.

    The headline figure is:

    Webkit (ie Safari) nightly builds are 46% faster than Firefox nightly builds. Chrome is 7% faster than Firefox.

    However, the above statement does not give the whole picture. It appears that Safari's SFX JIT is not fully enabled on Windows. Recent Webkit checkins indicate that the Windows JIT is still being worked on. As Chrome is currently a Windows-only browser, it is impossible to compare Webkit-SFX with Chrome. Therefore, the 46% figure advantage Webkit has over firefox was obtained on OSX 10.5.

    Microsoft have been working on a JavaScript JIT for a couple of years now. It's called Managed JScript and runs on top of the Dynamic Language Runtime (DLR). However, there has been no news from this project for a long while and it certainly won't be included in IE8. The old ActiveScripting-based JScript engine in IE8 has been tweaked a little for performance but lags a long way behind the other vendors. This is a real shame as Microsoft have some world-class virtual machine architects.

    : Sunspider 0.9

    Windows XP SP2 results (Core2 Duo, T7300/2Ghz)
    Chromium: 2747.8ms +/- 2%
    Firefox: 2941.4ms +/- 4.3% (1.07x as slow)
    webkit: 3124.6ms +/-3.3% (1.14x as slow)

    OSX 10.5.4 results (Core2 Duo, 2.8Ghz)
    Firefox: 1361ms +/- 2.7%
    webkit: 931ms +/- 2.3% (1.46x as fast)

    Browsers used were the latest nightlies:

    Win32 Firefox 3.1b1pre Gecko/20080910043000 (Content JIT=on)
    OSX Firefox 3.1b1pre Gecko/20080910020330 (Content JIT=on)
    Webkit r36309 using Squirrelfish-Extreme (SFX)

  • Fixup candidates for .Net v4

    The C# team has been fairly candid in the features they are considering for C# v4. You just have to look hard enough. We'll know what they decide on soon enough as the PDC is just around the corner. Declarative side-effect free methods, Contra/covariance of arguments/return values, Declarative dynamic dispatch (VB-style object access). These all sound great. Perhaps I can add a few suggestions of my own not just for C# but for .Net in general:

    A few ideas for .Net vnext:

    • String.Join should work on IEnumerable<string> not just arrays.
    • C# enums should be OO just like Java 5.0.
    • Forget about the ThreadPool. We need a resource-management WorkerPool. Something that manages a task based not just on availability of threads, but of memory and IO. There's no point in throttling threads if you're task/agent is memory or IO bound.
    • C# null propagation (lifted member access). Ian Griffiths has by far the best description I've ever seen. I'd love to use something like var x = foo() where the double period (..) means propagate nulls.
    • Server-side CSS parsing, optimization, minimizing.

     As for Visual Studio:

    • Collaborative Editing (cross network pair-programming). Eclipse does it. SubEthaEdit does it.
    • Much better JavaScript type inferrence in the IDE.
    • MSTest is too tightly coupled to the IDE. And it shows. MSTest is needy in many areas. There are 2 .Net Unit Testing pioneers at Microsoft. It's a wonder they got it so wrong.
    • Sort out Edit n Continue so it actually works when I modify an iterator, anonymous method etc. We're still a long long way from the glorious REPL in VB1-6.
  • When does FlagsAttribute not mean Flags?

    In the System.Data namespace, there's a ConnectionState enumeration. This has values like Open, Closed, Connecting, Fetching to represent the state of a database connection.

    I pondered on whether a connection which is "Fetching" is also "Open". It turns out that ConnectionState is declared with the FlagsAttribute - The mono DocComment for this enumeration says it best:

    /// This enumeration has a FlagsAttribute that allows a bitwise combination of its member values.
     Great - so now I can do something like:
    if((connection.State & ConectionState.Open)==ConnectionState.Open) ...

    Wrong! Further down the MSDN documentation for ConnectionState, tucked away in the Remarks section is the following paragraph:

    The values in this enumeration are not designed to be used as a set of flags.

    Great. So the question is, which of the database providers follow the FlagsAttribute, and which follow the documentation comment? Does SQL Server treat ConnectionState as flags? Does the managed Oracle provider? Does Oracle's ODP.Net provider treat it as flags? etc.

    Certainly there are many people who treat it like flags. There are equally many people who ignore it.

    I think I know this history behind this debacle - if anyone knows for sure, I'd like to know. If you actually look at the ConnectionState definition, you will see that ConnectionState.Closed has a value of 0. This flouts the .Net Design Guidelines and I expect it to be raised by FxCop/CodeAnalysis. Go ahead and read that link if you're not sure why bitwise comparison with 0 is a bad idea. This means there are a lot of bugs about due to the use of Bitwise comparison of ConnectionState.Closed. So I suspect that the System.Data team realized they'd made a mistake and was unable to retroactively remove the FlagsAttribute - so their approach was to document the issue in an obscure remark.

  • The CLR garbage collector is in need of help

    Back in 2000 when the CLR was first shown it's generational garbage collector was fairly cutting edge. But it's weaknesses show it's age - and it desperately needs updating.

    Here's an example of just one of the ways the current GC (.Net 3.5) doesn't work:

    A .Net process with a number of threads has just saved a large amount of data to a database. All of these threads could potentially allocate memory which causes a GC collection. However, saving the data to the database is a fairly long operation and thus the data gets promoted into the GC's generation 2.

    After saving the data, the .Net process waits until it's instructed to do some more work. However, because a large amount of data has been promoted into Gen2, it just sits there until sufficient new allocations cause a Gen2 GC collection. The problem is that if no new work happens for a long time (hours), these unreachable objects take up (virtual) address space. Yes, NT will swap it out to disk as virtual memory. But it's still there.

    The GC really needs a timer, which periodically checks if no GCs have happened recently and the process has consumed very little CPU, then perform a Gen2 collection.


    Aside: Does anyone know how to trigger a GC.Collect() on a process from a different process, or from inside WinDbg? One of the problems with WinDbg/SOS is figuring out just which objects are reachable - you would have to !gcroot every possible object just to figure out which are the reachable ones.

  • Windows Geniune Disadvantage

    Windows Genuine Advantage
    Code not available. The validation code could not be obtained. This may be due to technical difficulties, or you may be running an unsupported operating system. Please close this window and attempt the validation process again, or use the Back button in your Web browser to return to the download details page. [Error code: 0x80072ee7]

    Why does Microsoft insist on making my life difficult?

  • Refactoring

    I'm doing a lot of porting and refactoring at the moment. I've come to the conclusion that porting from a dynamically typed language to a statically typed language is an order of magnitude more difficult than doing the reverse.

    Refactoring is an interesting game. There are some very easy refactorings - especially with the toolsets available to us nowadays. However, there are some extremely difficult refactorings. I've found the most difficult to refactor methods have one or more of these 3 aspects:

    1) It has long methods (>40 lines)
    2) The method shares a variable for different things
    3) Loops do more than 1 thing

    1) causes 2) - so eliminating 1) means that 2) doesn't happen.

    I sure wish the NextBigLanguage has an upper bound on the lines/method it can parse.

  • Code Smells in C#

    A lot of my development is done with Code Smells. Whether I'm working on my own code or refactoring someone elses code, if I see some code and it smells funny, then I investigate further.

    Recently I've come to realize there's a code smell which we can blame Microsoft (Anders) for! 

    C# Code Regions. 

    Back in the early days of .Net (ie 2000-2003), I used to moderately region my code. But since reading Martin Fowler's seminal Refactoring book where the concept of Code Smells was introduced, I found that I didn't need C# Regions.

    So why are regions a Code Smell?

    There are two types of regioning I've seen in code. Regions that group methods, properties, member variables, constructors etc together, and those which collapse a block of code. I'll deal with each separately.

    Using Regions to group members.

    In many companies, its commonplace (and even enforced practice) to group similar class members together and place a region block around them. I've even seen this as a coding doctrine inside Microsoft. The problem is that Visual Studio already has a facility to do this automatically. And it's been around in VS since it was called VJ++ and before that in VC. It's called the Class View window, accessed with the shift-ctrl-C shortcut. So why anyone would care about building a class "DOM" into their code using Regions, rather than rely on the Class View window I have no idea. Perhaps they're using Notepad?

    Using Regions to hide blocks within a method.

    This is when it gets really nasty. I've seen multi-hundred line long methods containing regions, and subregions and sub-subregions. I've seen switch() statements wrapped in a region, then each individual case have a region, then subregions inside of the region inside of the case. Switch statements are already a Code Smell, but having region after region after region is a sure-fire way of telling you the method is too long.

    So, please, can we all resolve to stop using Regions?

    Interestingly, there are many features that Java has started copying from C# - but regions were not one of those. Perhaps in C# v4 Microsoft can right the wrong and deprecate regions?

  • Caching and SOAP

    If you're making a SOAP request (in this case with the SOAP toolkit), and you get: 

    "WSDLReader:Loading of the WSDL file failed HRESULT=0x80070057: The parameter is incorrect."

    during the MSSoapInit() call, check that you haven't added a cache-control:no-cache custom header to IIS, or that HttpExpires isn't set to zero days.

  • Silverlight Sudoku with LINQ

    A couple of months ago I came across a very clever Sudoku solver written by Peter Norvig. Peter had written the solver using Python. The solver was special as it solved by constraint propagation using list comprehensions and generator expressions making it extremely fast and very memory frugal. It turns out that the new LINQ feature of C# v3 is basically list comprehensions and generator expressions by another name.

    Brendan Eich, the creator of JavaScript had ported the original Python implementation to JavaScript and I thought it would be a good idea to try to port it to C# v3. You can check out the code here.

    I always had a vague plan to wrap the solver in a Silverlight front-end for added WOW factor, but it never really materialized. Fast forward two months and David Anson posts a Silverlight-based Sudoku game on his blog. This gave me the perfect excuse to integrate the LINQ-based solver into his codebase. I extended David's code to respond to the Escape key and complete the board with the first solution found. You can play with it online here, or download the code here. Have fun!


     To solve a game, give the Silverlight board the input focus and hit the escape key.


  • Silverlight crashing

    So I'm sat here watching the Mix07 keynote, 45 minutes after Ray Ozzie first got to the stage. I download the Silverlight Beta for Mac and install it.

    Firefox has crashed 4 times while trying to view the Silverlight demos on This is getting really frustrating trying to review the new features.

  • Silverlight

    As has been blogged all over the place, WPF/E has been renamed Silverlight (Note: lower case 'L').

    Some people say there's more to come at MIX07. Well, I think I've discovered what the secret is. It's hidden inside of the Silverlight promo video. Silverlight is written in Java! Don't believe me? Take a look at a frame grab from the promo video, clearly showing Silverlight's use of SAX, the javax namespace and use of Java API's like vector.addElement.

  • The trouble with dynamic languages

    Javascript is a dynamic language. And that's a problem. On the one hand it allows you virtually unlimited ability to play with data structures and mold them to your own design. On the other, it presents the virtually insurmountable problem of testability.

    You see, a dynamic language delays the checks which are performed by a static language during compilation until runtime. So you have no idea whether your code is correct until it is executed. The usual answer to this is to write unit tests. But the people who say this tend to gloss over the difficulty of getting 100% unit test coverage. 70% coverage is usually considered a very high test rate. And then there's the difficulty of getting the coverage metrics themselves. At the time of writing, there is only one Javascript/browser code coverage tool around. Hopefully all the hype surrounding AJAX will encourage more of these tools to be written.

    One tool that is available now is JavascriptLint. Lint was the tool you may have used many years ago to statically analyze your C source code for common programming errors not picked up by the compiler. So JavascriptLint does the same but for Javascript. It's based on SpiderMonkey, the C-based Javascript engine in Firefox. So you can be sure it's rock solid with zero false positives. It's a fantastic tool to help AJAX programming as it seamlessly integrates into your favorite IDE as an external tool. Some of the errors it picks up on are:

    Some of these are only available with the tool in Option Explicit mode. Even if it doesn't spot outright bugs in your code, the checks for type coercion and missing semicolons are performance bugs in your code.

    The benefits of JavascriptLint are so conclusive it gets run over my code several times per day.


  • What language was ASP.Net originally written in?

    In this video, Peter Hallam, one of the original C# designers talks about a new C# v3 feature, but also says that WinForms was the original consumer of the C# language.

    This totally contradicts my understanding. I thought that ASP.Net (XSP) was the original team using C# and that WinForms (WFC as it was) was only ported to C# from Java over the winter of 1999/2000 by Chris Anderson.

    Am I wrong?


More Posts Next page »