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.
Well, do you really need the else statement? Error stops the code anyway. This leads to a more elaborate approach. You can make a procedure with a signature ifError(int $condition, string $message) and put the if block in the functions making your code even easier to read, you could also not error out and return the answer of the condition thisway you could stack all problems and bail out at one well defined cleanup step. leading to something like:
proc int ifProblem(int $condition, string $message){
warning $message; //we want a chance to clean up
return $condition;
}
and then in your main:
int $problem=0;
$problem=ifProblem((!$condition1),”because of …”);
$problem=ifProblem((!$condition2),”because of …”);
$problem=ifProblem((!$condition3),”because of …”);
if ($problem){
…clean up after yourself and end…
}
oops that contains tought change the return to ! $condition and make the = in the main *= and int $problem=1… But yes im sure you get the picture.
Damn im not thinking forget the last one just change = to +=. And the proc to:
proc int ifProblem(int $condition, string $message){
if($condition)warning $message;
return $condition;
}
Hey Joojaa! Thanks for replying.
I’d say everything is yet better than the tree-thing :]
“error” was an example. Of course I “return” as well in appripiate places.
But I’d hesitate to do an extra proc for this. But to be able to stack the problems is a nice idea… thanks!
Why not put all errors to a string array and if the array is empty go on..
Well you could do that but then you would probably end up doing yet again a ladder.
Theres just not easy way out, if the solution has to be inline and cant tidy up you know. And about extra procs i see what you mean. But since they are global and do tidy things up they do represent something useful. Not to be forwned at.
I mean how many times have you written a piece of code and then suddenly came t the conclusion that you wished you had refactored the code on start.
hehe :] yea so true.