schlitt.info - php, photography and private stuff ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :Author: Tobias Schlitt :Date: Wed, 19 Nov 2008 23:29:46 +0100 :Revision: 1 :Copyright: CC by-nc-sa ============= Nice PHP code ============= :Description: At our weekly usergroup meeting, which is taking place just right now, we again discussed our beloved topic, the Ternary Operator. I like that thing really much and I believe it makes code much more readable in a lot of cases. Anyway, I tend to stack ternary operators and I didn't even know, that the PHP manual recommends to not do that. I think this recommendation is only valid of you don't add appropriate braces, to indicate the precendences. Anyway, while having a beer, we thought about some more "funny" things you could do with it... For example, executing mutliple statements, within one section of the operator... here is the result: At our weekly usergroup meeting, which is taking place just right now, we again discussed our beloved topic, the Ternary Operator. I like that thing really much and I believe it makes code much more readable in a lot of cases. Anyway, I tend to stack ternary operators and I didn't even know, that the PHP manual recommends to not do that. I think this recommendation is only valid of you don't add appropriate braces, to indicate the precendences. Anyway, while having a beer, we thought about some more "funny" things you could do with it... For example, executing mutliple statements, within one section of the operator... here is the result: :: echo ( ( 1 == 1 ) ? call_user_func( create_function( '$a, $b', 'return ($a * 2) + $b;' ), 10, 15 ) : 0 ); **Update**: Some more funny constructs after some more beer. I simply love lambda calculus. :) :: call_user_func(($func = create_function('$func, $x', 'echo $x; return ($x < 10 ? $func($func, $x+1) : $x);')), $func, 1); .. Local Variables: mode: rst fill-column: 79 End: vim: et syn=rst tw=79 Trackbacks ========== Comments ======== - Derick at Thu, 01 Feb 2007 20:28:44 +0100 Don't you ever dare to commit this sort of code though! :D - Richard Thomas at Thu, 01 Feb 2007 20:46:23 +0100 Hate them with a passion, yes they are easy to understand if you don't go stack crazy but they make scanning through code harder. - Ivo Jansch at Thu, 01 Feb 2007 21:22:54 +0100 When used properly, it can give you pretty clean code. Sometimes I encounter them in the most complex situations, in which they only cause confusion. - Thomas Koch at Thu, 01 Feb 2007 21:35:52 +0100 Somebody on #php.de showed me another nice application. I'm only not sure, if it saves you any time. In Python you can give a default value when assigning a the value to a variable. That's usefull for example, when a you want to give a default if a function returns null or false. In PHP write: $var = ($var = functionCall()) ? $var : 'no return from function!'; - Toby at Thu, 01 Feb 2007 21:38:07 +0100 You should read internals and search for ifsetor operator. - Ivo Jansch at Thu, 01 Feb 2007 22:25:30 +0100 It would be cool if this worked: $var = $_REQUEST["somevar"] || "default"; instead of 'ifsetor'. But this is syntactically impossible. - Armand at Fri, 02 Feb 2007 10:17:20 +0100 "...I believe it makes code much more readable..." Are you joking? It's horrible! - Toby at Fri, 02 Feb 2007 10:50:14 +0100 Please read exactly, what I wrote. The code shown here is absolutly unreadably, that is true. But there are cases, where the ternary operator makes code more reabale that nested ifs, I think. - Armand at Fri, 02 Feb 2007 11:11:43 +0100 I cant remember any situation in my programming practice where such a style was helpful. I consequently reject such a style in my projects. All what i can say is: If you dont use it you havent to break with such a habit, which is painful. - Ivo Jansch at Fri, 02 Feb 2007 11:18:50 +0100 I remember that in the past, there were some Code Poetry contests in the perl community for writing the most beautifully constructed code. Maybe we should do something similar sometime for PHP :) - Tobias Struckmeier at Fri, 02 Feb 2007 17:01:00 +0100 Imageine code like this: $myvar = $http->hasVariable( 'foobar' ) ? $http->variable( 'foobar' ) : 'defaultValue'; This is more readable as for example: $myvar = 'defaultValue'; if( $http->hasVariable( 'foobar' ) ) { $myvar = $http->variable( 'foobar' ); } Because in one line you see there is an assignment only when a value exists. Otherwise you have to read the whole if statement. But that is only one example. - Armand at Fri, 02 Feb 2007 18:29:22 +0100 what about encapsulation? some thing like this: class test{ ... ... function hasVariable( $var_name ){ if( $this->http->hasVariable( $var_name ) ){ return $this->http->variable( $var_name ); } else{ return 'defaultValue'; } } } I think it's more maintainable and readable. (ok it depends on what you understand under readable) - Armand at Fri, 02 Feb 2007 19:14:09 +0100 from my assembler time. Most of the related web pages still exists with beautiful masterpieces. ..., 65-byte Life Simulator,... http://www.df.lth.se/~john_e/gems.html --- code poetry . - Jan at Mon, 19 Feb 2007 18:02:27 +0100 What makes your if-statements unreadable are the insane bracketing rules you seem to force upon yourself. Try bracketing and indenting pragmatically instead of dogmatically. Compare
if ($cond) { $a = 1; } else { $a = 2; };to
if ($cond) $a = 1; else $a = 2;Unfortunately my point is kinda hard to recognize here because your comments don't support the pre- HTML Tag