Tip #6: The importance of the default case
It is said that “is good to catch an exception as soon as possible”. This is the motto of the post.
You never wrote perfect code! Neither do I and probably no mortal will do this. You must accept it and try to make your code as best as possible. There are many ways to do this but today will focus the attention on the ‘default’ case.
How many times you wrote?
select variable
case 1: ...
case 2: ...
...
case n:
end |
I did this a lot of times. Why do we do it? Because we assume we are bullet proof and out code is perfect. One might say that the previous snippet is not wrong but take a closer look. What if variable is not 1 and not 2 and … and not n? What happens?
Software Craftsmanship
Writing code and creating working software is not hard but writing quality code and creating valuable software is.
The software craftsmanship manifesto values:
Not only working software, but also well-crafted software.
Not only responding to change, but also steadily adding value.
Not only individuals and interactions, but also a community of professionals.
Not only customer collaboration, but also productive partnerships.That is, in pursuit of the items on the left we have found the items on the right to be indispensable.
One can hardly doubt that the principles from then manifesto can be satisfied in other way than through passion. Of course, not all individuals involved in the process of software creation do their job with passion and dedication. Some do their job just because they need money or because they have some other interest and they really don’t care about the final product. They create software because they have to and usually is bad software.
Bad software is not software that does not work! Bad software, from my developer point of view, is software that was created chaotic. The project, from the beginning or from another point in the project’s timeline, was not governed by some rules and tenets. The individual involved in the project did not adhere to some standards and everyone was doing anything just to make a piece of working software.
This kind of projects are like pipes with rubber tape. You add more and more rubber tape (bad code/ideas, hacks) where you find a crack and in the end you will be over whelmed by the mess you created. Also, the fixing cost (refactoring) will be enormous.
ATM Interface [Bad Practice]
Today I had one of the most funny and annoying ATM experiences.
First, let me describe the ATM: a big screen with 4 buttons on the left side and 4 on the right side. The numeric keyboard(digits, Enter, Cancel and Backspace buttons) is placed on the right side on some angle so you can type easy.
I inserted the card and I was asked for the PIN code. Well… one of the digits was not really working. I pressed the button so hard that eventually I get two asterisks – this means I’ve typed a digit twice. [Ups!] The backspace or cancel buttons were not working and, on the screen, there was just one option: “Confirm PIN”… Without any other alternative I pressed the “Confirm” button and surprise: I got the screen where you choose the amount of money you want to withdraw [Wow, this is a mind reading ATM - he definitely read the PIN from my brain :) ]. Forgot to mention, the Confirm button is not on the numeric keyboard, it is on the right side of the screen – pretty intuitive.
Now let’s choose the sum… Well the sum contains the digit that’s not working… Pressed it again hard and got two digits [ Déjà vu? ]. The backspace button as not working and the only option from the screen was “Cancel”. So I have to move from the numeric keyboard to the screen side buttons. [ It deleted the whole number I've typed. I just want to delete one digit... Annoying! ]. After a few tries I’ve managed to enter the sum and pressed OK – again this was a screen side button.
Now this is the funny part: I get the receipt but no money. [ Wow! ] On it there was a message “Invalid PIN” [ You must be kidding. You asked me about the sum I want to withdraw and made me enter a number almost impossible to type just to inform me that the PIN - which I have entered previously - is wrong?? Damn ].
All I wanted was to take the card and leave from that stupid ATM. Guess what? The card cannot be taken out of the machine until you enter the correct PIN… So I got back to the PIN screen. Being extremly carreful I’ve managed to enter the PIN – I repeat myself: there was no cancel button and if you enter a wrong PIN 3 times the card will be blocked – and the sum I want.
What more can I say?
Find misplaced comments and quotation marks
Many programming text editors automatically format comments, string literals, and other syntactical elements. In more primitive environments, a misplaced comment or quotation mark can trip up the compiler. To find the extra comment or quotation mark, insert the following sequence intro your code in C, C++, C# or Java.
/*"/**/
This code phrase will terminate either a comment or string, which is useful in narrowing the space in which the unterminated comment or string is hidden.
obj->text = "Hello world; // Some other code delete obj; |
This code will raise an error because you forgot a quotation mark after “world”. Placing the /*”/**/ after that line will close the string literal.
obj->text = "Hello world; // Some other code delete obj; /*"/**/ |
The selected text is now a string literal and you won’t get any compilation error. You have limited the search space for the error.