Archive for the ‘.NET’ Category

h1

Coding Styles and Standards

July 5, 2009

At my current engagement, recently, we have decided to start making our coding standards more uniform and so some of the guys got together and decided that from now on, we shall no longer prefix private member fields with an underscore “_” and will just use lower case.

So for example the following property:

private string _foo = null;

public string Foo

{

get

{

Return _foo;

}

}

would be written like this instead:

private string foo = null;

public string Foo

{

get

{

return foo;

}

}

I thought it was a much nicer coding style in terms of pure cleanliness of the code. It seemed more pleasurable to look at. Today on a separate project I am working on, I spent 1.5 hours debugging the following mistake because of this practice. Let’s see how quickly you can find the error

Now, I’m sure you found the error quite quickly especially because I have specifically given you the context of the problem but as you can Imagine, this is not terribly hard mistake to make. I know I have learnt my lesson on the importance of clear code > pretty code. I would be interested in hearing what others have to say about this.

h1

CopyPaste+

August 31, 2008

I have always been bitten with the limitation of the windows clipboard. I have a bad habit (completely my fault as a user) to copy (usually important) things into my clipboard and then close down the screen with the intention to paste the clipboard contents into another application which is usually a browser, word, notepad or maybe even excel. Sometimes tho, I get distracted before I paste in the content and accidently press control c again over some other content, thereby losing what was in my clipboard.. Arghh! Normally, I would have to relaunch the application and fetch the original contents of what I wanted copied into my clipboard. Well this week end I finally got around to creating CopyPaste+ which is pronounced CopyPastePlus, sort of like C#. Yes, so I’m not the most creative guy around, but hey – I don’t care! Anyway, here is what I would LIKE my application to look like:

CopyPaste+

CopyPaste+

This is just a quick mock-up that I made… Okay maybe not me but, my so much more creative friend William did.

And here is what the app currently looks like:

puke

puke

Okay, so it looks like puke… BUT, I promise, it’s super useful!

Download the application here

Note: Requires .net framework 3.5 to run which can be found here

How to Use

The application monitors the windows clipboard and adds it to a list of 5 clipboard contents (The default is 5 but this is configurable). At this point you may want to retrieve clipboard content #3 so you would press control + shift + v instead of the normal control v to paste. This activates the applications sexy UI which appears in front of you, fixed and glued to the centre of the screen. From here, you should click on the box you would like to paste. Once clicking on the rectangle, the application hides itself and has overwritten your current clipboard with the selected item. You then simply paste in (tip: control v ;p) your content into whatever application you want. Simply repeat this step to retrieve any clipboard content you would like.

Configuring Number of Clipboard Content Items:

To increase or decrease the number of items you would like to have, all you need to do is have this config file sitting side by side to the exe and change the “NumberOfBoxes” value from 5 to whatever you like

Future Releases

  • Configurable number of Items will be done via User Interfaces options
  • UI will look like Williams
  • Selecting Icon in task bar will activate the UI
  • Will be able to handle content other than just text i.e. images, files etc.

License Agreement

On Request, I will supply the source code to anyone that wants it for non commercial use, for free.

Contact me here to request the source code.

h1

VB Extension Methods > C# Extension Methods =(

July 31, 2008

As my title childishly explains VB extension methods seem to have one over c# extension methods. Yesterday I found the following limitation in c#: You cannot modify the object you are calling the extension method on by ref. you must create a copy of the object and pass it as a return value. An example will make this clearer. You CAN do this:

public static IEnumerable<T> AddRange<T>(this IEnumerable<T> list, IEnumerable range)
{
List<T> totalList = new List<T>();

foreach (T t in list)
{
totalList.Add(t);
}

foreach (T t in range)
{
totalList.Add(t);
}

return totalList;
}

You can NOT do this

public static void AddRange<T>(this IEnumerable<T> list, IEnumerable range)
{
List<T> totalList = new List<T>();

foreach (T t in list)
{
totalList.Add(t);
}

foreach (T t in range)
{
totalList.Add(t);
}

list = totalList;
}

It’s a sad day today my c# friends ;p

h1

WPF Findings

July 28, 2008

Here at work I am currently on my first commercial WPF project. Some of the things that I have learnt about wpf (the hard way)

* There is no Paging or Sorting support in the gridview (unbelievable isn’t it?)

* There are no validators!

* The WPF Binding Engine uses reflection when binding against Dependency Properties and as such your properties need to be public. If not, you will get a silent exception in the output screen

* When binding to a dropdown list ComboBox you cannot bind against a nullable int. Why? I have no idea, but you will find that the selected index changed event will not fire

* There is no Gif Animation Support in the Image control. (expected to be supported in next release of wpf)

I have quite a few more points but they were the top few that I could think of that were easy to list out.

Overall I am loving what WPF has to offer and it is quite a different thinking approach as to web development. Although I must admit I feel a bit home sick (asp.net)

h1

Unit Testing Internal methods.

July 10, 2008

Today I did some pair programming with Richard Banks from Readify.

When I explained to him that I use the IDE to generate these property accessors that use reflection to test internal and private methods he showed me a nicer way around it:

  http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx

Basically all I had to do was [assembly: InternalsVisibleTo(“Assembly Name”)] to the assembly.cs file and make sure that the “Assembly Name” was pointing to the unit tests assembly name.

This worked perfectly! Thanks Richard!

P.S I should state the obvious note that this approach doesn’t work for private methods

h1

Results of SourceMonitor on my current project

June 29, 2008

Well last week Richard from Readify had been called in to help us here at my current engagement – Carlson Marketing. While he was here he pointed me to a few programs that I had not known about. One of them was called SourceMonitor. What it does is basically run some metrics against your code to see how clean and how well/poorly written your code is. Before I start keep in mind that the max code complexity and average code complexity should be between 5 and 15. We ran it against some of the code that was contracted off to an offshore company in india and the results that came back were extremely poor (max code complexity of 80). Rather than bagging out what a poor job they had done I decided to see if my own code that I had written myself was any better. I consider myself to be fairly padantic about the way code is written and try to keep it simple as best I can. I decided to run it against one of my more complex projects here to try and get my worst case scenarios. Here are the results SourceMonitor-profile-code-complexity-metrics

 

as I suspected one of my methods is too long and is too complex. The method basically maps a business object from a web service that’s hosted somewhere across the world. There are many properties on the class that need to be mapped each with fairly complex business logic(s). From this we can see that I have not written enough comments and have a single method that is slightly too complex. On average tho we can see that the rest of the code is fine. It is one problematic method that is throwing everything off a little. I have not gone back to refactor this code as it has been code reviewed by fellow senior devs and we agree that it is an exceptional case and that it is written well. The idea isn’t to stick between the green area 100% of the time but rather to get a rough idea on how well your code has been written. If you genuinely write clean, simple code, I think you will be surprised at how often your code will hti the sweet spot of that green circle.

Here are the results of the code that the offshore company had written: offshore-company-code-complexity-metrics

As you can see the max code complexity is 80 and the average code complexity and comments is off as well.

 

Summary:

SourceMonitor is a great way to make sure your code is clean, simple and most importantly – understandable. It is NOT something to be followed as bible but rather a guide.

h1

TFS Build running out of space.

June 23, 2008

This morning all our builds started failing with the following in the error logs

the reason was because the drop location directory had accumilated too many builds and had run out of space. Rather than deleting the builds through explorer in the drop location you should remove them through TFS so that TFS knows about it.

Here is the command to do so: TfsBuild delete <TeamFoundationServer> <TeamProject> <BuildNumbers> [/noprompt]