Tuesday, November 26, 2013

Learning Mac: Basic Setup

Background

A few month ago, a friend just gifted me his MacBookPro.
About two years earlier he felt to need a Mac to be cool, but never managed the transition as a decades old Windows user. So he switched back to Windows. And his old MacBookPro now is mine.
Thanks Martin!

To overcome my almost exclusive Windows only experience so far, I decided to use the Mac to dive into node.js development.

This blog post is just a little scratch pad to note down some steps and findings with the basic setup of OSX. Development related stuff will come later. More might follow.

Attention: For an even moderately experienced Mac user most of the things might be trivial. But being an almost Windows-only person so far, I'm constantly finding myself struggling with absolutely basic stuff.

Phase 0 - Six Month Ago

Basic Setup

On my first weekend as a Mac owner, I tried to clean it up from everything left over from the previous owner, update the software, and setup some basic stuff.

Here's what I did (from the top of my head):
  • Created a new user account including getting an AppleId. - Interesting: It felt strange to type apple.com into a browser's address bar. :)
  • Deleted the previous owner's account
  • Got the latest version of OSX
  • Got Dropbox from the AppStore and set it up
  • Set up the pre-installed Microsoft Outlook 2011 (strange version number for a Windows-guy)

Print a Photo from my Camera

That was astonishingly hard.
A few of the challenges along that way:
  • Simply seeing a file from the inserted memory card. Constantly hitting Win+E (which is not there of course) to open a Windows Explorer. Accidentally found the automatically mounted memory card on the Desktop (is it called like this on a Mac?)
  • Installing the correct driver for my photo printer
  • Chosing photo paper instead of normal paper. I remember that I gave up on this one. The result of the print was awful.

Bottom Line

For the first day, I had the feeling that it is incredible hard to work on the Mac. I was constantly googling almost every little command. On windows, I love using keyboard shortcuts all way. And on the Mac it is all different.
I'm sure it will take quite an amount of time until I will feel productive on OSX as well. But I will strive for it and share.

Thursday, August 1, 2013

Dictionary: Indexer Setter Can Add New Item

Did you know that the following test passes instead of failing with an exception?

[TestMethod]
public void WriteAccessToNonExistingKeyAddsTheElement()
{
   var dict = new Dictionary<string, object>();
   dict["NonExistingKey"] = new object();
   Assert.AreEqual(1, dict.Count);
}

What I learned today is, that you can add an item to a dictionary by just assigning it using the indexer setter where I had expected an exception.

Does this mean, that should happily use this indexer syntax to add new elements to a dictionary?
No!

Here's my reasoning:

1. It is semantically wrong

What does a line like this say?

   dict[aKey] = aValue;

It says: Set the value for the item in the dictionary associated to the key aKey. Trying to do this for a key that has not been added to the dictionary at all, does not make sense.

Therefore I would expect an exception.

2. It is inconsistent

A read access to the indexer with a non existing key throws an exception. So it seems inconsistent to get no exception in the write case.

3. It is unexpected

Of course, letting the behaviour of the index setter being as it is, was a valid design decision. But I think it is an absolutely unexpected behaviour. There are explicit Add(...) methods in the Dictionary-class. The name of these methods are absolutely clear. They say that they do. So why not make these the only means to add an item?


Conclusion

To make your code readable, use an Add(...) overload. To change the value of an existing item in the dictionary, use the indexer setter.

Please.