Home Assistant

After taking the plunge into home automation, I knew it was only a matter of time before trying out Home Assistant. Having set up quite a few routines using Google Home, I started to notice the limitations and small annoyances that could easily be solved by something more flexible.

So yesterday I got too it: I borrowed a Raspberry Pi 3 (Buying a Raspberry Pi 4 is almost impossible at the moment due to the chip shortage), watched some tutorials on YouTube, and installed Home Assistant!

And I am impressed!

Home Assistant is not only very intuitive to use, it also had no problem detecting all my smart home devices, including a few I didn’t even know I had!

I am probably lucky for waiting a while with this whole smart home thing, but this software is mature! Looking forward to see what else it has to offer.

Smart Home

Being the tech-geek I am, it might actually surprise you that I did not start with any home automation until end of last year. Most of my friends were early adopters, but it never really seemed necessary to me. It was only last year when I attempted dipping my toe in the smart home “pool”, and oh boy, dip I did. What started with borrowing some old Philips HUE bulbs from a friend, quickly became a first purchase of my own.

What happened exactly between a couple of months ago and now, is still unclear. All I know is that when someone rings the doorbell, speakers announce it to me. When evening comes, lights eliminate the living room as my curtain automatically closes. And when I wake up, music starts playing by itself. I might be a late bloomer in the smart home ecosystem, but boy am I a fan 🙂

Language differences in unexpected places

At the moment, I am mainly using two programming languages: C# during work and Java for my hobby project (http://strategicrps.com/..Yes, I am still working on it 😉 ).

Because of the many similarities between these two languages (at least in syntax), it is quite easy to switch from one to the other. However, it is also because of these similarities that it is sometimes easy to forget that they actually are different languages using different frameworks.

Recently, I came across one of these instances, and I thought it would be nice to share.

As an introduction, I want to ask you the following question: In the two code snippets below. What will be the values of “exists1” and “exists2”?

C#

List<int> intList = new List<int>();
Dictionary<object, int> dictionary = new Dictionary<object, int>();

dictionary.Add(intList, 7);

bool exists1 = dictionary.ContainsKey(intList);

intList.Add(3);

bool exists2 = dictionary.ContainsKey(intList);

Java

List<Integer> intList = new ArrayList<Integer>();
HashMap<Object, Integer> hashMap = new HashMap<Object, Integer>();

hashMap.put(intList, 7);

boolean exists1 = hashMap.containsKey(intList);

intList.add(3);

boolean exists2 = hashMap.containsKey(intList);

Even though the snippets are very similar (ArrayList and HashMap are the Java equivalents to List and Dictionary in C#.NET), the answers actually differ. In the C# code, both booleans will be “true”, while in the Java code, the second value will be “false”.

Both HashMap and Dictionary first use the object hash method (hashCode() in Java and GetHashCode() in C#) for mapping, followed by the equals method (equals(Object) in Java and Equals(object) in C#) in case of collisions. This is where the difference occurs: whereas the ArrayList in java implements its own hash method based on its content, the List object in C# does not. As we change the contents of the ArrayList in the example above, the original hash value used as the key for the HashMap, is changed, and the list cannot be found anymore. In case of the List in C#, the hash implementation of the object class is being used, which is related to the memory location of the object.

As I have the most experience with C#, I did not expect the hash code of the ArrayList to change when changing the contents of the list. That being said, if I actually think about it, the Java implementation actually makes more sense to me; The hash code should change with the state of a class. My experience with the way C# works simply conditioned me not to give this a second thought.

Ironically, if you have a look at the source code of the object class in C#, it has the following comment:

// GetHashCode is intended to serve as a hash function for this object.
// Based on the contents of the object, the hash function will return a suitable
// value with a relatively random distribution over the various inputs.
//
// The default implementation returns the sync block index for this instance.
// Calling it on the same object multiple times will return the same value, so
// it will technically meet the needs of a hash function, but it's less than ideal.
// Objects (& especially value classes) should override this method.

If I were to interpret this comment, I would actually expect that a class like List should have overwritten the GetHashCode method, just like Java does.

Before wrapping up, I feel like I have to add that I do realize you should never use mutable objects as keys, nor in C# or in Java. The case I came across was very different from this one, and I merely created this case as an example.

Even though this is probably not something you will ever encounter (if you do, there is a big chance that something else is wrong with your implementation), I still thought it was an interesting subject to write about. It is not only about the underlying technicals. It is also a reminder of the things we can encounter when we start to think we understand it all.

Whatever your personal take is on this subject, I hope it was at least an interesting read.

Moved to a new home

It has certainly been a while since I last updated my blog (luckily I can secretly add some backdated posts to get back on track), but it has been quite a hectic period recently.

Among other things, I have been moving to a different apartment, which is taxing enough. Coincidentally, the apartment came available during a period I had already planned a vacation to Morocco. In the end, I was able to combine the two, but it certainly was a stressful period.

Of course moving always requires a lot of work to be done on the new home, which was the reason I had less time for writing and coding in my free time.

Now I am happy to report that I am all settled and ready to get back to work on my hobbies. I am already hard at work on writing a new article for Mind Dough and am also working on a new version for strategic RPS. Stay tuned!