Create a Custom Layout in a RadChart for Silverlight

The default layout for the RadChart for Silverlight is a very common one. It has a title at the top, a legend on the right, and an area in the middle reserved for the chart. It looks roughly like this.

DefaultLayout 

If you started by using the default layout for the chart then you probably used these properties in some way.

Chart1.DefaultView.ChartArea
Chart1.DefaultView.ChartLegend
Chart1.DefaultView.ChartTitle

When you switch you’ll be adding in your own custom content inside of the RadChart tag in the XAML. You can only have one piece of content inside of the RadChart tag, so make sure you put in something like a Grid to do the layout. In this case I am removing the Legend and am going to just have the Title and Area in the chart, so I add this XAML for my chart. With this in place I am now able to see a spiffy new chart.

UsingDefaultLayout

One more thing to do here. I’ve got my new layout in there but the chart is still using the old layout. Notice that the space for the chart and the legend still exist. I have to tell the chart that I don’t want it using the default layout anymore. There is an all-too-obvious name for the Boolean property to disable the default layout. Just set UseDefaultLayout to False in the XAML and you’ll be using the correct layout.

CustomLayout

XAML for Custom RadChart Layout

<telerikChart:RadChart x:Name="Chart1" 
        UseDefaultLayout="False">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
 
        <telerikCharting:ChartTitle
            Content="Number of Hamburgers Eaten" 
            HorizontalAlignment="Center"/>
        <telerikCharting:ChartArea 
            x:Name="ChartArea1" Grid.Row="1" 
            NoDataString="" />
    </Grid>
</telerikChart:RadChart>

Time-Tested Testing Tips – Part 7

At last night’s Cleveland .NET Special Interest Group I met Russ Nemhauser who gave a great beginners’ talk on Testing, TDD, and Mocking. One thing I noticed that he did when he was showing how to write unit tests was kind of interesting. I admit I usually take tests one at a time when I get my idea for the test I want to write I just go ahead and start writing it. When he was demoing he started by brainstorming a handful of tests and made comments for each test name.

Normally what I would do is sit for a second and think of the few cases of each type I want to test. So I might sit and think, “OK, which edge-cases exist that I need to watch out for”. Then I would go and write just the ones I deem likely to ever come up. I’m obviously not going to test every case out there.

So I tried combining his technique with mine to see what kind of result I would get, and it worked out pretty well. Even though I’ve not been using it long I’ll post this technique here.

Brainstorm and Prune

Start by coming up with some functionality you want to add. brainstorm a bunch of tests you could write to check a lot of different cases. Make a bunch, because it doesn’t take much time. Write the names of these stormed tests as commented lines as you come up with them.

Make sure when brainstorming that you’re getting some important types of tests: some testing the happy path, some testing the sad path, and some testing edge-cases. This will get you a good mix to choose from. Now obviously if you came up with only a few things to test then write them all! If you came up with a lot of tests then pick which ones are most likely to cause problems if they misbehave and also the ones which are most likely to occur and write them.

Don’t Write Too Many Tests For One Method

I need to be careful how I say this. I want people to write a lot of tests, however, a lot just means you should be testing pretty much everything you can. You need to be careful not to write too many. This is why I say to prune in the previous suggestion. Certainly it takes very little time to add a few extra test cases, but you’ll be unhappy if the method signature changes. Or if the business logic changes. Remember that one of the big reasons we test is to mitigate the cost of change. Testing should make it easier to change the code, but if you write to many it will make things harder.

If you wrote a dozen tests for a method and then you add in a new parameter then congratulations you now have a dozen tests which you need to modify. That is kind of painful. Perhaps if you had only written six tests and left off some of the redundant ones you could have saved yourself a lot of extra work there.

As a general rule I think that if you ever need more than about a handful of tests then your code is probably too complex. It is time to think about the single responsibility principle and figure out how you can break things apart, because perhaps you were correct that a dozen tests were needed. It just happened that those dozen tests should have been testing three different methods with four for each one.

Let Pain Guide Your Learning

When I was learning to test I did a lot of stuff wrong. Testing was painful. I knew that it was beneficial, but I didn’t usually like doing it. Why? Well because I was not doing it correctly. When I first started testing I was using unit and integration tests without really realizing that I was doing too many integration tests. Why? Because I didn’t know how to do things correctly. After a while I learned how to write more unit tests and it was great for a while, but I did a mistake that I have now seen many others make. I shifted my dependency onto something else. After a lot of work I now think I know how to test reasonably well, and I avoid making mistakes by remembering how painful a similar approach has been in the past.

I think that if you haven’t felt the pain of testing badly you’ll never understand how to do it write. When testing is painful you’re probably doing something wrong. This is the sign that you should try something else on your own or ask someone else how to better handle your current situation. Someone can sit here like I am now and tell you how to do something, but until you see why you need to do it that way it is hard to agree. I hope someone uses my testing advice here and when they run into some problem that they remember something in these testing posts and adjusts what they’re doing accordingly.

Windows 7 Keyboard Tips

Anyone who would be reading my blog probably already knows that you can use the keys [alt] + [tab] to switch between programs running on your windows system. This functionality has been around for a while. There are a lot of new and beneficial keyboard shortcuts that you might not know. Some came with Windows Vista and some with Windows 7, but after some time using the new Windows 7 interface I deem a lot of these shortcuts to be essential.

Opening Applications Using the Keyboard in Windows 7

Opening an application takes a good amount of time if you don’t have quick easy access to a lot of applications. I believe keyboard access is essential. Certainly I use quick-launch still, but I use it in combination with a keyboard in Windows 7, because of a slight deficiency in the new taskbar.

Win-Search

Searching for the application is a pretty good one if you haven’t set the app as a quick-launch one. Just press the [Win] key and start typing. Windows will display the results grouped in the start menu and you’ll be able to select the one you want to open. This is quick, but there are faster solutions.

Win-Number

The quick-launch bar is faster than most people realize. This isn’t new to Windows 7, but is relatively unknown as far as I can tell. Try holding down the [Win] key and press the number starting from the left of the app you want to run. For the application closest to the start menu you would hold [Win] and press [1]. In my case 1 opens Internet Explorer 2 opens FireFox and 3 opens Chrome. The order doesn’t really matter, but the point is that I know the numbers for the apps I use most often and I don’t change those numbers so they’re easy to use.

Win-Shift-Number

Just like using Win-Number you can also hold [Win] and [Shift] and press the corresponding quick-launch number to open another instance of an application that is already running.

 

Quick-Launch

Someone is probably wondering why quick-launch is in this post talking about using the keyboard. Well it is here because quick-launch now requires the keyboard if you want to quickly and easily open a second instance of the same application. Since the task-bar and quick-launch are combined now if we click on an application that is already open all it does is bring that app to the front of the screen. To open another instance just hold the [Shift] key when you click on the icon and you’ll get a new instance.

Ctrl-T

This is another quick-launch related shortcut. By holding [Ctrl] and clicking [T] you will be selecting quick-launch items. When you get the app you want to open just click [Enter]. If the application is already open then you can use the shortcut I mentioned before this one in combination. Once you have the app selected just hold [Shift] and click [Enter] while selecting the desired application.

Switching Windows Using the Keyboard in Windows 7

We “multitask” when we use computers or at least we think we do. Since we don’t really multitask we get stuck context switching all the time. This can really drain our ability to get things done, so we should make sure the switch happens quickly. There are plenty of shortcuts in place to make this easier.

Alt-Tab

By holding down the [Alt] key and pressing the [Tab] key we are able to browse through the set of running applications. These applications will be displayed with thumbnails of what the window looks like so we can decide if it is the one we want.

Win-Tab

Similar to the previous one, holding the [Win] key and pressing the [Tab] key will allow you to cycle through the running windows also. This garners a different view. Instead of having thumbnails in a rectangle in the center of the screen we instead get a sort of diagonal cascading line of windows that have been shrunken a little bit smaller so they can line up. This view in my opinion is kind of neat, but I still tend to use Alt-Tab. If you expect to often have difficulty differentiating applications from a thumbnail then this view is probably better for you.

Ctrl-Tab

Those familiar with tabbed applications know this common method of switching between tabs. This is available in a large number of Windows 7 applications. Hold [Ctrl] and press [Tab] to switch between tabs in an application. This functionality exists in most web browsers and a handful of other applications.

Ctrl-T

Windows 7’s new taskbar has added a very new interface to Windows. To select apps in the task bar you can hold [Ctrl] and press the [T] key which will select the applications in the taskbar. Once on the desired application simple press [Enter] to select that app. If only one window exists for that app right now it will open. If there are more then you will then use the left and right arrow keys to select the one you want and then press [Enter] again to bring it to the front.

Win-Number

This is one of the ones I mentioned for opening an application, but if the app is already open using it will select the application. Just hold [Win] and press the corresponding number of the application. Also if you want to switch between windows of that application just press the number multiple times until the correct one is selected.

There is another window switching option I really want in Windows 7 and that is the ability to switch between the same type of application. By this I mean that if I have an Explorer window open I want to be able to switch between the current explorer windows with an easy works-for-everything keyboard shortcut. I understand that the ones I’ve mentioned work, but they are clunky if I know that I want to restrict to just one application type. Win-Number seems like it is the best, but some applications I don’t keep pinned to the taskbar so I don’t know their number.

If you know any more cool shortcuts or know of a way I can switch easily between the same application please post them in the comments.

Time-Tested Testing Tips – Part 6

If you haven’t seen my earlier posts on this topic. I’ve mentioned a bunch of tips which should make your testing easier and more effective. If you’re looking for more tips check out these previous posts.

Time-Tested Testing Tips - Part 1

Time-Tested Testing Tips - Part 2

Time-Tested Testing Tips - Part 3

Time-Tested Testing Tips - Part 4

Time-Tested Testing Tips - Part 5

Don’t Repeat Yourself

I am sure a lot of you have heard this before and know that Don’t Repeat Yourself (DRY) means that you should reuse code so that you’re not repeating the same code all of the time. This also applies to testing. Sure we know to extract the logic in our tests, because we know that test code should be treated very well.

I am not talking about the logic in your tests not being repeated. I am saying that you should not test the same thing twice. Try to only test each thing once. This makes identifying the issue a lot easier. This is extremely important for edge cases. Make sure you’re only testing your edge cases in your edge case test.

You should have a test for each edge case and preferably it is the only test using parameters for the edge case. If something else is using it you’re creating difficulties in maintenance as well as tracking down bugs.

If you break a piece of code dealing with an edge case it will be harder to track down since two tests will be failing as a result. If there were only one it would be easier to find and fix the issue. Also there is the simple fact that things change. What happens if later on the desired logic of the program changes and the handling of the edge case changes. Well now you have to change the code in two places instead of just the one. This makes it more difficult to change the logic of your program. repetition == bad

Start Writing Tests For Everything

When you’re going to try some new piece of code how do you go about it? I am sure a lot of people will create a small sample Console Application and try something and have it print out results to the Console. This works very well, but if you’re really trying to pick up testing you should try writing a test instead. If you want to see how some new functionality works give it a shot in a test. Write a test to see what happens. Assert on the results and try to predict the output. This is a fun little way to work with things and you’ll get more experience using your testing framework.

It is important when learning to write tests that you do it as often as possible. Get used to writing them whenever you write code, so when you’re looking at something new you might as well look at it with a test. What better way to “test it out” than using your unit tests to do it.

Have a great day! Keep testing!

Constructors Should Be Simple and Stupid

There are plenty of commonly known and upheld ideas about how software should be written. There are rules and guidelines professed by many developers which recommend following this design principle or that one. Some are more important than others. Some are easier to find and correct than others.

One such rule that I believe is important to follow is that all constructors remain simple and stupid. This is about as advanced as I believe a constructor should even be.

public SomeObject(int someValue, int someOtherValue, 
    ISomeKindOfInterface someKindOfInstance)
{
    _someValue = someValue;
    _someOtherValue = someOtherValue;
    _someKindOfInstance = someKindOfInstance
}

These are merely assignment statements which use the local variables (parameters). We never ever want to call methods in our constructors. This includes calling constructors inside of our constructor. There are very good reasons for this which I will explain in detail.

 

 

Reasoning for keeping constructors simple

It is difficult to test classes which do work in their constructors. When work is done in the constructor it becomes more difficult to manage the object. This is because the mere creation of an instance causes something to happen. This is difficult to handle in unit-testing because you’re trying to keep control of all aspects of the code while testing. Control is required in order to test effectively, because you need to be able to expect certain behavior to exist based on carefully controlled circumstances.

Otherwise dependencies are hidden. When we instantiate variables inside of a constructor we are “taking a dependency” on the object we are creating. By depending on that code we are limiting ourselves greatly because we are not able to inject anything else. We are not coding against an interface we are working with a concrete class that will be difficult to change or remove.

When we go to test it will be difficult to do so because of these dependencies which have been hidden away in our constructor. Classes should be honest with you. They should tell you up front what else they are dependent on. This honesty is given by having constructors which are open and giving information freely by asking for the dependent objects through constructor parameters.

Otherwise we are adding an extra responsibility. The Single Responsibility Principle is another commonly held principle that states than any object should have one responsibility. This is powerful because it means that any object should have only one reason to change. When we add code to the constructor we have given the object the responsibility for knowing how to construct the object graph for this type of object. If the object is complicated to construct then there should be something else with that responsibility.

What to look for

Keep your eye out for any and all method calls. If any code other than a field or property is being called from within a constructor this is bad.

Instantiation of other objects is a sure sign of a dependency. This goes for anywhere not just in constructors. If you have the “new” keyword it means you’re dependent on the object being created.

Any “logic” in the constructor. If there are conditional statements then you probably have something to fix.

More than a handful of lines is probably too many as well, and is a sign that the constructor is doing more than it should.

 

Example bad code

public SomeObject(int someValue)
{
    _myKindOfTown = new Chicago();
 
    if (someValue > 5)
    {
        _myKindOfTown.HandleMyKindOfRazzmatazz();
    }
}

How to solve the problem

Removing the Object Initialization

One of the easiest ways to improve things is to get rid of the object initialization from the constructor. Passing the values for these objects as parameters is how we get around the issue of initialization. This makes it so that we do not do the work in the constructor.

In some cases we know we will not use that object, for example in our tests, so we can pass in a NullObject.

This process is called Dependency Injection, and it allows us to “inject” our dependencies into our objects.

Create a class to handle the construction

If your object has some complicated requirements involved in the construction that need to happen every time the object is created, you should think about creating an object whose sole responsibility is to create these other objects. As we said earlier a complicated initialization task is another responsibility, so we can have an object who owns that responsibility.

These are often Factory classes who know how to build other objects.

Fixed Code

// Object creation method in another class
public SomeObject CreateSomeObject(int someValue)
{
    ITown chicago = new Chicago();
    if (someValue > 5)
    {
        chicago.HandleMyKindOfRazzmatazz();
    }
    return new SomeObject(chicago);
}
 
 
// ctor for SomeObject
public SomeObject(ITown myKindOfTown)
{
    _myKindOfTown = myKindOfTown
}