Spot the defect: Bad comparisons, part three

Did you notice how last time my length comparison on strings was unnecessarily verbose? I could have written it like this: static int ByLength(string x, string y){  if (x == null && y == null) return 0:  if (x == null) return -1;  if (y == null) return 1;  return CompareInts(x.Length, y.Length);} static int CompareInts(int…

22

Spot the defect: Bad comparisons, part two

Suppose I want to sort a bunch of strings into order first by length, and then, once they are sorted by length, sort each group that is the same length by some other comparison. We can easily build such a device with higher-order programming: static Comparison<string> FirstByLength(Comparison<string> thenBy){  return (string x, string y) =>  {   …

21

Spot the defect: Bad comparisons, part one

The mutable List<T> class provides an in-place sort method which can take a comparison delegate. It’s quite handy to be able to sort a list into order by being able to compare any two elements, but you have to make sure you get it right. First off, what are the requirements of the comparison delegate?…

15

Not as easy as it looks, Part Two

Holy goodness, did you guys ever find a lot of additional ways in which an “eliminate variable” refactoring can go wrong. Just a few of your observations: (again, in every case, “x” is eliminated.) Any situation in which x is being treated as a variable rather than a value will pose a problem. Some obvious…

21

Not as easy as it looks

My colleague Kevin works on (among many other things) the refactoring engine in the C# IDE. He and I were at the end of last year discussing the possible cases for a hypothetical “eliminate variable” refactoring. I thought that it might be of interest to you guys to get a glimpse of the sorts of…

41

Enormous Explosions

Welcome to 2011 everyone; I hope you all had as restful a time as I did over the winter break. We’ll get back into language and tools design next time; today’s post is dedicated to the memory of my friend Frank, an old and dear friend of my whole family who passed away shortly before…

13