TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Indentation style of an array (http://www.talkphp.com/general/1878-indentation-style-array.html)

Wildhoney 01-06-2008 04:53 PM

Indentation style of an array
 
I am merely wondering as to how people indent their arrays. For example, when you have an array with some pretty substantial lines, putting it all on one line isn't usually the greatest style in the world because you then get stupidly long lines.

I used to format my arrays like the following:

php Code:
$this->m_aAccounts[$szUsername] = array (
                                            'username' => $szUsername,
                                            'password' => $szPassword,
                                            'level_name' => $pLevel->getLevelName(),
                                            'level_flags' => $pLevel->getLevelFlags()
                                        );

But even that is wasting so much white-space to the left of each item. Therefore recently I've been using the following styling method which I feel is far better:

php Code:
$this->m_aAccounts[$szUsername] = array
(
    'username' => $szUsername,
    'password' => $szPassword,
    'level_name' => $pLevel->getLevelName(),
    'level_flags' => $pLevel->getLevelFlags()
);

This is taken from the way I format my Javascript dynamic functions, like so:

javascript Code:
$('myElement').onclick = function()
{
    // Do something fancy.
}

How do you go about indenting your arrays? I'm curious :-)

Karl 01-06-2008 04:57 PM

I too followed the first method for some time, but also changed to the latter for the exact same reason. However, now I've updated my monitor it doesn't really bother me either way.

The only downside to the second method is that the blocks kind of look like functions at a glance, but that is really a very minor hindrance. There is another alternative:

PHP Code:

$this->m_aAccounts[$szUsername] = array(
    
'username' => $szUsername,
    
'password' => $szPassword,
    
'level_name' => $pLevel->getLevelName(),
    
'level_flags' => $pLevel->getLevelFlags()); 

It's pretty much the same as your second method but without placing the opening and closing brackets on their own line. I think that removes the relation to the function and still provides a good solution. Thoughts?

xenon 01-06-2008 05:09 PM

I'm going with this style (extras from a piece of code of mine, I'm too lazy to come up with something right now :D):

PHP Code:

$sql->select( array( 'what' => 'p.project_name',
                     
'from' => 'projects p',
                     
'join' => array( array( 'type' => 'left',
                                             
'table' => 'ratings r',
                                             
'on' => 'r.project_id=p.project_id' ) ),
                     
'groupby' => 'p.project_id',
                     
'orderby' => 'p.project_name ASC',
                     
'where' => 'r.rating<>"NULL"' ) ); 

PS: what does "sz" in front of your vars stand for? I know a is for array, o for object and so on, but sz? - string perhaps?

Wildhoney 01-06-2008 05:58 PM

Yep. Technically a zero terminated string which doesn't apply in PHP. But to me it looks a lot nicer than $sVariable.

short 01-06-2008 06:40 PM

PHP Code:

$sql->select
    array( 
        
'what'    => 'p.project_name',
        
'from'    => 'projects p',
        
'join'    => array( 
            array( 
                
'type'  => 'left',
                
'table' => 'ratings r',
                
'on'    => 'r.project_id=p.project_id',
            ), 
        ),
        
'groupby' => 'p.project_id',
        
'orderby' => 'p.project_name ASC',
        
'where'   => 'r.rating<>"NULL"' 
    
)
); 


Andrew 01-06-2008 08:28 PM

I do it like what Karl posted. I keep it to the left, however I add one tab, and keep the parentheses for array() on the line with something else, so as to not add too many unneeded lines.

sjaq 01-06-2008 10:25 PM

PHP Code:

Config::set('system', array(
        
'errorController' => 'error',
        
'defaultAction' => 'index',
        
'debug' => true,
        
'rootSafety' => true,
        
'fileCommand' => '/usr/bin/file',
        
'logs' => array(
            
'error' => 'error.log',
            
'default' => 'log.log'
        
),
        
'timezone' => 'Europe/Amsterdam'
)); 


Salathe 01-06-2008 11:01 PM

I generally follow a similar approach to the last post, by sjaq, except that indent depths are always the same (one tab; don't hate me for using tabs) whereas the initial indent is double the length in the code posted above.

I know the following is not strictly about indentation, but I feel it applies here. I like to keep all of the 'key value' (=>) operators nicely lined up as well (using spaces as padding).

All in all, it's just personal preference and one that I've adopted over time.

PHP Code:

// If I were feeling really pernickety,
// I'd order the keys alphabetically ;-)
$aSample = array (
    
'errorController' => 'error',
    
'defaultAction'   => 'index',
    
'debug'           => true,
    
'rootSafety'      => true,
    
'fileCommand'     => 'usr/bin/file',
    
'logs'            => array (
        
'error'       => 'error.log',
        
'default'     => 'log.log'
    
),
    
'timezone'        => 'Europe/Amsterdam'
); 



All times are GMT. The time now is 07:59 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0