Friday, August 16, 2013

Two ways to convert a single value to a collection

Just featuring this SO question: Is there a way of converting a T to a IEnumerable<T> without using Add or AddRange

Node item; 
IEnumerable<Node> list = (magic)item;

If rephrased. the question is how to implement magic without Add and AddRange methods.

I usually use the first posted code:

IEnumerable<Node> list = new[] { item };

However, this solution looks more elegant for me:

public static IEnumerable<T> ToEnumerable<T>(this T source)
    yield return source;

And use it like

IEnumerable<Node> aSequenceOfOne = item.ToEnumerable();
But  it is just matter of preferences.

Wednesday, August 14, 2013


I've been using Mercurial to track my pet projects, code snippets and so on for two years at lest. And I always stored all the settings (like username, enabled extensions etc.) in .hg/hgrc file. Copying the settings from repo to repo after each clone.

And yesterday (!) I occasionally found out about the file C:\Users\[your_name]\Mercurial.ini that contains all the system-wide Mercurial settings, like your favorite external diff tool and the default username.

Honestly, guys, know your tools. It will save you a lot of time.

Tuesday, August 13, 2013

Not using P4Merge from Mercurial command line

Following the previous post I have wondered why P4Merge worked fine for single file:

hg p4diff filename
hg p4diff
(if only a single file was changed in the repository).

If more than one file was changed,  P4Merge gave me a cryptic error:

As I learned from dumping the command line arguments, hg plainly provides a diff tool with two folders — base and working respectively.

This means that P4Merge is useless here — you have to use something like WinMerge or another tool with folder comparison feature. So, another line goes to .hg/hgrc file:

hgext.extdiff =
p4diff = p4merge
wdiff = winmergeu

Now I can type hg wdiff and get a nice folder comparison. Pity that P4Merge can't do folder comparison — I really like the way it presents the differences.

Monday, August 12, 2013

Using P4Merge from Mercurial command line

I like Mercurial and I like managing all the changes from command line. What I don't like is CLI-based diff.

So, to use P4Merge as from Mercurial command line, just add this to your .hg/hgrc:

hgext.extdiff =
p4diff = p4merge

And launch it as follows:

hg p4diff filename

or just

hg p4 filename

(Mercurial is smart and doesn't force you to type the whole command, only the part of name that identifies it)

Took a hint from this SO answer:

Now I wonder how to open p4merge for each of the changed files in the repository.

EDIT: this post covers the topic quite extensively.

Monday, August 5, 2013

Special characters in Windows Performance Monitor counter names

Some time ago I stumbled upon a problem with perfmon. I was trying to monitor a network interface called Intel(R) PRO/1000 MT Network Connection — that was what ipconfig /all gave to me. The problem was that the corresponding performance counter instance name was Intel[R] PRO_1000 MT Network Connection.

Obviously perfmon didn't like some characters and held them special. But which ones? What is the replacement convention?

LATE EDIT: It's documented here:
No need to read farther.

I failed to find the exact answers, but came up with these two articles:
  1. Using the PDH Functions to Consume Counter Data > Creating a Query > Specifying a Counter Path
  2. Retrieving and Modifying Data in ADO.NET > Performance Counters
The first one actually describes the special characters used by Performance Data Helper and Windows Performance Monitor. The second (although I found it first) contains a piece of code that replaces the special characters with some escape characters.

So, while the transformation is rather trivial (the code is from the second article):

string counterName = oldName.Replace('(', '[').Replace(')', ']')
                     .Replace('#', '_').Replace('/', '_').Replace('\\', '_');
you cannot be sure it covers all the characters. However, the first article enlists all the special characters used by perfmon:
\Generic separator for computer, object, and counter.
(Beginning of instance name.
)Ending of instance name.
/Separates instance and parent instance.
#nIdentifies a specific occurrence of a same-named instance.
* Wildcard character.

Besides, the asterisk must not be replaced, so adapter name Local Area Connection* 11 is exactly the same in perfmon.

So, when you use some performance monitor counters from you code, you have to applay a simple transformation to any user-entered counter names and instances (i.e. network adapter names, application pool names etc.).

This way you can be sure the code won't break when it encounters something with parentheses in its name.

Friday, July 26, 2013

Just a quick note

Last year I worked a lot with JavaScript developers learning C# to work on a ASP.NET project.
Currently I've been observing several guys with an extensive C++ experience learning C# to work on a WPF project.

Both JS and C++ guys are really good in their field and nothing like new to programing. This makes the observation of their progress really interesting. If they were coming from non-C-family languages, it would be a bit less interesting, as they would need to make themselves comfortable with the syntax. In this case, only ideas and concepts were the issue.

In fact, all of them tried to use C# in their usual way (certainly not in the way C# was meant to be used). The interesting thing was to see how diverse the intended misuse was.

Just one example — generics. While JS devs had a very hard time understanding just why we need this feature, C++ guys impatiently asked why generics are so much less functional than C++ templates.

Thus it a very interesting thing to see people learning new concepts — like static typing or garbage collection. It provides you with a unique experience of understanding how people think, work and learn and how good it is to know many different concepts and ideas.

Wednesday, May 1, 2013

Book Review: Domain-Driven Design: Tackling Complexity in the Heart of Software

DDD  itself

It has been some time since I read this fantastic book. As its title implies, the book is about Domain-Driven design. What is it? Wikipedia states the following:

Domain-driven design (DDD) is an approach to develop software for complex needs by connecting the implementation to an evolving model. The premise of domain-driven design is the following:
  • Placing the project's primary focus on the core domain and domain logic.
  • Basing complex designs on a model of the domain.
  • Initiating a creative collaboration between technical and domain experts to iteratively refine a conceptual model that addresses particular domain problems.
The definition above is long because it's hard to write a definition that will be short and formal at the same time. Moreover, this 'long and formal' definition does not really capture the ideas of DDD (at least as I see them):
  1. Developers should know and understand the concepts and ideas of business domain and base the design on the model of the domain. I mean, not only they should know about how invoices are handled in the app, but also understand the idea behind an invoice. That understanding is critical to creating designs that adhere to domain model, not fight the model.
  2. The domain language is really important in designing the application. Just consider the following sentences. 'We store legs of a journey in a table in the database' OR 'We persist itinerary information'. The second one will be better understood by a domain expert. He might even point out that we don't need to store the whole itinerary,
The lines above are probably no better than the formal definition, but they should give you the idea.

The book

The author talks a lot about ideas, pros and cons of DDD, refactorings that lead to more domain-oriented code. It also discusses several patterns of writing code that can be easily matched to corresponding domain ideas. For example, Strategy Pattern (as well as most of the classic GoF patterns) are not about DDD, but about lower-level OOD, and Specification Pattern is about real-world specifications transformed into code.

Thus the book covers all the levels of applying DDD: from strategic architecture decisions to writing code, it still tends to be more high-level — architecture and design get more attention than code. And I like this idea, because code may be relevant to many developers, but the ideas are relevant for everyone.

My impression

I really liked the book because it gave me several insights on system design and architecture, broadening the range of things I take into account when writing new products or redesigning the old ones. In the same time, on my level of expertise I might need a much shorter DDD intro book (like this one:

Would I recommend reading it? Definitely yes if you are interesting in software design. It won't make you a great software architect after you turn the last page, but it shows you the path.

Thursday, March 7, 2013

Breaking the silence

It's been a tough time recently. From the time of the latest post I've managed to read the Domain-Driven Design book, get quite proficient with MSBuild (for better or for worse) and have got promoted from a junior developer to a fully-functional developer.

I'm soon to write a review for the DDD book, and share some code samples with advanced MSBuild features I've learned. But I need time for that, so please be patient.