måndag 10 december 2018

Overuse Example of the Null Coalescing Operator (??) and Null-Conditional Operator (?.)

The sweet taste of syntactic sugar

From Wikipedia
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

Sometimes though, too much of the sweetness can become a mouthful.

Suppose you need this to guard against collections that are null or empty

but in the rush you head directly for the sweet style. Its shorter and doesn't repeat the variable name, but is it really more readable?

Last week I saw code like that. Since my own code was dependant on that method I really would like it to be correct. At a first glance it appeared to be buggy. So I tried it out in LinqPad and saw that it behaved totally correct and therefore felt a bit perplexed. Do you?

There's three cases
  1. collectionOfInts has at least one value
  2. collectionOfInts is empty
  3. collectionOfInts is null
I find the first and second case pretty straightforward, but I couldn't evaluate the null scenario in my head. For instance, I became unsure about the operator precedence between "!" and "??".


It was when I started to dissect the code it became apparent what it was that confused me.

I expanded

collectionOfInts == null ? null : collectionOfInts.Any()

and saw that I had a type conversion error. I had to cast null to a type.
collectionOfInts == null ? (bool?)null : collectionOfInts.Any()

Up until now I hadn't realized that the result from the "?."-operator was a nullable bool. Reading up on operator precedence I learned that the "!"-operator has much higher precedence than the "??". Those are two clues to what goes on here.

What type and value do you think the variable "result" will get in the code below?

As shown below, "result" is a nullable bool that is null! Not null is null!

This means that when collectionOfInts is null the code can be rewritten like this:
!collectionOfInts?.Any() ?? true 
!(bool?)null ?? true
(bool?)null ?? true

So, no sugar in this case?

After having discussed the code with the author and realizing that he also had trouble understanding what happens in the null case, I say "no"! The sugar is there to make the code more readable, but apparantly it doesn't in this case.

Make your own sugar

There is a solution though, that I find easiest to read, but was a bit surprising when I first saw it, and that is:
if (collectionOfInts.IsNullOrEmpty())

The thing that surprised me was that the author believed that you could call a method on an object that is null. But you kind of can. If the method is an extension method.

If you make an extension method like the one below it also works for strings, since a string implements IEnumerable<char>

Is it worth using the extension method and risking surprising a reader of the code?
People seem to disagree about that :)


onsdag 28 november 2018

My first project in Python: a sudoku solver


It was a weekend a few weeks ago when my youngest child saw a sudoku in the paper and wanted to solve it. It was a hard one. And when they are hard I can't keep it all in the head and need to write remaning possible numbers down, but most often I don't find room for that. And for hard sudokus you sometimes have to guess for a number and try that out. When that guess turns out to be wrong, it is no fun at all to backtrack to how the sudoku board looked like before the guess. Both problems can be solved by a computer though! :)

Last time I wrote a sudoku solver was back in 2006 when learning Ruby. I've looked in all my stashes for that code, but it seems to be lost forever. This time the language that I've been looking at besides C# was Python, but so far I hadn't written anything else than short exercises, so it seemed like a good fit to try using it for a small project like this.

I don't even dare to google for sudoku solvers, there probably exist thousands of them, but I want to do this small project in my own little bubble.

Used techniques


This is what the board looks like. The nine squares on the board will be called "big squares", and the dots represents the squares that will be called just "squares" or "small squares".
In this example the user has entered two known values, a 9 and a 5. 

When running the solver in knockout mode it will knock out the known values from the list of possible values for all other squares. In the example below the value 9 has been knocked out from all squares inside the yellow marking which contains:
1. the big square that the 9 is in,
2. the row the 9 is in
3. and the column the 9 is in

The same goes for the 5, which affect all squares inside the red marking.

When there is a list of numbers in a small square, it is a list of values that still is valid to try out for that small square for solving the sudoku. Therefore, the lists of possible values inside the yellow marking all lacks the number nine, and all the lists inside the red marking lacks the number 5. Small squares that are inside both the yellow and red marking lacks both 9 and 5.

When the knockout runs and a value is knocked out from a square which value list suddenly only contains a single value, that square has become solved by the algorithm. Another round of knockout will be run for that square so its new value will be knocked out from all the squares the solved square affects.

Find unique value

When no more squares can be solved using the knockout technique, the next thing to look for is a list which contains a value that is unique in its big square. In the example in the image below, the square inside the red marking is the only square inside its big square (yellow) that still has the value 7 as a possible value. Therefore the square can be solved with that value and might kick off a new round of knockouts. This example won't though

Brute force

When no more can be done by the other two techniques, it's guessing time. For this, the solver goes looking for the first square with the lowest count of remaining possible numbers. In the board below it is the square marked with red that contains possible numbers 3 and 7.

The solver will begin to try with the number 3. So it sets the square to that value and then runs the "knockout" and "find unique value" routines. 
For this guess the board ends up looking like the board in the top of the image below. This board state is a fail because of the solved numbers the big square in the middle of the bottom row, there are two occurences of the number 8.

That erroneus state makes the solver understand that it has to backtrack to the last guess and try out the other value, in this case number 7.

Some sudokus requires multiple squares to be solved using the brute force routine. In the example above the printout says "Brute forcing 4 squares", which means that three other squares has gotten their value by guessing before the processing of the current square.

The code

The solution is a bit unfocused because I never decided if I wanted to build a complete solver or just a helper that could help us a little bit when trying to solve hard sudokus during the weekends. 

Another reason the code might seem strange is that I wanted to investigate the functional side of Python, but didn't get any relevation. Which isn't too strange I just found out, reading the answers in the link below. 

Over all I would say that Python isn’t very functional at all, and if you try to force a functional style into python it doesn’t feel very natural, and you will have to break out of it each time you want to use a third party library.

So writing a solver in F# or Haskell might be material for another project, but currently I'm a bit fed up with sudokus :)

torsdag 22 november 2018

Do you know the keyboard shortcuts for the kanban board in TFS?

Here's a list with lots of keyboard shortcuts for TFS and Azure DevOps (former Visual Studio Team Services former Visual Studio Online).

The shortcuts below are the ones I've been missing the most. Sometimes it takes quite a bit of patience to move a work item around the board, especially if there is scrolling involved. I'll try them out first thing at work tomorrow!

And there seems to be a rich text editor coming up!

tisdag 13 november 2018

Alter template for "Create and initialize field" in Visual Studio to get fields prefixed with underscore

I've got a fresh installation of Visual Studio and this time I haven't added Resharper. And so far I don't miss it! I haven't been hammering out that much code, but it seems like the built in code assistent in Visual Studio has become way better, because I usually, almost immediately, greatly miss the extra help I get from Resharper.

What I've already found though, is that its auto generation of fields from parameters to the constructor needs some tweaking. Well, if you like to prefix your fields with an underscore that is.

In case you didn't know, you can position the marker on a new constructor parameter and press "Ctrl + ." to open up the Quick Actions and Refactorings menu.
Then you can choose to Create and initialize field and get presented with a preview of what the result will look like.
The images below shows what it looks like with the default setting.

Visual Studio automatically creates the field "arg1" and assigns it with the constructor parameter.

I prefer fields to be prefixed with underscore to make it possible to immediately distinguish fields from local variables when reading the code, which I think is a great help when trying to get a grip of what the code does. And it seems to be the standard for .net core.

To change the template for this, go to 
Tools -> Options -> Text Editor -> C# -> Code Style -> Naming

Click Manage naming styles. Add a naming style like the one below.

Add a new row by clicking the plus. 
Select Private or Internal Field as Specification.
Select your new naming style as the Required Style.

Click OK. 
I had to restart Visual Studio make the update work.

Now it looks like this:

And the result is:

Of course Stack Overflow had the remedy when I became tired of renaming my auto generated fields and removing "this.". So thanks for that kspearrin and Maciek! :)

onsdag 7 november 2018

Fun fact: Origin of "upper-case" and "lower-case"

UPPER-CASE and lower-case

These terms originated from the common layouts of the shallow drawers called type cases used to hold the movable type for letterpress printing. Traditionally, the capital letters were stored in a separate shallow tray or "case" that was located above the case that held the small letters.

Info from https://en.wikipedia.org/wiki/Letter_case

Original image from https://upload.wikimedia.org/wikipedia/commons/1/1a/Upper_case_and_lower_case_types.jpg

söndag 4 november 2018

Just finished Fran Bow, a game from Killmonday Games in Hedemora

After reading in the local paper that there is a computer game studio, Killmonday Games, in Hedemora now, (probably the very first, with a released game anyway), I felt that I had to try the game they'd produced. I found their game Fran Bow on Steam, it is a point and click adventure game with a really dark, bloody and insane feeling to it. I think you understand by looking at the trailer.

Luckily I like point and click games, so now, after a total of 10 hours of playing according to Steam I've finished it.

I liked the varied puzzles and that you only got a handful of items in the inventory and that the different places visited were pretty restricted. During the last year I've been replaying the first episodes of Monkey Island with my daughter and many times got rather frustrated over how many things Guybrush carries around and how many places he can go to. It leads to too much trial and error and that's avoided in Fran Bow!

I also liked that you could double click when walking to the next scene, which instantly takes you there.
What I disliked though, was the sluggish conversations, which also could be sped up some by well-timed double clicks though, but led to lots of clicking during conversations and it was hard to get the timing right and you still have to wait a while before the next text showed up.

I've got the impression that the game has a big fan base. I didn't become a fan, but found it playable and wish Killmonday Games good luck with their upcoming games!

Are the line numbers missing in the call stack of your logged exceptions?

Sometimes when being on a bug hunt it can be a great help to read the call stack of a logged exception and to be able to see which line the exception was thrown from.

So, if your logs lacks line numbers like this one below, you don't get that extra help...

System.NullReferenceException: Object reference not set to an instance of an object.
   at LineNumberDemo.Program.ThirdMethod(Int32 value)
   at LineNumberDemo.Program.SecondMethod(Int32 value)
   at LineNumberDemo.Program.FirstMethod(Int32 value)
   at LineNumberDemo.Program.Main(String[] args)

To fix that you have to change the Debugging information setting in the project's build settings. 
- Open the Properties for your project. 
- Click Build > Advanced and change "Debugging information" from "None" to "Pdb-only".

The pdb (Program database) files contains the infomation needed to map an instruction in your program to the original line in the source file. After changing that setting the log now looks like this:

System.NullReferenceException: Object reference not set to an instance of an object.
   at LineNumberDemo.Program.ThirdMethod(Int32 value) in C:\Code\LineNumberDemo\LineNumberDemo\Program.cs:line 42
   at LineNumberDemo.Program.SecondMethod(Int32 value) in C:\Code\LineNumberDemo\LineNumberDemo\Program.cs:line 35
   at LineNumberDemo.Program.FirstMethod(Int32 value) in C:\Code\LineNumberDemo\LineNumberDemo\Program.cs:line 29
   at LineNumberDemo.Program.Main(String[] args) in C:\Code\LineNumberDemo\LineNumberDemo\Program.cs:line 16

In Visual Studio 2017, "Pdb-only" is the default setting for the Release configuration, but I'm not sure it always has been, because I've seen lots of projects that did not include the pdb-files. They make the release bigger, but as long as that isn't a problem, I don't see any reason to not include them.

Good bug hunting!

lördag 27 oktober 2018

Kul respons på min kommentar till mjukvaruutvecklar-podcasten Väg 74

Efter att ha lyssnat på några avsnitt av podcasten Väg 74 och det i mina öron många gånger låtit som att jag lyssnat på en skiva med Trazan och Banarne så skrev jag nedanstående tweet.

Nästan ett halvår senare lyssnar jag på avsnitt 83 "Undvik projekt", som har ett intro (lyssna här) baserat på tweeten, kul! :)

Just det avsnittet tyckte jag inte var så givande för mig annars, men är du sugen på att börja lyssna så kan jag rekommendera t ex avsnitt 17 "Vrid upp samarbetet till max med Mobprogrammering" eller avsnitt 15. "Tänk på test".

Och för er som växt upp utan Trazan och Banarne så finns de tydligen både på SVT:s Öppet Arkiv och Youtube såklart!

torsdag 25 oktober 2018

Open PowerShell (or Command Prompt) in Admin mode in the current folder from File Explorer

Sometimes it can be handy to open a PowerShell (or Command Prompt) from File Explorer and directly end up in the same folder that you had open in File Explorer.

These two instructions might be exclusive for Windows 10 only, I'm not sure.

Non-admin mode
Go to the address bar, type cmd and press enter.

Admin mode
For PowerShell, press alt, then f, then s then a. Alt, f, s, a
For Command Prompt, alt, f, m, a

1. Pressing Alt will bring up this menu with hotkeys

2. Pressing f will present the next menu with a new set of hotkeys

3. And pressing opens the last menu

4. Tada: PowerShell i admin mode in the correct folder

torsdag 4 oktober 2018

Do you use big enough color distance?

Read the texts in the image below.

Which text do you find easiest to read?

Probably the one with the largest color distance between the text and the background color. That is the one to the right.

How can you make sure you use big enough color distance so that even the audience in the far back can read the text in your presentation?

One way is to use Mark Millers Highlight Explorer app where you can experiment with different colors for backgorund, foreground and highlighted text.

https://github.com/MillerMark/HighlightExplorer (it's just the source code so you need Visual Studio to run it though...)

If you got money to spend and want to know more about it you can take his course The Science of Great UI

måndag 9 april 2018

Do your ajax calls block each other so they don't run in parallell? Setting the right SessionStateBehavior might fix it!

Today I tried to optimize an asp.net mvc page. The page did three ajax calls, two of them were fast and one was slow. The slow one executed first and blocked the other two so they couldn't execute and return with their data until the slow one had finished.

I was surprised by this since the ajax calls really should be asynchronous. The problem turned out to be the use of session. When session is passed with the ajax call, the call blocks other calls that passes the same session. Because if the first call alters the session it should have an impact on the other calls so they have to wait.

The solution to this was to add this attribute on the controller.

When the controller is marked with that attribute asp.net knows that the code in the controller can't alter the session state so the other calls don't have to wait for it.

Here's the blog post that got us on the right track.

söndag 11 mars 2018

Multi-cursor editing in Visual Studio Code, edit many places simultaneously!

Did some pair programming with @nevva and saw him do some multi-cursor editing in Visual Studio Code I hadn't seen before. So here's three different ways to edit your text files at multiple places simultaneously.

Use Esc to exit multi-cursor mode.

1. Ctrl + Alt + arrow down (or arrow up)
A simpler variant of this in the classic Visual Studio is Shift + Alt + any arrow

2. Alt + left mouse click

3. Mark some text => all occurrences gets highlighted
Ctrl + Shift + L => every occurrence gets its own cursor

söndag 28 januari 2018

Two subtle Linq-related bugs that ReSharper highlighted during code review

ReSharper, a productivity tool that helps you automate some of the code writing and also analyses the code written. The biggest drawback I find with it is that it makes the Visual Studio startup a lot slower. Yes, that is annoying, but I still value the code quality help it gives me more than a fast startup.

This is two examples of Linq-related bugs that ReSharper pointed out for me when I was reviewing code. I hope I would have found the bugs anyway, but Resharper made it a no-brainer to find them and almost impossible to miss.
The examples do not show the original code, only the same problems.
The code writers did not use ReSharper.

Example 1

This is example 1, without the help of ReSharper, do you see the problem?

This is the same code, but with ReSharper active, the problem is shown more clearly, isn't it?

The gray code in the if-block tells us that the code isn't used. Ok, so why isn't it used? Hoover over the blue squiggly-lined code and you will see:

Expression is always false. This hopefully makes a programmer with little experience in Linq to do some research to find out why. And then change the if-condition
projects == null 
projects.Count == 0 

Because Linq's ToList() does never return null, if no rows are matched an empty list is returned.

Example 2

This is a similar problem, do you see what the problem is this time?

With ReSharper activated it looks like this again, but not for the same reason.

The variable projects is not the resulting list of the query. It IS the query. A query that hasn't been executed. And therefore, since it is a query (or an IQueryable object) that is assigned to a value during creation, it cannot be null when the execution of the code reaches the if-condition.


These two bugs can to a code writer or reviewer be rather subtle, but with the help of ReSharper they are not so subtle. Subtle bugs can easily go under the review radar and if the code isn't tested thoroughly it can even reach production. And bugs having come that far are often more cumbersome/expensive to fix than if they had been caught earlier in the process.
That's why I find ReSharper worth its price, both when it comes to money and performance.

torsdag 25 januari 2018

The Visual Example I Always Search for Before Doing a Feature Branch Rebase

Now and then we discover that we've branched out a feature branch off of the wrong branch. To get the workflow correct, we need to move the feature branch so it branches off of the correct branch, a so called feature branch rebase. 

This happens so seldom that I always have forgotten the command for how to do it, so I google it and hope for finding the same example that I found useful the last time. The example isn't hard to find, but this post is for making sure I don't lose it.

The example is copied from the section More Interesting Rebases at https://git-scm.com/book/en/v2/Git-Branching-Rebasing

tisdag 16 januari 2018

C# Linq Equivalents in TypeScript

I have been looking for something like the C# Linq functions in TypeScript, but didn't have any luck. Today a colleague tipped about this site, where Linqs IEnumerable<T> extension methods are listed together with the methods TypeScript equivalent.

A few examples to give you an idea of what it can look like.