Archive

Archive for the ‘Development’ Category

How To: Read String Line by Line

January 19, 2011 Leave a comment

Breaking a string containing multi-line text to a list of lines can be easily done using StringReader. Here is a method that does this and small test program:


class Program
{
    static void Main(string[] args)
    {
        IList<string> lines = BreakLines("This is a test for breaking multiline string \nto a list of lines.");

        foreach (var item in lines)
        {
            Console.WriteLine(item);
        }

        Console.ReadLine();
    }

    private static IList<string> BreakLines(string text)
    {
        IList<string> lines = new List<string>();

        using (StringReader reader = new StringReader(text))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                lines.Add(line);
            }
        }

        return lines;
    }
}

 

Categories: Development, How To Tags: ,

Assertion Classes in MSTest Framework

October 8, 2010 Leave a comment

For the last couple of months I’ve been doing a lot of unit tests using MSTest framework. Although it is good to have special assertion classes to simplify verification of strings and collections names picked my framework developers are not very good as for me.

These classes are named StringAssert and CollectionAssert. Looks good at first glance. But took me some time to figure out they exist.

Lets think about context in which they are used. I’m doing assertions in tests and I definitely know about core assertion class named Assert. Suppose additional classes were named AssertString and AssertCollection. Small change in names would make dramatical change in discoverability. As I start typing for Assert class in IntelliSense I would see them next to core assertion class.

This example demonstrates that developers should think about usage context of classes when naming them.

Categories: Development Tags:

Fixing TortoiseCVS Icon Overlay in Windows 7

July 28, 2010 5 comments

After installing TortoiseCVS on Windows 7 icons of folders and files that are under CVS control didn’t change. Which clearly dispappointed me. Some Web browsing and several tries lead me to the proper solution.

To fix the issue you need to rename subkeys  under “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\ShellIconOverlayIdentifiers” starting with “Groove” to something that will move them to the bottom of subkeys list (i.e. VGroove).

TortoiseCVS Fix On Windows7

Categories: Development, Software Tags:

How To: Perform column-based selection in Visual Studio

July 22, 2010 Leave a comment

Sometimes is is needed to copy some information from source code in a column-based style (parts of several lines starting from some position inside the line). It turned out that there is very easy way to achieve that. Just need to use Shift + Alt during selecting area with the mouse.

Column-based selection in Visual Studio

Categories: Development, How To Tags:

Virtual Functions in Constructors Trap

May 5, 2010 Leave a comment

In .NET calling virtual function in constructor of a class is a bit tricky. While one can expect that implementation from current class will be invoked, runtime actually always calls implementation of the function from the most derived class of all possible. So if virtual method in child class doesn’t call implementation from parent one can get trapped and initialization will be done incorrect.

For example, using BaseClass and DerivedClass provided below

		class BaseClass {
			public string DefaultName {
				get {
					return _defaultName;
				}
			}
			protected string _defaultName;

			public BaseClass() {
				InitializeDefaults();
			}

			public virtual void InitializeDefaults() {
				_defaultName = "BaseClass";
			}
		}

		class DerivedClass : BaseClass {
			public DerivedClass() {
			}

			public override void InitializeDefaults() {
			}
		}

when creating instances of the classes DefaultName will be different:
BaseClass baseClass = new BaseClass();
// baseClass.DefaultName: "BaseClass"

DerivedClass derivedClass = new DerivedClass();
// derivedClass.DefaultName: null

Categories: Development, Sharp C# Tags:

Analog for RowTest attribute from NUnit in MSTest

April 8, 2010 Leave a comment

Simone Chiaretta in his How to simulate RowTest with MS Test post has a good sample of how to use Data-Driven Unit Tests from MS unit test framework to achieve same behavior as with RowTest attribute from other frameworks (like NUnit and mbUnit). I only wish that MS solution would be as simple as original RowTest.

Categories: Development Tags:

How To: Unit Test a Singleton

February 25, 2010 Leave a comment

Unit testing a Singleton is not a trivial task because each test needs untouched Singleton object to keep all the tests isolated. One way to achieve this is to recreate underlying singleton object using reflection during test setup or teardown as described by Jonathan de Halleux and Omer van Kloeten.

But I think that a better way is to extract implementation from singleton into a separated class. This way we’ll have a class that contains all the logic to be tested and another class used to wrap it into a singleton.

public class LoadBalancer {
	internal LoadBalancer() {
	}

	// LoadBalancer class implementation goes here...
}

public sealed class LoadBalancerSingleton {
	private static readonly LoadBalancer _instance = new LoadBalancer();

	private LoadBalancerSingleton() {
	}

	public static LoadBalancer Instance {
		get {
			return _instance;
		}
	}
}

Using InternalsVisibleTo attribute in this assembly we can allow test project to perform needed unit testing. Although technically it is possible to create another instance of the LoadBalancer class inside this assembly comments on constructor and common sense should prevent such a situation.

So to my mind it’s a good alternative to reflection approach.

Categories: Development, How To Tags:

Comparing Strings in Java

May 22, 2009 Leave a comment

My nearest project will be for Android platform and this week I was exploring the platform and Java since last five years I’ve been developing using Microsoft .Net.

Yesterday I needed to check if response status from web service is equal to “ok”. I was influented by .Net experience and did it just like this:

// Wrong way!
if (responseStatus == "ok") {
}

I didn’t expect this will not work. After some digging in Java documentation I found out that by using “==” operator I compared addresses of strings instead of contents.

The right ways to compare strings in Java are:

// Right ways!
if (string1.equals(string2)) {
}
if (string1.equalsIgnoreCase(string2)) {
}

Categories: Development Tags:

Borland C++ 3.1

March 16, 2009 Leave a comment

Today I needed to check some code in C++ and decided to use old good Borland C++ 3.11 just because it’s fast to install it.

Unfortunatly I found out that my copy of it died together with my external drive :(

I must confess that it was not such an easy thing to find working download link on the web. But I found one at http://www.eni4ever.com/downloads.php?cat_id=1. This page also contains links to some old IDEs for other languages.

Looks like this resource doesn’t contain it anymore. So here is a link to Borland C++ 3.1 in my public folder on SkyDrive.

Categories: Development, Software

MVC vs MVP

February 12, 2009 Leave a comment

Recently during my recearch on the different between MVC and MVP patterns I found two great articles.

Hear are two quotes from them.

1. Everything You Wanted To Know About MVC and MVP But Were Afraid To Ask

The two patterns are similar in that they both are concerned with separating concerns and they both contain Models and Views. Many consider the MVP pattern to simply be a variant of the MVC pattern. The key difference is suggested by the problem that the MVP pattern sought to solve with the MVP pattern. Who handles the user input?
With MVC, it’s always the controller’s responsibility to handle mouse and keyboard events. With MVP, GUI components themselves initially handle the user’s input, but delegate to the interpretation of that input to the presenter.

2. MVC or MVP Pattern – Whats the difference?

Here are the key differences between the patterns:

MVP Pattern
o View is more loosely coupled to the model. The presenter is responsible for binding the model to the view.
o Easier to unit test because interaction with the view is through an interface
o Usually view to presenter map one to one. Complex views may have multi presenters.
MVC Pattern
o Controller are based on behaviors and can be shared across views
o Can be responsible for determining which view to display (Front Controller Pattern)

Categories: Development Tags:
Follow

Get every new post delivered to your Inbox.