Algorithms in the Real World

Quicksort algorithm animated
Image by en:User:RolandH
If you are here, I suppose you're familiar with algorithms. Probably your job consists in creating them. What you should know is that every sequence of operations that we accomplish is an algorithm.

What students learn in Computer Science classes is to create generic algorithms (such as the quicksort graphically explained in the image on the left). Then, when they start to work, new specific algorithms must be created.

Nowadays, especially with the advent of the so called Internet of Things, special purpose algorithms are everywhere -  and the control we have on them is very limited. Take a look to the following TED talk:



Surprised about the "Flash Crash of 2:45"? Well, if at Wall Street a software is able to make decisions based on few data, an algorithm that analyzes big data can surely vote in a board of directors, right?

Details and Respect Make the Difference (for Me)

How many weather apps there are in the application store of your smartphone? And how many virtual keyboards? And browsers? There are millions of clones out there but you are using that particular app and you don't want to change it. Why?

[ Laziness may be an answer, especially if you are a developer like me ;-) ]

Jokes aside, often I like to see if there are alternatives to the apps I use everyday. Sometimes I find good things but most of the time I keep using my old apps. So, which are the reasons for (not) changing? For me, mainly two: details and respect.

Usually the main feature is something that works pretty well in all clones. Any feed reader is supposed to let you read your favorite blogs. But only few let you have a customizable sharing button. It's a very small feature, a simple detail, but to me it's absolutely convenient.

Honesty. Trust. Respect. Love.
Good rule to follow in business and in life.
Image by Zaneology
But before trying a new app and evaluate its details, I check its permissions. I consider them a form of respect. Why do a weather app want to access my contacts? Why do a dictionary need to know to my position?

I know the answer: nothing is free. But this is a wrong answer. It would be different if the developer tells me: I need to access your GPS data in order to propose custom ads and keep the app free.

Maybe he is just bribing me but al least he don't think I'm so stupid to not understand that the permissions the app is aking for are not related to its features.

Self-limited Development

I'm sure you have already heard of the following proverb:
If all you have is a hammer, everything looks like a nail.
It has come to my mind while reading this Linked in discussion (don't be mislead by the original question), especially those answers claiming "never used goto" or that there is no need to use do-while.

Well, you should know what I think about goto, and I don't want to repeat here the good reasons to use it. What I'm saying is that if a tool has been provided by the creators of the language (and never removed across several revisions) maybe it's not so bad.

Toolbox

Look at the above toolbox: there is also a cutter. It's pretty sharpened and can be dangerous if not used carefully. I bet you'd never use a cutter instead of a screwdriver. Maybe sometimes you can use the scissors instead of the cutter but in some situations it will not be practical and probably the result won't be the same.

Would you avoid using the cutter just because it is potentially dangerous or because someone told you that it's not good? Why the hell are supermarkets still selling cutters if you don't have to use them?!

So, why are you not using some instructions legally provided by the language?

4 Lifeboats for Your Projects

Lifeboat drill, via Wikimedia Commons
I like the metaphor of the lifeboat used in this post written by +Jacopo Romei. But I want to slightly turn away from the original meaning of the post and concentrate on this sentence:
lifeboats don’t add anything up to a nice sunny sailing day
During development, there are several things that you should add and can be considered as "lifeboats".

1. Good Comments and Code Documentation

This should be obvious. It makes you save time and headaches when you need to understand quickly someone else's piece of code (or remind you why you've made that choice five years ago) while fixing a bug, adding a feature or integrating it into another project.

The faster you are in modifying your application, the happier your customer will be.

2. Add Checks for "Unexpected" Situations

We are so used to develop for modern PCs that checking for hardware limitations (out of memory, disk full, etc.) has become a far remind. But in these years a brand new class of devices with limited resources appeared: smartphones and tablets. How many time have you seen apps crashing without any notification?

But even a super-duper PC can run out memory, for example if a video editing application is running or if there is a browser with 100 loaded tabs. Never take for granted that all  resources are available for your process.

So, try to not forget that RAM and disk are not infinite.

3. Logging System

There's nothing worst than a customer that reports a crush that he's not able to reproduce. Without a logging system you cannot understand what's happened and thus you cannot fix the bug. And what's more important you cannot ensure the customer that it will never happen again.

With a well-created log file, you can reproduce the exact sequence of steps that lead to the issue and (hopefully) find the bug. Moreover, a good log may let you discover weird things that may cause future bugs (for example, a series of managed exceptions indicating that a is file corrupted or reporting a different version of a shared object).

Create meaningful logs and provide an easy way for your customers to send them to you.

4. Unit Tests

You have just added a feature, ended a refactoring session or fixed a small bug: what should you do now? Ensure that you didn't caused some side effect. And the only way is using unit tests to check every sensible piece of your code.

When you have done with these tests, you can sleep quiet.

Conclusions

None of these things will make you sell one more copy of your software, but they will help you in the future, while debugging a specific issue or adding a new function in a short time. They will make you save your time and possibly not lose a customer just because you aren't able to reproduce a bug. They will make you work better and with more confidence and peace of mind.

Tab War

Some years ago I've downloaded the ebook "C++ Manuale di Stile" (C++ Style Manual) written by +Carlo Pescio and in these days I'm reading (again) some of its chapters. One of them is devoted to tabs:
[...] in a big project in which I participated, "the length of the tab" was cause of severe tensions, with threats of layoffs and endless discussions. Even in this case, the basic problem was the lack of a standard accepted by all: some programmers defined the tab as eight spaces, other two, other three. In addition, some used only tabs, others used both tabs and spaces.

[...] it's necessary that an internal convention is established and it's respected by every developer. It's better to resolve disputes at the beginning of a project than being in the middle of a civil war when development is in progress.
From pages 56 and 57 - Translation by me
I've already written about the need of a common coding style but, as ancient Romans used to say, repetita juvant.

Avoid Perfection

So you have this great idea for the next killer app, don't you?

As you begin to write the code, millions of details start to obfuscate your mind. This should be done in this way, that function must be splitted in two, the UI needs some improvement, etc.

As the project goes on, many other ideas on how to make your app better come to your mind. And, for each one, you think that if you don't implement it, your app will be just an average app. So it ends that your application will never be completed and never be available to anyone. And, you know, this is not good for a killer app.

What you have to do is to stop adding features. Does your application cover only 10% of what is in your mind? Don't bother: make sure that 10% is working fine and then publish it. After that you can start working on other features in order to publish the release 1.1 which will implement 15% or maybe 20% of the original idea. And then 1.2 and 2.0 and so on.

In the meantime, you will receive some feedback from the users that will help you to refine your original idea - or to change it dramatically. And this will lead you to write a better application - even if probably not the best.

Because you always have to remember that
Perfect is the enemy of good - Voltaire

Image created with pinstamatic.com

[Solved] OpenGL issue with Xubuntu 14.04 inside VirtualBox

On my workplace, I use Xubuntu in a virtual machine (VirtualBox) in full-screen mode without any problem, at least until last week, when I've upgraded from Saucy Salamander to Trusty Tahr.

At first, everything seemed to work fine, but when I tried to launch Firefox, the whole screen became black and the only way to keep working was switching in windowed mode and then back to full-screen. A similar issue happened with Chromium, with the addition that, when back in full-screen, the browser window was always on top.

I've tried to re-install the guest additions, but without solving the problem. From a quick search on Internet, I've find that this issue was related to OpenGL, but everywhere they recommended to enable 3D acceleration in the virtual machine settings (and in my configuration it was already enabled).

Only this article stands out of from the crowd, suggesting to disable 3D acceleration (even if for solving a different issue). And this is what I did (see image below) and it worked.


Additional Information

Host OS: Windows 7 64 bit
Guest OS: Xubuntu 14.04 32 bit
Virtual Box release: 4.3.10 with Extension Pack

This is the not working OpenGL situation.
$ glxinfo 2>/dev/null | grep OpenGL
OpenGL vendor string: Humper
OpenGL renderer string: Chromium
OpenGL version string: 2.1 Chromium 1.9
OpenGL shading language version string: 4.20
OpenGL extensions:

And this is the situation after disabling 3D acceleration.
$ glxinfo 2>/dev/null | grep OpenGL
libGL error: pci id for fd 4: 80ee:beef, driver (null)
OpenGL Warning: Failed to connect to host. Make sure 3D acceleration is enabled for this VM.
libGL error: core dri or dri2 extension not found
libGL error: failed to load driver: vboxvideo
OpenGL vendor string: VMware, Inc.
OpenGL renderer string: Gallium 0.4 on llvmpipe (LLVM 3.4, 128 bits)
OpenGL version string: 2.1 Mesa 10.1.0
OpenGL shading language version string: 1.30
OpenGL extensions:

Probably if you need to play with some 3D game, this solution will not help you, but for my needs, it's just perfect.