Software Testing Blog

Category: Ask The Bug Guys

Better than nothing

Here’s a question I get occasionally: This gives an error stating that U must be a reference type to use it in C<U>. But the outer constraint says that T is a reference type, and the inner constraint says that U is a T, so why doesn’t the compiler know that U is a reference…

Continue reading »

Spot the defect: randomness

Today on Ask The Bug Guys I’m going to turn things around a bit and ask you to find and explain the bug. Suppose we want to generate a series of pseudo-random integers between 1 and 6 (inclusive) to simulate rolling a fair die. In the class library there is the useful random.Next(min, max) method…

Continue reading »

Ontogeny, phylogeny and virtual methods

Today on Ask The Bug Guys, a question I get occasionally, particularly from C++ programmers learning C#: I’ve heard that it’s a bad idea to call a virtual method from a constructor in C++ (or C#). Does the same advice apply to C# (or C++)? Great question. Calling a virtual method from a constructor in…

Continue reading »

Scheduling a Transfer

Like many people these days, I do much of my business online, including much of my banking. A few weeks ago, I tried to schedule a transfer through a bank’s online web-based banking application. After carefully entering my request, and clicking on submit, my browser returned a page containing the text Error 500: java.lang.NullPointerException. Had…

Continue reading »

Delegates and structural identity

Suppose we have a couple methods: and couple delegate types: And some code which calls the method: A question I am frequently asked is “why is there a type mismatch error on the last line, but the previous two lines work fine?” They appear to all do the same thing, so why is one of…

Continue reading »

Some enumerator bounds checks are omitted

This week’s episode of ATBG requires that you first understand the previous episode, so please read that first if you haven’t already. I’ll wait. … Welcome back. Reader Xi Chen notes that the mutable struct implementation of the list enumerator mentioned last time does implement IEnumerator<T> and IEnumerator, and asks why it is that the…

Continue reading »

Why does my enumerator not advance?

Here’s an interesting bug pattern that I am occasionally asked about, involving iteration over a collection that appears to not move forwards. Before we get into that though I’ll need to introduce a little helper method: The behaviour of this method is utterly trivial: it takes an action and a value and performs the action…

Continue reading »

Why does my code not crash?

For a bit of a change of pace, today on ATBG I’ll talk about mostly C and C++, with a little Java and C# thrown in at the end. A very common question I see on StackOverflow in the “C” and “C++” tags is “here’s a clearly buggy program that I wrote; why does it…

Continue reading »

Warnings vs errors

Today on Ask the Bug Guys, reader Raghavendra has a question about this bug: if someValue is of a non-nullable value type that defines an equality operator — like, int or Guid — then this code is legal, but almost certainly wrong, since the condition will always be true. Raghavendra’s question is why should the…

Continue reading »

Why does C# use UTF-16 for strings?

Today on ATBG a langauge design question from reader Filipe, who asks: Why does C# use UTF-16 as the default encoding for strings instead of the more compact UTF-8 or the fixed-width UTF-32? Good question. First off I need to make sure that all readers understand what these different string formats are. Start by reading…

Continue reading »