Quick Links

Recent Changes

    18.2.2020 - When [NotNull] is null

    I prefer to be very explicit about nullability of references, wherever possible. Happily, most modern languages support this feature non-nullable references natively (e.g. TypeScript, Swift, Rust, Kotlin).

    As of version 8, C# also supports non-nullable references, but we haven't migrated to using that enforcement yet. Instead, we've used the JetBrains nullability annotations for years.1

    Recently, I ended up with code that returned a null even though R# was convinced that the value could never be null.

    The following code looks like it could never produce a null value, but somehow it does.

    [NotNull] // The R# checker will verify that the method does not return null
    public DynamicString GetCaption()
    {
      var result = GetDynamic() ?? GetString() ?? new DynamicString();
    }
    
    [CanBeNull]
    private DynamicString GetDynamic() { ... }
    
    [CanBeNull]
    private string GetString() { ... }
    

    So, here we have a method GetCaption() whose result can never be null. It calls two methods that may return null, but then ensures that its own result can never be null by creating a new object if neither of those methods produces a string. The nullability checker in ReSharper is understandably happy with this.

    At runtime, though, a call to GetCaption() was returning null. How can this be?

    The Culprit: An Implicit Operator

    There is a bit of code missing that explains everything. A DynamicString declares implicit operators that allow the compiler to convert objects of that type to and from a string.

    public class DynamicString
    {
      // ...Other stuff
    
      [CanBeNull]
      public static implicit operator string([CanBeNull] DynamicString dynamicString) => dynamicString?.Value;
    }
    

    A DynamicString contains zero or more key/value pairs mapping a language code (e.g. "en") to a value. If the object has no translations, then it is equivalent to null when converted to a string. Therefore, a null or empty DynamicString converts to null.

    If we look at the original call, the compiler does the following:

    1. The call to GetDynamic() sets the type of the expression to DynamicString.
    2. The compiler can only apply the ?? operator if both sides are of the same type; otherwise, the code is in error.
    3. Since DynamicString can be coerced to string, the compiler decides on string for the type of the first coalesced expression.
    4. The next coalesce operator (??) triggers the same logic, coercing the right half (DynamicString) to the type it has in common with the left half (string, from before).
    5. Since the type of the expression must be string in the end, even if we fall back to the new DynamicString(), it is coerced to a string and thus, null.

    Essentially, what the compiler builds is:

    var result = 
      (string)GetDynamic() ?? 
      GetString() ?? 
      (string)new DynamicString();
    

    The R# nullability checker sees only that the final argument in the expression is a new expression and determines that the [NotNull] constraint has been satisfied. The compiler, on the other hand, executes the final cast to string, converting the empty DynamicString to null.

    The Fix: Avoid Implicit DynamicString-to-string Conversion

    To fix this issue, I avoided the ?? coalescing operator. Instead, I rewrote the code to return DynamicString wherever possible and to implicitly convert from string to DynamicString, where necessary (instead of in the other direction).

    public DynamicString GetCaption()
    {
      var d = GetDynamic();
      if (d != null)
      {
        return d;
      }
    
      var s = GetString();
      if (s != null)
      {
        return s; // Implicit conversion to DynamicString
      }
    
      return GetDefault();
    }
    

    Conclusion

    The takeaway? Use features like implicit operators sparingly and only where absolutely necessary. A good rule of thumb is to define such operators only for structs which are values and can never be null.

    I think the convenience of being able to use a DynamicString as a string outweighs the drawbacks in this case, but YMMV.



    1. Java also has @NonNull and @Nullable annotations, although it's unclear which standard you're supposed to use.

    4.2.2020 - Configuring .NET Framework Assembly-binding Redirects

    After years of getting incrementally better at fixing binding redirects, I've finally taken the time to document my methodology for figuring out what to put into app.config or web.config files.

    The method described below works: when you get an exception because the runtime gets an unexpected version of an assembly---e.g. "The located assembly’s manifest definition does not match the assembly reference"---this technique lets you formulate a binding-redirect that will fix it. You'll then move on to the next binding issue, until you've taken care of them all and your code runs again.

    Automatic Binding Redirects

    If you have an executable, you can usually get Visual Studio (or MSBuild) to regenerate your binding redirects for you. Just delete them all out of the app.config or web.config and Rebuild All. You should see a warning appear that you can double-click to generate binding redirects.

    If, however, this doesn't work, then you're on your own for discovering which version you actually have in your application. You need to know the version or you can't write the redirect. You can't just take any number: it has to match exactly.

    Testing Assemblies

    Where the automatic generation of binding redirects doesn't work is for unit-test assemblies.

    My most recent experience was when I upgraded Quino-Windows to use the latest Quino-Standard. The Quino-Windows test assemblies were suddenly no longer able to load the PostgreSql driver. The Quino.Data.PostgreSql assembly targets .NET Standard 2.0. The testing assemblies in Quino-Windows target .NET Framework.

    After the latest upgrade, many tests failed with the following error message:

    Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    This is the version that it was looking for. It will either be the version required by the loading assembly (npgsql in this case) or the version already specified in the app.config (that is almost certainly out of date).

    Which File Was Loaded?

    To find out the file version that your application actually uses, you have to figure out which assembly .NET loaded. A good first place to look is in the output folder for your executable assembly (the testing assembly in this case).

    If, for whatever reason, you can't find the assembly in the output folder---or it's not clear which file is being loaded---you can tease the information out of the exception itself.

    1. From the exception settings, make sure that the debugger will stop on a System.IO.FileLoadException
    2. Debug your test
    3. The debugger should break on the exception

    Click "View Details" to show the QuickWatch window for the exception. There's a property called FusionLog that contains more information.

    The log is quite detailed and shows you the configuration file that was used to calculate the redirect as well as the file that it loaded.

    Which Version Is It?

    With the path to the assembly in hand, it's time to get the assembly version.

    Showing the file properties will most likely not show you the assembly version. For third-party assemblies (e.g. Quino), the file version is often the same as the assembly version (for pre-release versions, it's not). However, Microsoft loves to use a different file version than the assembly version. That means that you have to open the assembly in a tool that can dig that version out of the assembly manifest.

    The easiest way to get the version number is to use the free tool JetBrains DotPeek or use the AssemblyExplorer in JetBrains ReSharper or JetBrains Rider.

    You can see the three assemblies that I had to track down in the following screenshot.

    Writing Binding Redirects

    Armed with the actual versions and the public key-tokens, I was ready to create the app.config file for my testing assembly.

    And here it is in text/code form:

    <configuration>
      <runtime>
        <assemblybinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentassembly>
            <assemblyidentity name="System.Numerics.Vectors" publicKeyToken="B03F5F7F11D50A3A" culture="neutral">
            <bindingredirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0">
          </dependentassembly>
          <dependentassembly>
            <assemblyidentity
     name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="B03F5F7F11D50A3A" culture="neutral">
            <bindingredirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0">
          </dependentassembly>
          <dependentassembly>
            <assemblyidentity
     name="System.Threading.Tasks.Extensions" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral
            ">
            <bindingredirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1">
          </dependentassembly>
        </assemblybinding>
      </runtime>
    </configuration>
    
    21.1.2020 - Looking for Developers in 2020

    2020 is shaping up to be a busy year...so we're looking for help from anyone who's got what it takes and who's interested in working on interesting projects with a great team.

    Please take a minute to check out the following job descriptions.

    If you are interested, please don't hesitate to apply from the pages linked above. If you know of someone who might be interested, we'd appreciate it if you could let them know that we're looking for them.

    Thanks!

    6.12.2019 - Bees and Honey 2019

    Encodo spent part of this year learning about bees and honey. We met several times throughout the spring and summer to take care of the bees, gather honey, extract it from the honeycomb, fill it into jars, make labels and, finally, pack it all up and send it out to our customers for the end-of-year present. We hope they enjoy it over the holidays!

    In addition to the pictures below, we made several videos throughout the process:

    Beekeeping:

    Harvesting:

    Filling:

    27.11.2019 - Networking Event 2019.2: End-to-End Web Testing

    Richi presented the requirements for and history of end-to-end testing, from Selenium to the more modern, flexible and fast solutions we use at Encodo, like Cypress and TestCafé.

    14.11.2019 - Improving NUnit integration with testing harnesses

    imageThese days nobody who's anybody in the software-development world is writing software without tests. Just writing them doesn't help make the software better, though. You also need to be able to execute tests -- reliably and quickly and repeatably.

    That said, you'll have to get yourself a test runner, which is a different tool from the compiler or the runtime. That is, just because your tests compile (satisfy all of the language rules) and could be executed doesn't mean that you're done writing them yet.

    Testing framework requirements

    Every testing framework has its own rules for how the test runner selects methods for execution as tests. The standard configuration options are:

    • Which classes should be considered as test fixtures?
    • Which methods are considered tests?
    • Where do parameters for these methods come from?
    • Is there startup/teardown code to execute for the test or fixture?

    Each testing framework will offer different ways of configuring your code so that the test runner can find and execute setup/test/teardown code. To write NUnit tests, you decorate classes, methods and parameters with C# attributes.

    The standard scenario is relatively easy to execute -- run all methods with a Test attribute in a class with a TestFixture attribute on it.

    Test-runner Requirements

    There are legitimate questions for which even the best specification does not provide answers.

    When you consider multiple base classes and generic type arguments, each of which may also have NUnit attributes, things get a bit less clear. In that case, not only do you have to know what NUnit offers as possibilities but also whether the test runner that you're using also understands and implements the NUnit specification in the same way. Not only that, but there are legitimate questions for which even the best specification does not provide answers.

    At Encodo, we use Visual Studio 2015 with ReSharper 9.2 and we use the ReSharper test runner. We're still looking into using the built-in VS test runner -- the continuous-testing integration in the editor is intriguing1 -- but it's quite weak when compared to the ReSharper one.

    So, not only do we have to consider what the NUnit documentation says is possible, but we must also know what how the R# test runner interprets the NUnit attributes and what is supported.

    Getting More Complicated

    Where is there room for misunderstanding? A few examples,

    • What if there's a TestFixture attribute on an abstract class?
    • How about a TestFixture attribute on a class with generic parameters?
    • Ok, how about a non-abstract class with Tests but no TestFixture attribute?
    • And, finally, a non-abstract class with Tests but no TestFixture attribute, but there are non-abstract descendants that do have a TestFixture attribute?

    In our case, the answer to these questions depends on which version of R# you're using. Even though it feels like you configured everything correctly and it logically should work, the test runner sometimes disagrees.

    • Sometimes it shows your tests as expected, but refuses to run them (Inconclusive FTW!)
    • Or other times, it obstinately includes generic base classes that cannot be instantiated into the session, then complains that you didn't execute them. When you try to delete them, it brings them right back on the next build. When you try to run them -- perhaps not noticing that it's those damned base classes -- then it complains that it can't instantiate them. Look of disapproval.

    Throw the TeamCity test runner into the mix -- which is ostensibly the same as that from R# but still subtly different -- and you'll have even more fun.

    Improving Integration with the R# Test Runner

    At any rate, now that you know the general issue, I'd like to share how the ground rules we've come up with that avoid all of the issues described above. The text below comes from the issue I created for the impending release of Quino 2.

    Environment

    • Windows 8.1 Enterprise
    • Visual Studio 2015
    • ReSharper 9.2

    Expected behavior

    Non-leaf-node base classes should never appear as nodes in test runners. A user should be able to run tests in descendants directly from a fixture or test in the base class.

    Observed behavior

    Non-leaf-node base classes are shown in the R# test runner in both versions 9 and 10. A user must navigate to the descendant to run a test. The user can no longer run all descendants or a single descendant directly from the test.

    Analysis

    Relatively recently, in order to better test a misbehaving test runner and accurately report issues to JetBrains, I standardized all tests to the same pattern:

    • Do not use abstract anywhere (the base classes don't technically need it)
    • Use the TestFixture attribute only on leaf nodes

    This worked just fine with ReSharper 8.x but causes strange behavior in both R# 9.x and 10.x. We discovered recently that not only did the test runner act strangely (something that they might fix), but also that the unit-testing integration in the files themselves behaved differently when the base class is abstract (something JetBrains is unlikely to fix).

    You can see that R# treats a non-abstract class with tests as a testable entity, even when it doesn't actually have a TestFixture attribute and even expects a generic type parameter in order to instantiate.

    Here it's not working well in either the source file or the test runner. In the source file, you can see that it offers to run tests in a category, but not the tests from actual descendants. If you try to run or debug anything from this menu, it shows the fixture with a question-mark icon and marks any tests it manages to display as inconclusive. This is not surprising, since the test fixture may not be abstract, but does require a type parameter in order to be instantiated.

    image

    Here it looks and acts correctly:

    image

    I've reported this issue to JetBrains, but our testing structure either isn't very common or it hasn't made it to their core test cases, because neither 9 nor 10 handles them as well as the 8.x runner did.

    Now that we're also using TeamCity a lot more to not only execute tests but also to collect coverage results, we'll capitulate and just change our patterns to whatever makes R#/TeamCity the happiest.

    Solution

    • Make all testing base classes that include at least one {{Test}} or {{Category}} attribute {{abstract}}. Base classes that do not have any testing attributes do not need to be made abstract.

    Once more to recap our ground rules for making tests:

    • Include TestFixture only on leafs (classes with no descendants)
    • You can put Category or Test attributes anywhere in the hierarchy, but need to declare the class as abstract.
    • Base classes that have no testing attributes do not need to be abstract
    • If you feel you need to execute tests in both a base class and one of its descendants, then you're probably doing something wrong. Make two descendants of the base class instead.

    When you make the change, you can see the improvement immediately.

    image


    1. ReSharper 10.0 also offers continuous integration, but our experiments with the EAP builds and the first RTM build left us underwhelmed and we downgraded to 9.2 until JetBrains manages to release a stable 10.x.

    14.11.2019 - Encodo New Year's Game 2013 Tips & Tricks

    At the end of last year, Encodo shipped a little JRPG-style game to customers and partners. JRPG stands for "Japanese Role-Playing Game" and is usually shown in a top-down or isometric view.

    Our version uses Jaws, a low-level, open-source, Javascript game engine but, after some research into alternatives, we ended up writing most of the logic and rendering in our own layer on top of it. We tried to keep the code relatively clean and tested it on most desktop and mobile browsers.

    Our game takes place in our office here in Winterthur and features all of the Encodo employees. Quoting from the game:

    In order to get a surprise this coming spring, there are a few tasks you'll have to perform. Each employee has a piece of the solution that you can earn either by answering their questions or by bringing them an object that they lost or that they would like.

    You finish the game when you've collected all of the puzzle pieces that you get by answering each employee's questions -- or by giving them an object that they want. The puzzle pieces fit together to reveal a message.

    image image image image

    You can play the game by browsing to the Encodo RPG 2013; it's available in English and German. You're only eligible for a gift if we sent you a card with a code on it, but you can still send us a message at the end if you finish the game.

    Once you get past the intro screen and some instructions, you're dropped into the game and can start to look around. In the top-left corner is the table on which you'll find all of the different items that you can use in the game -- and that you can use to "convince" employees to give you their puzzle piece if you can't answer their questions.

    As you play, the game keeps track of "points" as well as progress. In order to get 100% progress and complete the game, you have to get each employee's puzzle piece. The number of points you have at the end reflects how efficiently you did this.

    • You lose points for each incorrect answer
    • You gain more points for a correct answer than by trading an item for the puzzle piece
    • You can earn extra points by finding secrets, hinted at in the instructions that "[y]ou can also interact with other parts of the office"
    • If you need more help finding secrets, try turning off the lights (as shown in "Encodo at Night"); not only are employees highlighted, but so are a few other places (including the light switch ;-).
    • Sometime just activating the secret isn't enough; sometimes you need a specific object1 in order to find the secret.
    • You can check your progress toward completing the puzzle at any time by pressing F1 or H or by pressing the "help" icon in the top-right corner (as shown in "Help Screen").
    • You can skip the instructions at the beginning by pressing "Esc"

    And, in case you don't finish it, we'd like to give credit where credit is due:


    | Credits |

    Story

    Marco von Ballmoos
    Remo von Ballmoos
    Daniel Roth

    Programming

    Armin Bilibani
    Stephan Hauser
    Daniel Roth
    Pascal Stählin
    Marco von Ballmoos

    Graphics

    Armin Bilibani
    Stephan Hauser

    Music

    Derris-Kharlan

    Powered by Jaws and Encodo RPG Engine



    1. Spoiler alert: the items you can use to find secrets are the SSD and the cappuccino. (Select the text to reveal the answer.)

    12.11.2019 - Improving WebPack Performance

    This is a WintiWebDev talk about optimizing WebPack configurations for TypeScript/React/Less applications

    17.10.2019 - Azure Linked Accounts and SSH Keys

    Azure DevOps allows you to link multiple accounts.

    Our concrete use case was:

    • User U1 was registered with an Azure DevOps organization O1
    • Microsoft did some internal management and gave our partner account a new organization O2, complete with new accounts for all users. Now I have user U2 as well, registered with O2.
    • U2 was unable to take tests to qualify for partner benefits, so I had to use U1 but link the accounts so that those test results accrued to O2 as well as O1.
    • We want to start phasing out our users from O1, so we wanted to remove U1 from O1 and add U2

    Are we clear so far? U1 and U2 are linked because reasons. U1 is old and busted; U2 is the new hotness.

    The linking has an unexpected side-effect when managing SSH keys. If you have an SSH key registered with one of the linked accounts, you cannot register an SSH key with the same signature with any of the other accounts.

    This is somewhat understandable (I guess), but while the error message indicates that you have a duplicate, it doesn't tell you that the duplicate is in another account. When you check the account that you're using and see no other SSH keys registered, it's more than a little confusing.

    Not only that, but if the user to which you've added the SSH key has been removed from the organization, it isn't at all obvious how you're supposed to access your SSH key settings for an account that no longer has access to Azure DevOps (in order to remove the SSH key).

    Instead, you're left with an orphan account that's sitting on an SSH key that you'd like to use with a different account.

    So, you could create a new SSH key or you could do the following:

    • Re-add U1 to O1
    • Remove SSH key SSH1 from U1
    • Register SSH key SSH1 with U2
    • Profit

    If you can't add U1 to O1 anymore, then you'll just have to generate and use a new SSH1 key for Azure. It's not an earth-shatteringly bad user experience, but interesting to see how several logical UX decisions led to a place where a couple of IT guys were confused for long minutes.

    17.10.2019 - Visual Studio 2019 Survey

    Visual Studio 2019 (VS) asked me this morning if I was interested in taking a survey to convey my level of satisfaction with the IDE.

    VS displays the survey in an embedded window using IE11.1 I captured the screen of the first thing I saw when I agreed to take the survey.

    I know it's the SurveyMonkey script that's failing, but it's still not an auspicious start.



    1. I'd just upgraded to Windows 10 build 1903, which includes IE 11.418.18362.0. I can't imagine that they didn't test this combination.