Oh if you are scripting or coding you might come across those “insights”. This is of course to share my thoughts but to have a place to keep and remember this stuff as well. I mean maybe this is completely bollocks!! Comment if you you think so! :]
Ok: fall-throughs:
As I always try to cover as many cases as possible (within reason of course!) I create a lot of masseges to the user. In this style I really became sick of that huge if-else blocks that have the fall-through-message at the end. These tree like creatures:
if ($condition1)
{
// do all this ...
if ($condition2)
{
if ($condition3)
{
// do this too ....
}
else
error "bla because of ...";
}
else if ($condition4)
{
// do other stuff ...
}
else
error "bla bla blub...";
}
else
error "bla bla bla...";
this shows the usual style one would do: But there are major drawbacks: the conditions and the error-messages are at opposite positions. And if you add a condition that might escape the whole thing you’d have to add another “scoping-level”…
Ok, now the insight: I write the escape-relevant conditions in negative style a line in the very front of the script:
if (!$condition1)
error "because of ...";
else if (!$condition2)
error "because of ...";
else if (!$condition3)
error "because of ...";
else if (!$condition4)
error "because of ...";
// now do stuff ...
With this style I have the conditions right at the error messages. The indentation is much less deep. And all the stuff that could crap up the thing is coupled at a definite place.
Ok.. this might sound like kindergarten to a real programmer but. I felt quite smart when doing so ^^
whatever now I wrote it down. So be it.