Mind the runtime optimization

Yan Cui

I help clients go faster for less using serverless technologies.

You might know this already, but in C# whenever you write something like this:

   1: if (MethodA() || MethodB())

   2: {

   3:     // do something

   4: }

it’s not guaranteed that both methods will be executed, so DO NOT DO THIS if you’re relying on both methods to be expected to cause some desirable side-effects.

The reason for this is simple, at runtime, as soon as one of the methods returns true the whole expression will evaluate to true regardless of the output of the second method. So as far as the runtime is concerned, it can safely skip the second part of the if condition as a form of runtime optimization.

Here’s a quick demo that shows this behaviour in action:

   1: public class MyClass

   2: {

   3:     public bool FlagA { get; set; }    

   4:     public bool FlagB { get; set; }

   5: }

   6:  

   7: public bool MethodA(MyClass myObj)

   8: {

   9:     myObj.FlagA = true;    

  10:     Console.WriteLine("Set FlagA to true");

  11:     

  12:     return true;

  13: }

  14:  

  15: public bool MethodB(MyClass myObj)

  16: {

  17:     myObj.FlagB = true;    

  18:     Console.WriteLine("Set FlagB to true");

  19:     

  20:     return true;

  21: }

  22:  

  23: ...

  24:  

  25: var myObj = new MyClass();

  26:  

  27: // this evaluates to true, but only MethodA is invoked

  28: var isMyObjChanged = MethodA(myObj) || MethodB(myObj);

  29:  

  30: // prints FlagA [True], FlagB[False]

  31: Console.WriteLine("FlagA [{0}], FlagB[{1}]", myObj.FlagA, myObj.FlagB);

This behaviour also applies outside to if statements like the one at the top of the post. I wasted some valuable minutes trying to solve a WTF bug resulting from this, hopefully it won’t catch you out too!


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


2 thoughts on “Mind the runtime optimization”

  1. You can get what you were after by using the bitwise operator instead of the logical one.

    ie, var isMyObjChanged = MethodA(myObj) | MethodB(myObj);

  2. Andy – ah, that’s a good idea, never came across my mind.

    The only thing I worry about doing this is if someone else comes along, thought it was a typo and changes it to ||! Nothing a little bit of comment won’t solve though ;-)

    Thanks for the suggestion!

Leave a Comment

Your email address will not be published. Required fields are marked *