Original address: An advent of unconditional quality code
This blog is entry invites you to a experiment in code. It's a experiment that runs four weeks and can is performed secretly at even your. It might improve the way you are about conditional statements in an object oriented programming. You don ' t need any special hardware or setup, just the'll to change your coding style a bit each week.
The experiment
Beginning with this year's Advent (a season of the Christian religion), you are asked to omit one type of conditional stat Ement each week while programming your regular code. The omitted statements add up, so this is you have to spare four different statements in the week before. There is no relation to Christmas (or religion) other than it's a four week period at the "End of", which is the Rfect timeframe for the experiment. And your might buy yourself a little present for Christmas if you succeeded at the experiment (idea:a new programming book ).
The four stages
For every stage, your are asked to write your normal code without a specific statement. It is perfectly valid to use semantically equivalent code constructs to achieve the same goal. This experiment was even more successful if you are creative and diversified in your of the variations original. Remember that's the stages add up. On the fourth stage, your are asked to use none of the statements mentioned. Stage 1 (week): don ' t use "Else" Stage 2 (second week): don ' t use the conditional operator "?:" St Age 3 (third week): don ' t use "switch" Stage 4 (Fourth week): don ' t use ' if '
You are don't asked to change existing code to conform to this restrictions, except you need to work on the lines of that cont Ain the prohibited statements. You are should apply the rules to your new code rigorously, though.
Explanation of Stage 1 (Don ' t use ' else ')
This rule bans the different occurrences of the else-branch to your if-statements. The It includes every "else if" or "elsif" your programming language might. The rationale behind the rule can is found in the Object calisthenics and rule #2 by Jeff Bay. This is a explanation of it by being cellfish.
Explanation of Stage 2 (Don ' t use the conditional operator "?:")
Elvis is dead. Let this resemblance to the hairdo rest for a week, too. It contains a hidden else statement that is restricted since stage 1. Another rationale is this conditional operator isn ' t very easy to read/grasp if stretched out a long line.
Explanation of Stage 3 (Don ' t use "switch")
A switch (or case, or select) statement is no but a big if-else cascade. It ' s handy sometimes, but can be replaced by a lookup table (like a hashmap) virtually everytime. In Martin Fowler's book "Refactoring", the switch statement counts as its own code smell category. You are should try to live without it for a week. If you are need inspiration, try this article in how to avoid it.
Explanation of Stage 4 (Don ' t use "if")
Yes, you didn ' t misread. There is a whole campaign this tries to avoid the if-statement altogether. Read their website for inspiration on and to survive this week. Maybe you might make new friends with polymorphism and some the other implicit conditional. Remember, this is a short week just before Christmas. Try it, you might is surprised how easy it looks with hindsight.
Ready, Steady, go!
This is experiment starts with the the ' The ' Advent at Sunday, 28.11.2010. Every stage lasts for one week and adds up to the previous stages. The experiment ends at Christmas.
Good luck! And if you do with it, drop us a comment with your experiences.