Writing better code


A key factor to being a successful developer is writing good, solid, maintainable, error free code. It seems like a pretty obvious – albeit innocuous – statement, so why state the obvious? When you check code into the main branch of the source code for your organisation, you immediately expose your abilities and disabilities as a developer to the people you work with and for. If you check in a bad bit of code that contains bugs, it usually isn’t you that first notices the bug, but a colleague and you can look inept, especially if it is a silly mistake (and you have made a few of them – your boss may start thinking you are inapt to continue in your role in the company).

Writing good code is very difficult, especially when the code base is changing rapidly so you as a developer need tools that can make your life easier. The more experienced you become as a developer, the more you look to ways of making your life easier. Jetbrains have a visual studio add-in called ReSharper that is a little like FxCop, which the smarter developers amongst us use. If you are wondering why some developers seem to produce very clean code consistently that omits silly bugs (even on a Friday afternoon), you will more than likely find that they are using ReSharper.

I don’t doubt that a lot of people may dislike the fact that this takes over your IDE (this really is for the better though), in fact, If I recollect correctly I tried ReSharper a few times, then removed it (after a few word of mouth trials) every time because my IDE was less responsive, and because I was impetuous. It was only when I released a test application to a customer a few months ago (that kept crashing unexpectedly), when desperation set in, and drove me (frantically) looking for errors in my code. The main culprit  (in this specific instance) was a handful of null references in the codebase that had gone unnoticed, that ReSharper readily identified and I corrected, consequently achieving rapid application stability, over the course of a few days long code review. I’m not saying that all my problems were fixed, but most of the niggling issues were, with little to no effort.

It is not just null checks that ReSharper identifies, but a pretty comprehensive set of common coding mistakes that even the best programmers frequently make. The ‘Big Win’ is in that you get to correct problems there and then, as the code is written, before it becomes an issue. Visual Studio 2008 introduced background compilation that surreptitiously increases developer productivity by showing them errors as the code is typed, rather than waiting to press F5. ReSharper is orders of magnitude more helpful, if you use it correctly, you end up with a very clean codebase, with no unnecessary code, that has had common errors identified, giving you the developer confidence in your code.

To show a few basic examples

22-05-2010 15-16-47

Here I can remove the redundant delegate constructor call that the IDE generates. I can also remove the this keyword. The this keyword is rather abused in C# (Me in Visual Basic) and a lot of code samples (even from Microsoft) show developers misguided bliss with the word. One only ever needs to use the word when you have two variables named the same in a class (typically in a constructor) like this

22-05-2010 15-26-58

In most other cases it is redundant to use the word.

22-05-2010 15-21-35

In the sample above there are several issues. I have dead namespaces at the top, a poorly named control, two poorly named variables, that can also be made readonly. This is just an iota of the static runtime assistance that you get as a developer, and you can adopt the mantra of “writing a little code that does a lot”, rather than “writing a lot of code that does a lot”, with significantly less bugs.

I strongly encourage you to download this tool, acquaint yourself with it, and start working a little smarter (even on Friday afternoon).

Free WPF Themes


Update 1/10/2010 : Reuxables have now released some free themes including a lightweight version of Gemini (below) that has just the two colours. I have a sample application here showing the theme in an application I have just released.

Update : As rightly pointed out by a reader, Reuxables have stopped their free offer as they are now concentrating on the new .NET 4.0 controls and themes, including the elegant Gemini to be released soon.

Gemini

It seems the only freely available themes are the ones one codeplex. These were originally designed by the reuxables team, but I have lost faith in them with the withdrawal of the free themes they had available.

I think the best free themes at the moment are the Silverlight 4 themes, but they are yet to be ported to WPF.

Original Post

A little while back I noted that reuxables were gathering steam. Slightly earlier that year, we were all wowed by the Lawson Mango application

lawson_m3_cs_1

as this was a clear illustration that WPF could offer a vector based visual experience that left your ‘jaw on the floor’. Reuxables have now released a free version of the above theme called Inc

inc

I have been using this in my WPF applications, and have grown rather quite fond of it, principally because WPF themes can be a bit ‘too much’ for applications that one uses daily. You can get the theme here (there is also a free Metal theme [below] if that takes your fancy)

Metal

Improve the quality of your code with NDepend


Before a parachutist, mountain climber or formula one driver undertake their tasks, a number of checks need to be undertaken. These checks will assist in deciding whether the parachutist for instance will even jump out of the plane. If a typhoon is on the way, all are likely to wait until it has passed, the type of climbing gear the mountain climber will choose, must depend on the type of mountain they are to climb. What all these checks result in, is less surprises, in computer programming this is analogous to bugs.

The Ironbridge Shropshire

I know that architecture is ubiquitously used to describe engineering software, but the comparison is indeed an irresistible one. Take the Ironbridge in Shropshire for example, every element of this engineering feat must have been stress tested. The materials must have been tested to see how they react to temperature changes, weight and so forth. You can even find out how many rivets they used, and the length of the steel used in the bridge, all wonderful and useful information. Shropshire bridge (as I’m sure your well aware) is not the only bridge in the world, as there are a great many others that come in different shapes and sizes and age. The design process for a lot of these constructions requires a similar set of circumstances and considerations. A formula one motor car for example, still has four wheels as does a mini cooper. Writing software is no different, and most if not all software projects entail a similar set of circumstances and considerations, whether massive, big or small, you still want to deliver a fast, efficient, reliable and effective application.

I have recently started using NDepend, to quote from their website;

NDepend is a tool that simplifies managing a complex .NET code base. Architects and developers can analyze code structure, specify design rules, plan massive refactoring, do effective code reviews and master evolution by comparing different versions of the code

NDependMain

If you prefer the ribbon, you can change the user interface to suit

Ribbon

The way I look at my code has been changed forever. What you get is an abundance of relevant and pertinent information about your code, that you never thought possible. NDepend is about assisting you the developer (whether a single man outfit or team) to ensure that you are producing the highest quality code possible. There is so much information at your fingertips, it is overwhelming.

The Model View Controller, Model View Presenter and Model View View Model design patterns for example, are all about separating the concerns of your code, and increasing the testability of your code base. NDepend is another necessary tool, in that when you perform code reviews, it becomes an indispensable tool. Architects for a project can set rules for the code base, and easily check to see that they have been adhered to from a quality point-of-view, and developers themselves can ensure that they remove any sloppy code they’ve written, and spot anomalies and problem areas before they check in their code.

Here I have just loaded an assembly for an application that I wrote a while ago. I have a pane (one of many) that furnishes me with information about the number of lines of code I have for instance, comments, methods, fields and types.

ProjectLines

In another pane, you have information pertaining to code quality, methods that are too big, methods that are too complex, methods to refactor, methods with too many variables, methods with too many parameters, the list goes on and on. Each point an important code quality concern.

CodeQuality

In the same pane under design, you have suggestions that stateless types in the code could be changed to static types, or that a class without a descendant should be sealed, and again a myriad of other code quality issues that need to be addressed.

Design

Why should it be sealed? The sealed modifier is primarily used to prevent unintended derivation, but it also enables certain run-time optimizations. In particular, because a sealed class is known to never have any derived classes, it is possible to transform virtual function member invocations on sealed class instances into non-virtual invocations.  You have 5 classes here that can be changed to Structs, improving performance as they are now value types and not reference types from a .NET runtime standpoint.

You have Visual studio integration, Reflector integration and a wealth of other features.

VSIntegration

An issue any Software architect or Developer always runs into is performance and trying to ensure that their code is as optimised as possible, ensuring that their end users have the best possible experience when they use an application. I have always used this article every once in a while to ensure that my applications are structured correctly, and refer to it every now and again (most of the article is relevant to Silverlight and WPF as well). Clearly by using NDepend you will end up with better performing applications, and also more maintainable codebases, because all the ‘gremlins’ are ‘found out’ soon and the more you review your code with it, the better quality your code will be as a result.

NDepend is an essential new development tool, that I will blog about more, once I understand it better.

Unable to find manifest signing certificate in the certificate store


I have recently upgraded operating systems from Windows Vista to Windows 7 beta 1. When attempting to run a program that complied on Vista, I get the error message; Unable to find manifest signing certificate in the certificate store. [name of the project]

A brief Google search shows this is a common error, but no-one seems to have the error I do, as the suggestion is to edit the .cs.project file and remove the manifest signing section which I presently do not have.

The cause of this error is click once. Whenever you use click once, it creates a temporary strong key name like this one

tempkey1

 

 

 

 

 

 

 

 

 

 

It is this key that is missing, hence the compilation error. Luckily to create another temporary key is easy. Double click into the properties node in  the Visual Studio Solution Explorer, select the "signing" tab and click on Create Test Certificate…certificate1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Don’t bother entering a password, click on OK, and you should now find a new temporary key has been created and added, so your project will now compile.

Free Dundas ASP.NET and Windows Forms Charting Components


Microsoft purchased Dundas last year. They have now provided the charting components free (hope gauges are in the pipeline), all re-named and re-branded, but I have used their former name, because they are known industry wide as components of an exceptionally high standard. These charting components are used in all manner of business applications, including accuracy and mission critical applications monitoring activity in nuclear power stations.

MSChart

You can download the components from here. Make sure you read all the links, especially on deployment; If you build an application using the controls, your setup and deployment should add MSChart.exe installer as a pre-requisite.

It must be said that the install experience is fragmented, and  one has to add the charting component manually in the Visual Studio toolbox by browsing to the System.Windows.Forms.DataVisualisation dll, even after installing the toolbox creator.

I would also highly recommend ensuring you download and run the Visual Studio samples, because it is a very rich resource for samples, features and demonstrations, and the best way to acclimatise oneself with the new library.

Yahoo ditch WPF messenger client


In this announcement, Yahoo have nailed the lid in the Windows Presentation Foundation coffin firmly shut, and returned to native Win32 development for their Messenger Client.

I am rather disappointed about this, because I can see the huge potential of WPF, like my favourite Zurich Airport WPF demo from a couple of years back. Any business will always review the popularity of certain products, and I’m sure the download figures for the WPF messenger must be abysmal. It will inevitably take some time before the reasons why Yahoo took this decision start to percolate, but lack of popularity must be a key aspect.

What is great about programs like Skype and Windows Live Messenger (formally MSN Messenger), is that they are so simple to use, and very responsive. The average end user of said applications is always in a hurry to either send a message, or respond to one, so-much-so that really fancy graphics end up being a hindrance, especially if they are used to something quick, which WPF has not always been. For me it is a case of “just give me a text box and the ability to send my message”. It is as simple as that.

As a developer, you need to be really judicious about when to elect WPF for an application, because what you think is pretty, may actually get in the way of usability, which is the key mistake Yahoo have made. They got to too fancy with the WPF UX and forgot about simplicity.

If I was tasked with building a Messenger client (assuming the design time experience had been improved to that of windows forms at least), I would build an exact copy of the previous UI, and gradually add the features that make WPF stand out when compared with Win32. This would result in an application that appears to an end as a succession of the previous, and not having to learn something new again, to achieve the same result i.e. send and receive messages.

Microsoft have also been erroneous, principally by not eating their own dogfood. It is so important for companies to “practice what they preach”, but they too have stuck with Win32 for Windows Live Messenger, is it a surprise that everyone else has as well?

Report Builder 2.0 Released


Microsoft SQL Server 2008 Reporting Services Report Builder 2.0 (bit of a mouthful isn’t it?) has now been released and is available for download here.

Overview

Microsoft SQL Server 2008 Reporting Services Report Builder 2.0 delivers an intuitive, Office-like report authoring environment enabling business and power users to leverage their experience with Microsoft Office 2007 products. Microsoft SQL Server 2008 Reporting Services Report Builder 2.0 supports the full capabilities of SQL Server 2008 Reporting Services including:

  • Flexible report layout capabilities of SQL Server 2008 Report Definition Language
  • Data Visualizations including charts and gauges
  • Richly formatted textboxes
  • Export to Microsoft Office Word format

Features specific to Report Builder 2.0 are focused on simplifying the process of creating and editing reports and queries and include the following:

  • Easy to use wizards for creating table, matrix and chart data regions
  • Support for directly opening and editing reports stored on the report server
  • Support for using server resources such as shared data sources
  • Query designers for multiple data sources including a Microsoft SQL Server-specific query designer

ReportBuilder

Note; The title says SQL 2008 and is misleading, as you can use this to target SQL 2000, 2005 and a whole host of other databases like Oracle and Microsoft Access.

The Death of Windows Forms – Part 2


In a previous post, I was deliberating about whether to elect windows forms or WPF for an application I was writing. In the end I chose to use the products from Component Factory. The proprietor of said company (Philip Wright) has recently elected to construct a disquisition into WPF adoption. I have chosen to go a little further into reasons Philip states as problematical.

WPF Tooling

I agree whole-heartedly with Phil, it is immediately perspicacious upon opening up the Visual Studio 2008 toolbox, that the controls available are like “Old Mother Hubbard going to the cupboard and finding that it was bare”. Tim Sneath, a Group Program Manager for WPF has a comprehensive listing into the latest improvements in the WPF space, and you can see that controls are still the last thing being considered, as performance and graphics have been the priority.

The best example of WPF tooling is WPF/E, better known as Silverlight. The first release candidate is now available but why is this significant? The significance lies in the fact that Silverlight is a cut down version of WPF, and it is only now that it is beginning to reach completion. If a cut down 4MB version of something that is currently about 80MB (WPF assemblies) is only just nearing completion, you can safely assume that it will be some time before “completion” is reached for the far larger WPF libraries. That is the prime indicator of the work still left with WPF.

I must however disagree a bit with Phil on a minute point. Resource Dictionaries make it a breeze to skin or template all the controls in your application, so as more and more controls become available, then it is very easy to affect your complete applications look with literally a “switch”. The issue with WPF, is new types of controls. At present most vendors are recreating controls already available in windows forms, and augmenting them with WPF. Until the WPF toolbox reaches parity with Windows forms and new types of controls start to be created with WPF that are unachievable in windows forms, then WPF will be seen as playing catch-up.

WPF Applications

This is such an important point. I have lengthy but related posts here and here. Microsoft have always been in the Operating System and Server business, and relied on third parties to produce the eye-candy. The lack of applications is directly linked to why Vista (A WFP bedrock) has been received so poorly. As I’ve said in the links above, the same 100% of applications I used on XP, are the same 100% I use on Vista.

Probably the most widely talked about WPF application is the Lawson Mango application. It is important that my following remarks are not misconstrued so I must clarify that I would love to have worked on such an application.

The prime evocation to take from this application is that it is beyond beauty, and present applications do not compare one iota. This is a huge application, with the form count being around 10 000. It is very clear then that this was

  1. A money no object commission
  2. Technology and not business need driven (or mostly technology and a bit of business need)

For the average business application, resolving business need is the prime requirement. I am developing a Scheduling and BI application for a company, and the quite frankly, they couldn’t care less whether it was Windows Forms or WPF. Their prime requirements are

  1. Can you bring this project in under budget?
  2. Does it solve the business requirement?

One of the key factors that determines a software projects failure or success, is going over budget, and or not solving the business requirement. WPF is positioned disadvantageously here, because it is really very expensive at present to develop applications with the technology. If you get an accountant to do your requirements gathering and costing for a new or existing application, you will be shocked at how much more it costs to develop a WPF application, for no return on investment.

Another key factor in launching a business application is how easy it is to learn. An application that looks like Office 2007, will be far easier to train staff to use because the user interface is familiar hence intuitive. When you take the Lawson Mango application, and look at a company with 500 people and consider the ongoing training costs, then your budget for training goes into the many thousands, where with an Office user interface, that will be significantly reduced. Some might say this is boring, and you will always be stuck in the past, but to run a business you always have to look at the skills your current workforce have, and the return on investment that a specific technology will give you. It is for these reasons, that your typical business will prefer the Office 2007 UI in windows forms, to the Lawson Mango.

Supply Side Push

WPF will only gain popularity and mainstream recognition when Microsoft themselves release a WPF product. Obviously Windows 7 should contain more WPF applications and that will be a key factor in WPF adoption. What they need is a new Media Player done in WPF, or an application that is ubiquitously used in the operating system written “top down” in WPF. They also need to create managed wrappers available for both Windows Forms and WPF for the OS, unlike Vista at present.

Windows forms has already proven that it can create a good 60% to 80% of what is achievable in WPF, but as anyone that has ever developed a project of a decent size will tell you, writing software is very expensive, and unless WPF can start to negate the expense associated with it, for reasons aforementioned, it will continue to have a tardigrade uptake.

Not downloading .pdb files when debugging .NET


A quick tip is to cache your .pdb files onto your local machine when debugging .NET. This means that each time you debug the source then you don’t suffer the performance hit of downloading all the files again.

In Visual Studio go to Tools->Options and navigate to Symbols.

SymboleTools

Make sure you deselect the option to search the above locations and cache the files. In the example I have created a folder on my c:\ drive called RSCC where the files are saved.

Symbols

Switch off/on Visual Studio 2008 Background Compilation


A new feature in the Visual Studio 2008 service pack 1, is background compilation of code to check for errors as it’s written. This facility has existed in Visual Basic for some time now, though I doubt it was ever this fast. What you get in the IDE is a red squiggle (similar to a spell check error) notifying the programmer a mistake is made. In this example I am mistakenly trying to assign a string value to an integer.

ErrorMessage

This type of error checking is all-over-the-place and saves one significant amounts of time, principally because errors are corrected sooner, meaning one doesn’t compile a project and then find loads of errors. I have a huge project, that I feel is being slowed down by this, and need to check whether the background compilation is affecting "snappiness" of the IDE.

To do this (In Visual Studio) go to Tools and then Options.

In the Options dialog select the Text Editor node, then C# and go the the Advanced node. Remove the check in "Show live semantic errors" to disable background compilation, or conversely check it to enable the feature.

SemanticErrors

I would be interested to know if other users are experiencing a sluggish IDE – especially in the larger projects. It therefore "appears" that for larger projects, the time you save in fixing silly errors, is lost in IDE speed – disproportionate of course – but further testing is required.