| Defined | src/docs/contributing/general_coding_standards.diviner:1 |
|---|---|
| Group | Contributing |
This document is a general coding standard for contributing to Phabricator, Arcanist, libphutil and Diviner.
This document contains practices and guidelines which apply across languages. Contributors should follow these guidelines. These guidelines are not hard-and-fast but should be followed unless there is a compelling reason to deviate from them.
For example, avoid this kind of code:
$category_map = array_combine( $dates, array_map(create_function('$z', 'return date("F Y", $z);'), $dates));
Expressing this complex transformation more simply produces more readable code:
$category_map = array(); foreach ($dates as $date) { $category_map[$date] = date('F Y', $date); }
And, obviously, don't do this sort of thing:
if ($val = $some->complicatedConstruct() && !!~blarg_blarg_blarg() & $flags ? HOPE_YOU_MEMORIZED == $all_the_lexical_binding_powers : <<<'Q' ${hahaha} Q );
In Phabricator, you can usually use the builtin XHProf profiling to quickly gather concrete performance data.
For example, avoid these sorts of naming choices:
$PIE->GET_FLAVOR(); // Unconventional. $thing->doStuff(); // Ambiguous. $list->empty(); // Ambiguous -- is it isEmpty() or makeEmpty()? $e = 3; // Not descriptive. $this->updtHndlr(); // Nonstandard abbreviation. $this->chackSpulls(); // Misspelling, ungrammatical.
Prefer these:
$pie->getFlavor(); // Conventional. $pie->bake(); // Unambiguous. $list->isEmpty(); // Unambiguous. $list->makeEmpty(); // Unambiguous. $edge_count = 3; // Descriptive. $this->updateHandler(); // No nonstandard abbreviations. $this->getID(); // Standard abbreviation. $this->checkSpelling(); // Correct spelling and grammar.
When you ignore errors, defer error handling, or degrade the severity of errors by treating them as warnings and then dismissing them, you risk dangerous behavior which may be difficult to troubleshoot:
exec('echo '.$data.' > file.bak'); // Bad! do_something_dangerous(); exec('echo '.$data.' > file.bak', $out, $err); // Also bad! if ($err) { debug_rlog("Unable to copy file!"); } do_something_dangerous();
Instead, fail loudly:
exec('echo '.$data.' > file.bak', $out, $err); // Better if ($err) { throw new Exception("Unable to copy file!"); } do_something_dangerous();
But the best approach is to use or write an API which simplifies condition handling and makes it easier to get right than wrong:
execx('echo %s > file.bak', $data); // Good do_something_dangerous(); Filesystem::writeFile('file.bak', $data); // Best do_something_dangerous();
See Command Execution for details on the APIs used in this example.