 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
01-06-2008, 04:53 PM
|
#1 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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 
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
01-06-2008, 04:57 PM
|
#2 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
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?
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
|
|
|
|
01-06-2008, 05:09 PM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
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?
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
01-06-2008, 05:58 PM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Yep. Technically a zero terminated string which doesn't apply in PHP. But to me it looks a lot nicer than $sVariable.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
01-06-2008, 06:40 PM
|
#5 (permalink)
|
|
The Visitor
Join Date: Jan 2008
Posts: 3
Thanks: 0
|
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"'
)
);
|
|
|
|
01-06-2008, 08:28 PM
|
#6 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
|
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.
|
|
|
01-06-2008, 10:25 PM
|
#7 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
|
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'
));
|
|
|
|
01-06-2008, 11:01 PM
|
#8 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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'
);
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|