Created
October 29, 2012 19:40
-
-
Save brennen/3976038 to your computer and use it in GitHub Desktop.
a rough breakdown of how i approach php whitespace, for stilldavid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// i used to always indent after the opening php tag, but i've decided that's | |
// kind of silly for non-template stuff. | |
// general stuff: | |
// - 2 space indents, no tab characters. | |
// - i think whitespace contributes to readability, and code that is | |
// crammed into the least space possible often strikes me as harder | |
// to follow. | |
// - i like to align like things vertically. | |
// ----------- try to fit things in 80 chars where practical ------------------^ | |
class Foo { | |
// logically related assignments should be grouped and aligned on the | |
// assignment operator, which should have whitespace around it in most | |
// situations: | |
public $one = 1; | |
public $two = 2; | |
public $three = 3; | |
// non-public variables get underscores in front, because then you know | |
// something about their accessibility at a glance: | |
protected $_spaceship = 'Nostromo'; | |
// array elements should be aligned on the fat arrow, because tabular things | |
// are easier to read at a glance: | |
protected $_dudes = array( | |
'captain' => 'ralph', | |
'commander' => 'bob', | |
); | |
// i often think it's cleaner to indent nested arrays one indentation | |
// level than to make them line up all the way over with the fat arrow, but | |
// all i really care about is that the structure be obvious from the layout. | |
protected $_someNestedArray = array( | |
'one' => array( | |
'la', | |
'la', | |
'la', | |
), | |
// obviously way cleaner for this case - so sue me, i couldn't think of | |
// an example: | |
'two' => array('do', 're', 'mi'), | |
); | |
// modified k&r - i like the space in between function name and () for | |
// definitions. i don't care very much about this in general, but it should | |
// be consistent for every function or method defined in a file. | |
public function __construct ($one, $two) | |
{ | |
// things happen here | |
} | |
// sometimes it seems cleaner to put short, one-statement functions | |
// on a single line, especially if there are a bunch of similar ones. | |
public function getShip () { return $this->_spaceship; } | |
public function dudeManifest () | |
{ | |
$manifest = ''; | |
foreach ($dudes as $role => $name) { | |
// often, i build strings with the concatenation operator vertically | |
// aligned to the assignment, to keep the relationship between assignee | |
// and value clear: | |
$manifest .= "Role: $role\t" | |
. "Name: $name\n"; | |
} | |
// function calls should have spaces after the commas. | |
return $this->boilerplate($manifest, "DUDES\n", "\nFIN"); | |
} | |
public function boilerplate ($body, $header, $footer) | |
{ | |
// returns and other "language constructs" (print, echo, require, etc.) | |
// don't need parens around them. most operators are less visually | |
// confusing with spaces around them. | |
return $body . $header . $footer; | |
} | |
public function ralphHat () | |
{ | |
// i sometimes align ternaries like so because it scans quickly and | |
// makes the relationship less ambiguous. (also, generally, fuck | |
// the ternary operator, especially in php where its precedence is | |
// wrong.) | |
return 'ralph' === $this->_dudes['captain'] | |
? 'Captain Hat' | |
: 'Not-Captain Hat'; | |
} | |
} | |
// finally, i leave off the closing php tag in the file because i will be | |
// certain to avoid that thing where there's unnoticed whitespace after it | |
// and i get an impossible-to-track bug because it gets spit out by php | |
// as soon as the file is included. god i hate that. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want I'll add references for everything. :D