When Not-True Doesn’t Equal False

There is a strangely common, reoccurring bug I’ve seen return, again and again, in most projects I’ve worked on in recent years. I call it the “Not True != False” bug, and it crops up, usually, when passing query parameters to a search service of some sort, when you want to search for entities who match a certain condition, and you end up not being able not to search by that condition.

Confusing? Let’s back up for a second.

One of the more common patterns in use is having our client run a query on our server by creating an object describing the query parameters. Call it the QueryFilter or SearchParams or QueryDTO or whatever. This class will encapsulate the parameters we want to search on:

public class PersonQueryFilter
{
    public string Name {get; set;}
    public bool IsVIP {get; set;}
}

Here, a simple query class that allows us to send a search to the server, returning a list of Person objects that match the given name, or that have the VIP property set to true. Simple, right? This is the common, straightforward and naive occurence of this bug that I see again and again. The developer needed a way to fetch all VIPs, so they added a field to specify VIPs.

Why is this a bug? Because as it stands, this class does not allow us to not search by IsVIP. We can ask for Persons that are VIPs. We can ask for Persons that aren't VIPs. But we can't ask for Persons regardless of whether they're VIPs. A boolean is simply that, a boolean. It doesn't encode information beyond yes/no. All matching entities must have either True or False.

This is a pernicious bug, but it will probably survive initial testing if the developer doesn't write comprehensive unit tests. That's because it will seem to work. "Write a feature that finds VIPs. Did it find VIPs? Yes, it did. Good, it works".

The solution to it is, of course, very simple. The opposite of "Find where X is True" is, in this case, not "Find where X is False", but "Find where X is irrelevant". For searching by Name, we can simply specify null as a marker for "don't search by this parameter". For our booleans, C# gives us the Nullable<bool> type (or bool?) to specify a third option for bool. Yes/No/Irrelevant. Other languages have their own solutions. Javascript can specify undefined or null for any data type. In Java you would probably use the Boolean object instead of the boolean primitive for many other reasons anyway (like generics), which can be set to null.

There are many workarounds. But many developers, especially relatively inexperienced ones, don't always think of the nullability of their search parameters.

Which is why I run into this bug again. And again. And again.

When Not-True Doesn’t Equal False

There is a strangely common, reoccurring bug I’ve seen return, again and again, in most projects I’ve worked on in recent years. I call it the “Not True != False” bug, and it crops up, usually, when passing query parameters to a search service of some sort, when you want to search for entities who match a certain condition, and you end up not being able not to search by that condition.

Confusing? Let’s back up for a second.

[Continued on Bits and Pieces, the new location of my blog]

Snippet of the Day: AsSingleton

מכירים את זה שאתם צריכים להעביר ערך בודד לפונקציה שמצפה לקבל מערך או רשימה? נמאס לכם לכתוב כל פעם

var myList = new List<MyClass> { myObject };

רק בשביל לקבל רשימה בת איבר אחד? אל דאגה, שוועתכם נשמעה!

אני פותח את פינת ה-Snippet of the Day עם שני פונקציות קטנטנות להפוך את הקוד לקריא יותר. אפשר גם בכלל לשנות את השם ל-AsList ו-AsArray לקבל קוד עוד יותר קצר, אבל העדפתי את הגרסה המפורשת יותר.

ועכשיו אפשר פשוט לכתוב:

var myList = myObject.ToSingletonList();

איך לשמור על אותיות גדולות וקטנות בעולם עוין

כמתכנת C#, אני רגיל לחשיבות של case sensitivity – משתנים בשם test ו-TEST הם שונים ונפרדים. גם ב-XML וב-JSON, שני הפורמטים הנפוצים לתעבורת מידע, נהוג להקפיד על case sensitivity בשמות של השדות/אלמנטים. ולרוב זה לא בעיה – אתה שומר עם case מסוים ואז קורא באותו case, והכל בסדר.

אבל היום נתקלתי בבעיה. במערכת מורכבת ומבוזרת שעליה אני עובד, מידע מעובד בקצה אחד, שדות נשמרים, והישות כולה נשמרת בתוך מסד נתונים מסוים, כשכל ערכי השדות שלה נשמרים תמיד ב-UPPERCASE. אבל היום הייתי צריך לשמור בתוך כמה שדות מידע שה-case שלו היה חשוב לשימור – במקרה הזה, שם קובץ מתוך שרת לינוקס עם מערכת קבצים רגישה ל-case, ובלוק של XML שגם בו הייתי חייב לשמור על גודל האותיות. אבל המנגנון היחיד שהיה לי לשמור את המידע בשביל להוציא אותו ולפעול עליו בקצה השני של המערכת לא משמר את ה-case של הנתונים. אז מה עושים?

המחשבה הראשונה שלי היתה לקודד את המידע ב-Base64, שמאפשר לקדד מידע שרירותי לתוך “שפה” של 64 תווים בלבד. אבל גם זו שפה גדולה מדי, וכוללת גם אותיות קטנות וגם גדולות, כך ש-Base64 הוא case sensitive גם הוא. אז השלב הבה היה לקודד את המידע לשפה “רזה” יותר, שאין בה את ההפרדה הזו בין אותיות גדולות וקטנות.

האופציה הבאה שלי היתה Base32 או Base36. שתיהן שיטות קידוד דומות ל-base64, אם כי פחות סטנדרטיות. הן לא case sensitive, שזה טוב, אבל בגלל שהן לא שיטת קידוד נפוצה, אין ב-.NET Framework תמיכה מובנית בהן, כמו שיש ל-base64. אפשר למצוא לא מעט מימושים, כמו כאן או כאן, אבל במקרה הזה לא רציתי להכניס קוד לא ידוע או ארוך מדי למערכת ברגע האחרון, למרות שברגע שירגע המצב קצת, יתכן שאחזור אליו.

אז האופציה הבאה אחרי כן היא פשוט לקודד את ה-bytes של הטקסט שלי כצמדי תווים הקסדצימליים – זו המרה די ישירה, ודרך די נפוצה להציג שטחי זכרון גולמיים. החסרון של זה הוא שזו שיטה בזבזנית הרבה יותר מ-base36 (שלא לומר base64), כי את אותו כמות מידע תדרוש יותר תווים באלף-בית ההקסדצימלי – שאפשר גם לכנות אותו base16 – מאשר בבסיסים הגדולים יותר. אבל היתרון הגדול הוא שיש כבר בתוך ה-framework דרך לעשות את ההמרה אוטומטית, מה שהופך את זמן הפיתוח – ובעיקר הבדיקות – לקצרצר הרבה יותר. אמנם המימוש הוא במחלקה קצת נידחת כחלק מהתמיכה ב-SOAP, אבל הוא שם, והוא נתמך מאז .NET 1.1 ב-2003. ועם האפשרות לכתוב את זה כ-extension method, אז השימוש בכלל נהיה קל מאד.

public static string ToHexBytes(this string plainText,
     Encoding encoding = null)
{
    encoding = encoding ?? Encoding.UTF8;
    var bytes = encoding.GetBytes(plainText);
    return new SoapHexBinary(bytes).ToString();
}

public static string FromHexBytes(this string hexText,
    Encoding encoding = null)
{
    encoding = encoding ?? Encoding.UTF8;
    var bytes = SoapHexBinary.Parse(hexText).Value;
    return encoding.GetString(bytes);
}

Building a Nullable ComboBox

I like WPF. It has a bit of a learning curve, but once you get over it, you have an extremely extensible UI model that you can tweak very deeply. Unfortunately, this mindset made Microsoft leave many of the built-in controls with some missing functionality, apparently with the intention that people extend it if they felt like it.

One painfully missing piece, for me, was the ability to clear a ComboBox’s selection back to null, for a non-mandatory selection. You can add a Null value to the ComboBox’s ItemSource, but you can’t select it – selecting a null-backed value will simply return the last-selected value.

After viewing some less than optimal options on StackOverflow, I decided to roll my own clearable, nullable ComboBox, using a combination of two techniques – editing the ComboBox’s ControlTemplate to add a little “x” button to the SelectionBox, and a set of Attached Properties to apply the logic to the ComboBox in question.

Read more

Attaching on Startup

Here’s a neat trick I stumbled onto that can make life a lot easier – in our development workstations, certainly, and even in a pinch on staging and testing machines:

How many times have you wished you could attach to a process immediately when it launchs? If we’re running a simple EXE, we can launch it from the debugger directly, but if we’re trying to debug a Windows Service, a scheduled task or a sub-process that is launched automatically, it isn’t that simple. We can try to press CTRL-ALT-P as quickly as possible, but that will almost always miss the very beginning. We can add a call to System.Diagnostics.Debugger.Launch() to our Application_Startup() function (or equivalent), but that’s polluting production code with debugging statements, and it isn’t really something we can send over to a staging or QA machine.

I was all set to write a tool to monitor new processes being launched and latch onto them when I discovered that there’s no need, and Windows provides me with this facility automatically!

All we need to do is create a new registry key under HKEY_LOCAL_MACHINESoftwareMicrosoftWindows NTCurrentVersionImage File ExecutionOptions and name it after the EXE we want to attach to.

Under this key, we create two values: A DWORD named PauseOnStartup with a value of 1, and a string named Debugger, with a value of vsjitdebugger.exe. Here’s a sample .REG file

Code Snippet
  1. Windows Registry Editor Version 5.00
  2. [HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionImage File Execution OptionsMyApplication.exe]
  3. “PauseOnStartup”=dword:00000001
  4. “Debugger”=”vsjitdebugger.exe”

 

Now, when MyApplication.exe launches, it will automatically launch the familiar “Choose the debugger to use” dialog, and we can point it to our solution of choice.

Quick and Dirty Code Timing

When looking to optimize our code, the first and best advice we should keep in mind is this: Measure. Don’t optimize before you know what takes time, and then only optimize where it makes sense.

Measuring can be done in many ways. Using Performance Counters, high-performance timers and stress tests. But often, especially in early iterations, we just want to get a ballpark figure for how long a bit of code takes. Maybe a low-level database call that gets used a lot, maybe as a wrapper in our initialization code to know what should be moved to lazy evaluation. In those cases, the quickest approach is to simply use .NET’s System.Diagnostics.Stopwatch class. Start it, stop it, and print out the timing.

So to ease this common pattern, and to illustrate, once more, the IDisposable pattern I so love to use, here’s a wrapper around Stopwatch, a TimedBlock class that lets you start a new using block around the code you want timed, and have the timing outputted to the Trace listeners. This can be seen in Visual Studio’s Output window, or in any Trace listener you care to add.

Here is the code:

public class TimedBlock : IDisposable { private readonly string _blockName; private readonly Stopwatch _timer; private static int _anonymousBlockCounter = 1; public TimedBlock(string blockName = null, params object[] arguments) { _blockName = !string.IsNullOrEmpty(blockName) ? string.Format(blockName, arguments) : "Anonymous #" + _anonymousBlockCounter++; _timer = new Stopwatch(); _timer.Start(); } public void Dispose() { if (_timer != null && _timer.IsRunning) { _timer.Stop(); Trace.WriteLine(string.Format("{0} took {1} seconds.", _blockName, _timer.Elapsed.TotalSeconds), "Timer"); } }

  public static TimedBlock Start(string blockName = null, params object[] arguments) { return new TimedBlock(blockName, arguments); } }

And here’s the usage:

using (new TimedBlock("Potentially slow action #{0} ", actionId)) {

DoSlowOperation(); }

 

Notes:

  • Just for kicks, I added two ways to initialize the TimedBlock. Either with new TimedBlock() or with TimedBlock.Start(). They are identical, merely stylistically different.
  • The block is initialized with a name. This is used to find it in the trace output. If no name is passed, a default name will be used.

Have fun with it. Remember, it’s just a quick and dirty timesaver. Use it well.

Not-So-Lazy Static Constructors in C# 4.0

A coworker pointed me towards an interesting blog post by John Skeet about the changed behavior of static constructors in C# 4.0 (yes, I know, it’s been a few years now, but I never ran into it).

It seems that C# 4.0 now tries to be lazier when instantiated static fields. So if I have a side-effect free static method that doesn’t touch any members, calling it will not bother initializing the static fields. From Skeet’s post:

  1. class Lazy
  2.     {
  3.         private static int x = Log();
  4.         private static int Log()
  5.         {
  6.             Console.WriteLine("Type initialized");
  7.             return 0;
  8.         }
  9.         public static void StaticMethod()
  10.         {
  11.             Console.WriteLine("In static method");
  12.         }
  13.     }

Calling StaticMethod() will print “In static method”, but will not print “Type Initialized”!

This was a bit worrying. John Skeet said it shouldn’t impact existing code, but we were not so sure. Imagine a class whose static constructor does some unmanaged initialization work (creates a Performance Counter, for instance) while the static method  writes to the counter. This could potentially cause a hard to find exception, since our expectation was that static constructors can be relied upon to always be called first.

So we ran a quick test (and by “we” I most mean Amir), and it seems that this behavior isn’t as problematic as we thought. Look at this code, adding a static constructor to the class above:

  1. class Lazy
  2.     {
  3.         private static int x = Log();
  4.         private static int Log()
  5.         {
  6.             Console.WriteLine("Type initialized");
  7.             return 0;
  8.         }
  9.  
  10.         static Lazy()
  11.         {
  12.             Console.WriteLine("In static constructor");
  13.         }
  14.  
  15.         public static void StaticMethod()
  16.         {
  17.             Console.WriteLine("In static method");
  18.         }
  19.     }

 

The only difference is that we have an explicit static constructor, rather than the implicit one that initializes the field. Unlike the first test case, in this case calling StaticMethod() did call the static constructor, and we could see “In static constructor” printed before “In static method”. The compiler is smart enough to see that we have an explicit constructor defined, so that means we want it to be called, and it will be called. This was reassuring.

But wait, there’s more! It seems that once type initialization was triggered by the presence of the explicit static constructor, it went all the way. Even the x parameter was initialized, the Log() method was called, and “Type Initialized” was printed to the console, even before the static constructor. This was the behavior I was used to, where static field initializations are added to the beginning of the .cctor.

To summarize, the new lazy type initialization behavior for C# 4.0 is interesting, since it allows static classes that contain only side-effect free methods (for instance, classes containing popular Extension Methods) to avoid expensive and unnecessary initializations. But it was designed smartly enough to recognize when initialization is explicitly desired, and be a bit less lazy in that case.

(And thanks again to Igal Tabachnik and Amir Zuker)

Not-So-Lazy Static Constructors in C# 4.0

A coworker pointed me towards an interesting blog post by John Skeet about the changed behavior of static constructors in C# 4.0 (yes, I know, it’s been a few years now, but I never ran into it).

It seems that C# 4.0 now tries to be lazier when instantiated static fields. So if I have a side-effect free static method that doesn’t touch any members, calling it will not bother initializing the static fields. From Skeet’s post:

  1. class Lazy
  2.     {
  3.         private static int x = Log();
  4.         private static int Log()
  5.         {
  6.             Console.WriteLine(“Type initialized”);
  7.             return 0;
  8.         }
  9.         public static void StaticMethod()
  10.         {
  11.             Console.WriteLine(“In static method”);
  12.         }
  13.     }

Calling StaticMethod() will print “In static method”, but will not print “Type Initialized”!

This was a bit worrying. John Skeet said it shouldn’t impact existing code, but we were not so sure. Imagine a class whose static constructor does some unmanaged initialization work (creates a Performance Counter, for instance) while the static method  writes to the counter. This could potentially cause a hard to find exception, since our expectation was that static constructors can be relied upon to always be called first.

So we ran a quick test (and by “we” I most mean Amir), and it seems that this behavior isn’t as problematic as we thought. Look at this code, adding a static constructor to the class above:

  1. class Lazy
  2.     {
  3.         private static int x = Log();
  4.         private static int Log()
  5.         {
  6.             Console.WriteLine(“Type initialized”);
  7.             return 0;
  8.         }
  9.  
  10.         static Lazy()
  11.         {
  12.             Console.WriteLine(“In static constructor”);
  13.         }
  14.  
  15.         public static void StaticMethod()
  16.         {
  17.             Console.WriteLine(“In static method”);
  18.         }
  19.     }

The only difference is that we have an explicit static constructor, rather than the implicit one that initializes the field. Unlike the first test case, in this case calling StaticMethod() did call the static constructor, and we could see “In static constructor” printed before “In static method”. The compiler is smart enough to see that we have an explicit constructor defined, so that means we want it to be called, and it will be called. This was reassuring.

But wait, there’s more! It seems that once type initialization was triggered by the presence of the explicit static constructor, it went all the way. Even the x parameter was initialized, the Log() method was called, and “Type Initialized” was printed to the console, even before the static constructor. This was the behavior I was used to, where static field initializations are added to the beginning of the .cctor.

To summarize, the new lazy type initialization behavior for C# 4.0 is interesting, since it allows static classes that contain only side-effect free methods (for instance, classes containing popular Extension Methods) to avoid expensive and unnecessary initializations. But it was designed smartly enough to recognize when initialization is explicitly desired, and be a bit less lazy in that case.

(And thanks again to Igal Tabachnik and Amir Zuker)

Attaching on Startup

Here’s a neat trick I stumbled onto that can make life a lot easier – in our development workstations, certainly, and even in a pinch on staging and testing machines:

How many times have you wished you could attach to a process immediately when it launchs? If we’re running a simple EXE, we can launch it from the debugger directly, but if we’re trying to debug a Windows Service, a scheduled task or a sub-process that is launched automatically, it isn’t that simple. We can try to press CTRL-ALT-P as quickly as possible, but that will almost always miss the very beginning. We can add a call to System.Diagnostics.Debugger.Launch() to our Application_Startup() function (or equivalent), but that’s polluting production code with debugging statements, and it isn’t really something we can send over to a staging or QA machine.

I was all set to write a tool to monitor new processes being launched and latch onto them when I discovered that there’s no need, and Windows provides me with this facility automatically!

All we need to do is create a new registry key under HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Image File ExecutionOptions and name it after the EXE we want to attach to.

Under this key, we create two values: A DWORD named PauseOnStartup with a value of 1, and a string named Debugger, with a value of vsjitdebugger.exe. Here’s a sample .REG file

Code Snippet
  1. Windows Registry Editor Version 5.00
  2. [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\MyApplication.exe]
  3. “PauseOnStartup”=dword:00000001
  4. “Debugger”=”vsjitdebugger.exe”

 

Now, when MyApplication.exe launches, it will automatically launch the familiar “Choose the debugger to use” dialog, and we can point it to our solution of choice.