Avoid side effects outside of your application

You probably heard that side effects in programming can be harmful. This post is not about imperative versus declarative approach, though. This is about side effects you may create without paying attention to little details. Just few examples.

Running process and blinking

Let’s take this code in C# on Windows (it can be reproduced in any language, probably on other systems as well):

Can you guess what it does? Apart from not doing anything useful — it just starts new shell and makes it print 123 — it also creates new window which disappears “immediately”. However, the user will notice it and after some time it may become very irritating. Start your new processes in background unless explicitly asked not to by the user.

Bell tone

Again, C# example which you can translate to other platforms:

Do you know what it does? It prints ASCII character 7 which you can also write as \a. This is bell character which makes the operating system to make short “jingle” sound. It is especially irritating because it’s not clearly visible in Mixer, hence cannot be tracked down and user may have no idea which application makes the sound. So whenever you print something out to the console, just get rid of the character:

Temp files

Creating temporary files seems harmless but you should clean them up anyway. Not all users know how to remove them, they pollute the drive, not to mention that they can leak some information. Depending on your platform, you may use deleteOnExit in Java or something based on HKLM\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations registry in Windows.

Resource leak

If you open a handle, always remember about clearing it properly in runtime. Do not wait till the very end because you may cause significant pressure on the system. For instance, creating multiple GDI handles resulted in “breaking” UI which couldn’t be fixed by killing the application, user had to relogin.

Global named locks

Whenever you create a system-wide named lock, make sure that the name is unique. Make sure! Otherwise you end up with story like this. In case it disappears — there was a snippet on Stack Overflow showing how to get GUID of running assembly. The snippet had a bug and it didn’t generate correct value. Two applications decided to use this snippet and so they were using the same name for the mutex.

Focus stealing

Think twice before you show new window to the user. Focus stealing may be very irritating so instead of showing dialog, go with some balloon and blinking on the task bar.