At 12:26 AM 2/17/2002 +0100, Frank Joerdens wrote: >I am looking for a good manual or introduction on how to write readable, >extensible code (in any programming language). To give you an example, >this bit looks pretty darn horrible and I just don't know how to >rephrase it to make it more concise and readable: > >----------------------------- snip ----------------------------- >if ( ($var>0) & ( (strlen($var)==strlen(intval($var))) or >((strlen($var)-1)==strlen(intval($var))) ) ) { > > do stuff; > >} >----------------------------- snap -----------------------------
This is all just IMHO, because I'm still a bit of a beginner. I have always been tempted to write code in the fewest lines possible. But sometimes this is a Bad Thing, because it can make it very difficult to figure out what you were thinking when you wrote it. I think readability is more important, especially in PHP apps where you are not trying to squeeze out every last bit of performance (don't get me wrong, speed is important, but let's face it, we're not designing low level device drivers or real time kernels here...) Anyway, not really knowing what you are trying to accomplish above, I would rewrite it like so: $strlength = strlen($var); $intlength = strlen(intval($var)); if($strlength == $intlength || $strlength - 1 == $intlength) { $length_ok = true; } if($var > 0 && $length_ok) { do stuff; } Yes, that is 6 more lines of code than your example, but I believe it is much clearer and easier to read. Also, the above is actually self documenting IMHO. If I was going to use your example in my code, I would definitely have to comment it to remind myself of it's purpose later, where my example I believe doesn't require comments as it's easier to read. Don't have any specific book recommendations...maybe someone else will jump in and suggest something... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php