Monthly Archives: December 2010

Automating File Header Information with ReSharper

Many OSS projects often have some license information at the top of each file, reminding you of the evils you need to comply with. Some companies also have policies in place that require every single file to have company information. It would be quite cumbersome if one would have to constantly and manually enter this information. Fortunately though, you don’t need to.  Let’s examine the various ways to do this.

Inserting File Headers during Code Cleanup

ReSharper offers the possibility of running code cleanup on your file, project or solution. As it’s name indicates, Code Cleanup can clean up certain aspects of your files, like optimizing using statements, formatting the layout of type members, removing code redundancies, changing explicit type declarations to use var, and much more.

One of these options is to insert File Headers. For this, it uses the File Header values defined in ReSharper | Options | Languages | File Header. Therefore, the first step to take advantage of this feature is to define the header:

SNAGHTML1028ee97

We can optionally add a region name in the Embrace File Header into Region with Name. By doing so, ReSharper will wrap the header inside a region (currently the File Header is global across solutions).

Once we have the header defined, all we need to do is activate the option in the Code Cleanup. We can either activate it in an existing profile or create a new profile specific for Header insertion. To do so, we invoke the Code Clean up (Cltr+E,C in VS Scheme) and select Edit Profiles

SNAGHTML102e43a9

We then create a new Profile by clicking on Add, and giving it a name, for instance File Header Insertion.

SNAGHTML102f5fef

We then need to select all the cleanup operations we want to happen when we run the profile. In our case, we’re exclusively interested in the Update File Header.

Our newly created profile should now appear as an option when invoking code cleanup. To apply the cleanup to a file, we can invoke it when editing the file or having it selected in the Solution Explorer. To apply to the project, we select the Project. And as expected, selecting the entire Solution will apply the cleanup solution-wide. In our case, we want to apply the licensing to the entire solution

image

That’s it. With one action we’ve applied licensing to our entire solution.

Automating File Headers with File Templates

The previous solution works great when we want to apply licensing on a global scale. The downside is that it’s the same across different solutions. What happens if we want to customize it based on project? One other issue is that it does not  support any type of macros. As such, we cannot insert values such as the filename or date the file was created.

An alternative to this approach could be create file headers when we create the actual files, for which we can use File Templates. (we’ve covered File Templates in details in an earlier post). Let’s see how we can apply this to class creation.

We create a new template under Shared Solution Templates in File Templates

image

in this case called Licensed Class. The definition of the template is:

SNAGHTML1128e92f

which is the default class template, to which we have added a region with the license information and two additional macros: Filename and Date Created. There are corresponding Macros in ReSharper to obtain this information so no need to extend the functionality. We do set the Editable Occurrence of each Macro to false so that ReSharper does not prompt us for values.

The final step is to make this template accessible when creating classes (using Cltr+Alt+Ins). To do that, we need to drag it and drop it on the Quicklist as shown

image

Now when creating a new file, we get Licensed Class as the first entry

image

When we switch to a different solution, the Licensed Class will not be available, so it will not interfere with other solutions were this type of header is not required. Obviously, we could generalize this by modifying the built-in template as well as apply it to other templates (template embedding might be a good feature here!)

Like everything, this also has it’s downsides. First of all, you cannot *apply* this to existing classes since it’s on creation. Secondly, if you run code cleanup with a new File Header, it will overwrite the existing one. The one advantage it does have is that you can place that template anywhere you want. So for instance if you prefer your license information to be at the bottom of the file, you can do that. 

Using StyleCop for ReSharper to provide Company and Copyright Information

If you’re not familiar with StyleCop, it’s a tool for Microsoft that highlights certain coding styles. Howard van Rooijen wrote a plug-in for ReSharper that makes using StyleCop much easier and more interactive. Check it out if you’re not familiar with it.

One of the *rules* that StyleCop recommends is for files to have headers with information about author/company, etc. When you install StyleCop for ReSharper, it creates some options under ReSharper | Options as well as entries under the Code Cleanup options.

SNAGHTML113a8050

In order to define the information required, we can right-click on the Project Properties and select StyleCop Settings

image

and enter the information under Company Information

SNAGHTML113c9a07

By just enable StyleCop for ReSharper, we already get quickfixes on our files to provide header information

image

which generates

image

and we can apply it globally using the Code Cleanup Options that StyleCop adds

image

It is important to note that the license information would need to be provided independently using the File Header option of ReSharper. What StyleCop for ReSharper provides is the ability to combine this information with it’s own.

Summary

Based on the type of information we want included, as well as the scope, we can pick one of the different options presented here or combine them when appropriate. Right now the File Header option in ReSharper is slightly limited in the functionality it offers but already suggestions have been made to improve it. If there’s something you’d like, please either log it in YouTrack or leave a comment here.

SRP, as easy as 123…

…of course, you’d need to have the song ABC from the Jackson Five in your head for that title to be remotely amusing.

Single Responsibility Principle is such a simple principle. It states that a class should only have one responsibility. One responsibility. Not two, not three, one. Such a concise and simple definition is hard to get wrong, right? Right? RIGHT??? Hmm…

clip_image001

A conversation between two friends

I’m looking at this Customer class, it does a bit too much no?

Huh? What do you mean? It’s a Customer Class. It deals with Customers. That’s all it does. Not Employees. Customers. I can fetch a customer. I can fetch a lot of customers. I can store a customer. I can calculate their age. I can check if they are VIP customers. Everything there is to do with customers. That’s all it does. Isn’t that what this Single Responsibility Principle states? To only have one responsibility?

Well, yes, but I think you’ve kind of missed the point on what this whole responsibility thing is .

How so? It just deals with Customers right? In fact, isn’t what I’ve just described pretty much the ActiveRecord Pattern?

Indeed it is.

So then WTF is the problem? I don’t get it.

Digging deeper…

Hmm, let’s look at this class. It reads/writes a customer(s) from/to the database. Therefore it has some code that has to do with reading from a database. It calculates age and verifies if a customer is a VIP. So it must have some business logic code in there.

What happens when you need to change the persistence logic?

Well I’ll change the code.

Which code?

The Customer class of course.

Right. What happens when you need to change the business logic of how a customer is considered a VIP?

I’ll change it again…..Sorry. What’s your point?

So again, you’ll touch that class. What happens when Customers need some validation? You’ll again have to touch the same class.

And?

What happens when you touch code? You usually end up breaking things. And it’s not necessarily that particular thing you touch. You break other things that at that point you had no clue were somehow related. The more complicated the code, the more chances you have of breaking something. Of course, if on top of everything,  you don’t even have regression tests…well I’m sure you’ve suffered the consequences.

I kind of think of myself a competent developer. I’m sure I can work with enough care to not break things.

Right…. Now while you go and convince yourself of that, let me explain another problem. You see, writing code is easy. Understanding it is hard. The more code in a class, the harder it will be to decipher. The less code, the easier it will be to comprehend. If a class does many things, it will most likely have what? More code. The less it does, you got it! Less code. Forget logic for a moment. It even has psychological impacts. Open a large class and a small one. Which one will depress you more?

I have no problem understanding my own code. 

Sure. You’re very smart and you’ve worked on this code base and you know it well. What about other team members? Hell. Forget others. Why don’t you look at code you wrote yourself eight months ago. Do you know what it does?

Well mostly yes. But I guess that’s kind of why comments are useful

Why do you need comments if your code is clear enough to understand? And you know why your code is hard to understand? Because you’re trying to solve too many problems at the same time. Think about it. You’re doing Customer listings, storing, reading and some business logic. It might be the case that for this particular example it’s not too hard to understand, but that’s because both of us have beaten Customer management to death (along with Authentication) so we’re experts in it. But imagine being thrown into some code that you have little knowledge of the domain. Is it easier to understand a class if it does one thing or five? 

So what are you proposing?

It’s actually very simple. Divide and Conquer. Remember that? Back from our College days? Divide up a problem into smaller problems and solve the smaller issues. Dealing with mini-problems is easier than dealing with mega ones.

When we take a class and divide it up into smaller classes, it will be easier to deal with it. And dealing can mean both understanding it and modifying it. But if your customer class is doing all that, it’s bound to be harder to understand and maintain. That’s why it’s important to get these responsibilities right.

I see. But where have I gone wrong then? I kind of thought I understood the Single Responsibility Principle. Going back to my Customer class, how do I define what the responsibility is if it’s not “dealing with customers”?

Getting a grip on responsibilities

The main problem in complying with SRP is defining the responsibilities. Where do we draw the line? A few things that have helped me along the way have been…

Change

Have you ever heard people say:

“A class should only have one reason to change”

when talking about Single Responsibility?

Yes, actually I have. 

Well think about it. If a class changes for more than one reason, it must be because it’s doing more than one thing. Going back to your customer class, think how many reasons there is for it to change. If we have to touch the same class for changing different things, the risk of breaking something is higher, which leads to higher costs in maintainability.

One way therefore of trying to figure out the responsibilities of a class is by asking the question of how many reasons are there for it to change.

Naming

Naming is a good way to discover the responsibilities of a class. What does your class do? Give it a name describing what it does. Does it use And or Or? Is it hard to explain what it does? Do you need to use suffixes like Manager, Processor or Admin because you can’t pinpoint the exact word describing what it does? Maybe it’s because it does two things that one word can’t describe. These are all clues that your class is doing more than one thing.

Cohesion

Take your class. Look at your methods. Do they have parameters or are they using instance fields? If they are using parameters, remove them. Make them instance fields. Do you end up with methods that only use one of the five instances? That most likely is a warning of the low cohesion that exists between that method and your class. Cohesion is a indication of how well related lines of code are, how well related methods are to a class. 

And you use ReSharper right. Next time ReSharper prompts you if you want to make a method static, it’s telling you it does not use instance fields. That’s where you need to decide if that method actually belongs in that class. Sometimes it might, many times it might not.

 

OK, that’s kind of making sense.

Wait though. Many look at Single Responsibility Principle as something that applies only to classes. Wrong. It applies to methods too. The more things your method does, the more lines of code. The more lines of code. The harder to understand. The harder to debug.

Use the same process to identify responsibilities in methods. How many reasons do I have to change the method? What do I name it? If I can’t name it on a single line, bad! If I can’t name it without using And/Or , bad!

Refactor your methods to smaller ones. Give each smaller method a good name. Make it descriptive. Don’t waste time trying to comment your methods and the 20 parameters it takes. If it’s well named and has one or two parameters at most, it should be self-describing. Remember, parameters are used to operate on values, not decide what path to take in the method. For that, you create two methods.

(Beer enters the scene…)

Hahahhaha…

What?

I remember back when I first started using non-locking source control, I was concerned about running into conflicts when merging. I was right. I did run into issues. I bitched at the SCM. But now that I think of it, the problem wasn’t the SCM. It was the way I was working. It was having too much code in the same class.

Yep, and that would lead to contention since many people would need to touch the same code.

Yep. I was fighting the wrong problem. Damn, it seems such a simple principle, but hard to get right then….

Yes. It’s about asking the right questions. Single Responsibility Principle is about making code maintainable and understandable. That’s all there is to it really.

Right. Well dude, I guess it’s time for me to Refactor out that Class.

Yep. Oh and do yourself a favor. If you’ve not read the book Clean Code by Robert C. Martin (@unclebobmartin), get it. It teaches you much of what we’ve been talking about.

Cool!

Generating Graphs for YouTrack with HTML 5 and jQuery

A few days ago, a customer of ours asked me if it would be possible to visualize statistics in graphs in our Bug Tracker YouTrack. Answer is Yes. Since YouTrack offers a “ReSTful” API, you can obtain pretty much any kind of information you want from it and once you have this information, you can then display it however you wish, be it a table or a graph.

Let’s see how.

What we’ll be using…

One of the great benefits of jQuery is the rich ecosystem it offers. There’s a plug-in for pretty much everything you need, even for displaying graphs. In fact there are quite a few. We’ll be using jQuery Visualize. We’ll also be using jQuery (I’m using 1.4.4 but should work with older version) and jQueryUI, which is needed by jQuery Visualize. Finally we’ll also need jQuery Templates (I’ll explain why later on).

Don’t worry about downloading each individual file. Everything you’ll need is included in the demo code attached to this post.

Getting the information from YouTrack

For this demo, what we want to do is create a chart containing Issue counts for different projects. In particular, we are interested in Open, Fixed and Duplicate issues for ReSharper, dotCover and dotTrace:

image

In order to get this information from YouTrack we have to make a request to count the number of issues based on filters, i.e  ReSharper + Open, ReSharper + Fixed, etc.*. The URL for this would be:

http://youtrack.host.com/rest/issue/count?filter=project:ReSharper%20state:Fixed

which is nothing other than a URL encoded version of a search expression we can enter directly into YouTrack’s web interface,but to the rest API. We therefore need to send requests from our web page to YouTrack to obtain this information. The way we can do this is with Ajax and JSONP which YouTrack supports. Using jQuery.ajax, our code would look like this:

image

Problem is, this wouldn’t work, or at least, not always. When requesting a count of issues by YouTrack, the response is not immediate. YouTrack will free the request and will spin off to calculate the count. Once it’s complete it will then store the information in the cache. Next time a request is made, the value from the cache will be returned. What this means to us is that if we fire off a request, if it’s a cache miss, then we will get back a –1 instead of the actual count. We would therefore need to send a new request, until we get a value greater or equal to 0. This is not complicated to do with JavaScript and in particular with jQuery. It merely consists of polling for a result. But like all things jQuery, before you re-invent the wheel, check to see if there’s already a wheel and it turns to your liking, something which in this case does. This alternative to $.ajax allows us to poll servers and only process the result if a condition is met. As such, our previous code would now look like this:

 image

(the polling interval can be set for the calls. Here we’re using the default value).

In terms of requesting information from YouTrack, that’s all there is to it. What we need to do now is repeat the process for each project and status type and store the value.

[* Currently YouTrack does not support grouping so multiple requests will have to be made. This feature is planned however]

Representing the information as a table using jQuery Templates

We want to do two things with the results, represent them as an HTML table and display them as a graph. The data retrieved from the server is given to us as an array of objects (something we did in the code ourselves), where each object has a one property indicating the Project and three additional properties with the values of each status:

image 

We would now need to iterate through this array and generate a table from it. Instead of doing this in an ugly way by mixing tags with data, we can take advantage of jQuery templates (if you are not familiar with templates, you can think of them as a view engine for jQuery). What we do is define a template and then make a call to a function that replaces placeholders with our data. If the data we provide this function is an array, it will repeat it for each entry.

image

The code first creates the template for the markup we want to generate, which are rows of a table. It then calls the template function to create a compiled version of the template which will then be passed in to the tmpl function along with the data (the series variable). The call to appendTo is a jQuery method which adds the contents of the object it’s called on to the item passed in to the selector (in this case #rows). 

In the body section of our page, we can then define the rest of the table, which includes the header and the tag for the table body:

image

Creating the Graph

So far we have retrieved the information from YouTrack and created an HTML table to display it. Our main objective however was to also display this information graphically. That in fact is the easiest part. One reason we are using jQuery Visualize is because it can easily convert a table into a graph (it supports different types of graphs, including lines, pies, etc.). All we need to do is call visualize on a table.

image

That’s all there is to it. Once we put all the pieces together, we will end up with the following page:

image

This chart works in all browsers I’ve tested (IE 9, Opera, Firefox and Chrome)

Summary

The complete code for requesting information from YouTrack and generating the Graphs is available on my GitHub Blog repository. Once you examine it, you’ll see that it’s very few lines of code, thanks to the rich ecosystem around jQuery. I’m sure it can even be improved further, since I am by far a jQuery expert and can also be made a bit more generic to allow for different statistics to be obtained without change to the source.

I’m currently working on YouTrackSharp which is a wrapper for YouTrack requests in C#. So if you’re interested in doing the same thing from C#, you will soon be able to do.

As always, if you have any questions please leave a comment.

Enjoy!

Coverage with TeamCity and dotCover with MSTest, NUnit or MSpec

As some of you know, we recently shipped TeamCity 6 which includes, out of the box, a bundled version of dotCover. What this means is that you can now get coverage reports for your code easily, and of course, for free if you’re using the Professional version of TeamCity.  The setup is quite easy if you are using MSTest and NUnit. For MSpec, you need to take a few additional steps.

Using MSTest / NUnit Runners

Normally build files consist of a series of tasks that involve compilation and running of tests. With TeamCity, you can separate some of these steps out into individual TeamCity Build steps, which is what we will be doing in this case (everything that we see here applies to both MSTest and NUnit).

Here is our build.xml (MSBuild) file:

image

As we can see, other than compiling a solution, which in this case consists of the actual application and the test assemblies, not much else going on.

[Note: this could have been done using the SLN as the Runner Type under TeamCity since this example build script does not do much else. In real scenarios however, build scripts do more than just call a solution (in fact normally you’d call projects not solutions)].

In TeamCity, we create a new build project and setup the VCS root. We then add a new Build Step which calls this MSBuild file:

image

Notice that all we are doing here is calling our MSBuild script. No coverage settings yet.

Next thing is to add an additional build step in TeamCity. This time, we are going to call MSTest as opposed to MSBuild:

SNAGHTML1e386e79

SNAGHTML1e3d642d

(the sections cut out are blank).

In the .NET Coverage tool section we select JetBrains dotCover and then  add the assemblies we want coverage for (just the name of the assembly) prefixing them with +: and filtering out those we do not want coverage for with –:.

That’s all there is to it. Once we run the Build, we should now see a new tab with Coverage Reports as well as a new Artifact which contains the Coverage files zipped up.

image

The Code Coverage tab goes into more detail:

image

We can even drill down into individual classes and examine the code coverage:

image

If we are using NUnit instead of MSTest, the only difference is there test runner we select when adding a new Build Step in TeamCity. Instead of MSTest we choose NUnit along with the version:

image

What about MSpec or my Specific Test Runner?

If we are using MSpec or a different test runner that is not supported directly by TeamCity, we can still get coverage reports; we just need to do a little bit of additional configuration. TeamCity has an API which allows us to send it messages when we want to interact with it (this is actually quite a powerful feature but out of scope for this post so please leave a comment if you’d like me to cover it in more detail). We can leverage this API to tell it when to start coverage and where to get the results from.

Here is the build script for MSpec:

image

We have created two targets. The second one (TeamCity) is the one we are interested in. This does a couple of things:

1. The first <Exec> runs all MSpec tests so that we can see the test results inside TeamCity. This is not strictly necessary for Code Coverage but usually build processes do display these results. The –teamcity option we are passing in to MSpec is for it to generate the system messages that are then fed to TeamCity (see point 3)

2. The second <Exec> is the one that runs dotCover. This uses a configuration file called dotCover.xml which we will examine further down. We pass in the c (or coverage) option when calling dotCover.

3. This is a message we send to TeamCity to tell it that we have run coverage. We indicate the tool we are using (dotcover in this case) and where the results are located. TeamCity uses this information to then display the results in the UI. This is one of the API messages mentioned earlier.

Finally we need to define the dotcover.xml file with out configuration for running dotCover (for detailed information on creating dotCover configuration files, see here and here):

image

In terms of TeamCity, we then just define our build step that calls out to the build script:

image

Notice how we do not specify .NET Coverage options explicitly. And if all goes well, we can see the coverage output just as before:

image

Summary

We can see that running code coverage is now pretty straightforward when using MSTest, NUnit or even a custom test runner. Most of what we have covered for MSpec will work with pretty much any test runner in terms of coverage (feel free to vote here for MSpec support).

With any build process, there are numerous ways of doing the same thing. I’m going to show you one of them. Based on your setup and needs you might want to do things differently. Fortunately TeamCity is flexible enough to allow for many scenarios.

One thing to be aware of is that dotCover creates some temporary files for the XmlSerailizer in the Temp profile folder. This normally is not a problem unless the folder does not exist. If you are running TeamCity under the SYSTEM account, make sure that the folder C:\Windows\system32\config\systemprofile\AppData\Local\Temp exists. This will probably change in future versions so to avoid any possible issues.

Enjoy!