Works on my machine

C#

Get rid of deep null checks

Have you ever had to write a code like this one? var store = GetStore(); string postCode = null; if (store != null && store.Address != null && store.Address.PostCode != null) postCode = store.Address.PostCode.ToSt... I’m sure you have. We’ve all been there. The goal is to retrieve (or compute) a value, but in order to do that, we have to get through many intermediate objects, which could be in a default state. The aforementioned example is pretty easy. Sometimes, along the way, ......

Proxy Swarm

From time to time people ask me to vote. They send me a link and say: "vote for my school", "vote for my recipe in a cooking contest", "vote on my child's drawing" etc. I'm sure it happens to you too. Personally, I don’t care, but it led me to an interesting thought – since it is possible to bypass IP restrictions through proxy gates, it should be also possible to make votes automatically. I only need to get a list of proxy servers. Luckily, I found a pretty big one under this link. The goal The ......

NSlice v. 1.2

New version of NSlice has been released. What’s new? SliceDelete extension method for enumerable types. (You can read what SliceDelete does in the previous post) Like the Slice extension for enumerable types, SliceDelete works in a full lazy fashion, buffers the minimum number of elements required to perform the requested operation and disposes the source enumerator as soon as it's possible. What’s next? If you look at http://docs.python.org/2.3/... you can notice that there ......

Code review

I’m going to review 3 C# code snippets I have recently come across. At first glance they all may seem to be fine - they do their job and they can be understood easily. So what is wrong about them? I will try to answer this question thoroughly, but before I do that, I would like to ask you to do that too. Don’t rush into reading the review, think what you would change, how you would implement that. Ideally, write down your solution, then compare. 1. String concatenation This is a pretty common scenario. ......

NSlice v. 1.1

New version of NSlice has been released. What’s new? SliceDelete extension method for indexed and string types. SliceDelete returns exactly opposite result than Slice. The result set is ordered in the same way as the original one. Look at the following example that returns first 2 and last 2 elements: var collection = Enumerable.Range(0, 10).ToList(); collection.SliceDelete(2, -2); //or collection.SliceDelete(-3, 1, -1); Result: { 0, 1, 8, 9 } You can read more about NSlice in one of the previous ......

Slicing for .NET

One year ago I had a little crush on Python. Although I consider myself as a hardcore C# coder I think it is a good thing to try other languages. Even if we do not plan to change our major, it is nice to grasp other techniques and practice new ways of thinking, which can be later incorporated into our favorite language. First odd thing I read about Python was that a developer can use negative array indices. My reaction was: Why would I even want to get an IndexOutOfRangeException? That is just silly. ......

Overuse of “as” keyword

Recently I realized that I am overusing "as" keyword. It is probably because its syntax is more fluent for me than ordinary cast. I see this overuse even in Microsoft examples. Let the following code show you what I mean: object x = "1.0"; Version v = x as Version; Console.WriteLine(v.Major); and just for reference reasons ordinary cast: object x = "1.0"; Version v = (Version)x; Console.WriteLine(v.Major); When you run first snippet it will crash on third line throwing NullReferenceException. Second ......

Beware the IEnumerable<T>

If you see IEnumerable<T> as read only collection crippled brother, this post is for you. Many times I found myself loosing couple of hours looking for a bug everywhere, but not in a place where it was. From time to time the bug is caused by my simplified perception of IEnumerable. The thing is, if we do not know the mechanism that serves specific IEnumerable elements, we cannot be sure that each iteration will return equal collections with same objects. It is not a big deal if it hosts immutable ......

Linq’s ZIP for adjacent items

I found that Linq’s ZIP is really great for adjacent item computations. For example, let’s have a collection of dates: var dates = new DateTime[] { new DateTime(2000,1,1), new DateTime(2000,1,2), new DateTime(2000,1,5) }; How would you compute time difference of adjacent items ? I like to use Zip for this kind of job: dates.Zip(dates.Skip(1), (d1, d2) => d2 - d1); As you might expect, the result will be: { 1 day, 3 days } ......