May 262017

Quite often in singleton classes you might find yourself doing the standard plumbing code like this:

private static MyClass _current = new MyClass();

public static MyClass Current {
    get { return _current; }

or a bit simpler with C# 7:
public static MyClass Current = new MyClass();

If class is a bit heavier (e.g. using lots of memory) you might do it lazily with a lock:

private static MyClass _current;

public static MyClass Current {
    get {
        lock(SyncRoot) {
            if (_current == null) { _current = new MyClass(); }
            return _current;

What almost never gets any love is Lazy construct known from .NET Framework 4:

private static Lazy<MyClass> _current = new Lazy<MyClass>();

public static MyClass Current {
    get { return _current.Value; }

This will initialize class on the first access, it is tread safe by default, and you can even use custom initializer if needed, e.g.:

private static Lazy<MyClass> _current = new Lazy<MyClass>(() => new MyClass(42));

public static MyClass Current {
    get { return _current.Value; }

Exactly what doctor prescribed. :)

PS: And stop using double-check locking.

May 212017

For a project I am doing with kids we decided to use 3-digit 7-segment display. As we intended to video the project (eventually), it was easy to make decision that we’re gonna drive every digit fully – no multiplexing. Not only that simplifies programming logic but it also makes display rock steady during video – no flickering here.

However, with so many pins to drive, I got a bit scared about maximum power consumption MCU would need to deal with. Assuming we go with 10 mA per LED, what is actually the maximum current we’ll need?

The easy answer is 7×10 = 70 mA per digit, bringing it to 210 mA total. Assuming all digits are fully on (we haven’t used decimal point – otherwise it would be 8) we would indeed need that much current. However, realistically, are we always going to have number 8 on display?

For this purpose, we’ve gone and created a table – how many segments we actually use for each digit:

Digit Segments Current
0 6 60 mA
1 2 20 mA
2 5 50 mA
3 5 50 mA
4 4 40 mA
5 5 50 mA
6 6 60 mA
7 3 30 mA
8 7 70 mA
9 6 60 mA
Median 5 50 mA

As you can see, the most common result ended up 5 segments being lighted. This tells we actually can usually count on 150 mA of precious current needed.

But wait, since our project will count from 0 to 255, we actually won’t have all our digits on all of the time. We need to calculate how many segments we actually have lighted for numbers that we need. An eternity in Excel later we’ve found that median current needed (assuming random distribution) was 120 mA. Maximum was 180 mA (darn 208!) and minimum was 20 mA.

While 180 mA is not trivial, it is definitely less than 210 mA we originally had to count.

May 162017

With Visual Studio 2017 out and C# 7 in the wild we got a couple of new features. After using them for a while, here are my favorites.

Out Variables
The change here is subtle and many would call it inconsequential but I love. Out variables can finally be declared inline instead of pre-declaring them above parsing statement. This small change makes code flow much smoother and, in my opinion, makes all the difference.
For example, note the difference between these two code fragments:

int count;
if (int.TryParse(text, out count) && (count > 1)) {
    //do something with count

if (int.TryParse(text, out var count) && (count > 1)) {
    //do something with count

For rare cases when you don’t care about result you can also use underscore (_) character:

if (int.TryParse(text, out var _)) {
    //do something

Throw Expressions
If you are creating classes for public consumption, quite often a big part of your code is boilerplate argument verification and throwing of exceptions. You can now merge usage of that argument with null coalescing or ternary operators. Again, nothing huge but makes code a bit more to the point:

if (text == null) { throw new ArgumentNullException(nameof(text), "Text cannot be null."); }
this.Text = text;

if (number >= 2) { thrown new ArgumentOutOfRange(nameof(number), "Number must be 2 or more."); }
this.Number = number;

this.Text = text ?? throw new ArgumentNullException(nameof(text), "Value cannot be null.");
this.Number = (number >= 2) ? number : throw new ArgumentOutOfRange(nameof(number), "Number must be 2 or more.");

Is Expressions
Another change that seems small but it really improves code is just another style of as operator and it will probably cause its demise. It is a syntax candy allowing for inline variable declaration and a bit more readable code:

var comboBox = sender as ComboBox;
if (comboBox != null) {
    //do something with comboBox

if (sender is ComboBox comboBox) {
    /do something with comboBox

Local Functions
And lastly, there are local functions. Again, these are nothing you couldn’t do before. I don’t think they are going to be used often most of the time. However, when you parse stuff or deal with state machines, they do come in handy as they do capture local variables.

Wish list

The only thing I still desire in C# as compared to other languages are enum methods. It is not a big thing but it would remove a whole set of helper methods and similar plumbing.

May 112017

For this version probably the most notable change is copy/paste support. I know, how the heck can application in 2017 not support copy/paste in the first place. I can definitely think of an excuse or two but reality is that it was an inexcusable omission. Similar in the “revolutionary concepts”, finally a right-click menu is available so you don’t need to open an entry just to copy a password.

A lot of work has been done around textual passwords. As compared to the previous version, word count has gone down (27261 before vs 17701 now) but words that remained should be a bit easier to remember. Considering the default recommendation of 5 words per password is still giving you about 1 millennium in the worst case, I would say it is worth it. And don’t forget that estimate is conservative as heck: it assumes everybody knows all words you’ve used and that attacker can do 100 trillion guesses per second.

I also took liberty in removing the words found in the most common leaked passwords as well as showing the warning to user if he uses one. You can remove that warning in the Options at your own peril. :)

Filtering of entries has been improved, especially for the PasswordSafe imported categories. Additionally one can simply write * to show all elements and that should help the searches as well. Likewise Auto-type form got a bit of cleanup. Basic auto-type didn’t change but fields got Tab and Enter suffix each. It makes filling out information on forms much easier without having to predefine exact format.

A separate form for password history and other details has been added. While it doesn’t show anything that you couldn’t see in the old historical password combo box, different presentation does wonders for clarity.

Two noticeable changes happened on the setup side. The first one is change to .NET Framework 4 as .NET 2.0 simply became way too restrictive. I still didn’t want to leave Windows XP users stranded so this was as far as I was willing to go. You will also notice that most of settings are not stored in registry any more but in textual file. This should make those using Bimil without installing it much happier. :)

A few additional minor changes and bug-fixes were made but I will leave it for you to discover. As always, you can download new version from Bimil pages or upgrade from within application.