3. Disclaimer
● Not rules but recepies
● You are not the last person who will look at
this code
● Leave the code in better shape that you found
it
● Debates are welcome
4. Learn PHP and Drupal functions
● Build array (5 => 5, 6 => 6, …, 10 => 10);
– drupal_map_assoc(range(5, 10));
● Sort form element
– uasort($form, 'element_sort'); // by #weight
– uasort($form, 'element_sort_by_title');
● Validation: valid_email_address(), valid_url()
● Get all keys not prefixed with #
for (element_children($form) as $key) {}
6. Names
● Variables, classes – nouns. Methods, functions –
verbs
● Shorter variable name for local scope ($i)
● One name per concept ($user, $account, $person,
$site_visitor)
● Domain names ($student, $course, $lesson...)
● Patterns names (singleton, visitor …)
● Funciton name prefixes isSomething(),
setSomething(), getSomething(), doSomething()
7. Constants
● Use constants to describe what static vars
mean
if ($user->uid == 2)
if ($user->uid == USER_SUPER_ADMIN_UID )
if ($user->uid == self::USER_SUPER_ADMIN_UID)
9. Functions
● Functions should do one thing and be small
and clean. Example
menu_execute_active_handler():
– Checks whether site is online or not
– Executes menu callback
– Deliver callback result
● _menu_router_build() has more than 200
lines!
10. Functions
● Group arguments to eliminate number of
arguments
● Pass structures that function is going to alter if
results are complex
12. Function
● Helper functions underscore prefix
(_menu_delete_item())
● Group helper functions into classes with static
methods. Autoloading.
13.
14. Comments
● Comments get outdated first!
● Comments should be written when we fail to
find good variables / function names
● Think twice about names before writing a
comment explaining your intentions
● Why it is done this way and not What is this
15.
16. Spaghetti code
● Return result early (continue and break cycles)
23. Use Exceptions
● _io_checkout_page_ajax_validate__if_email_exists()
24. Use Polymorphism
● D8 plugins system
● Views handlers
● Take a look at function names in form.inc:
– form_type_checkbox_value(), theme_checkbox(),
form_process_checkbox()
– form_type_select_value(), theme_select(),
form_process_select()
25.
26. Organize your code right
● Separate files
● Follow traditions
● 1000 lines – you do it wrong
27. Organize your code right
● Sort by source and not by name
● First hooks implementations, then helpers
35. Refactoring loves tests
● Some tests better than
no tests at all
● Use DrupalUnitTestCase
for unit testing (fast)
36. To read
● Robert C. Martin. Clean Code: A Handbook of Agile
Software Craftsmanship
● Martin Fowler. Refactoring: Improving the Design of
Existing Code
● http://www.slideshare.net/rdohms/bettercode-
phpbenelux212alternate
● http://london2011.drupal.org/conference/sessions/code-
stinks
● http://chicago2011.drupal.org/sessions/aphorisms-api-
design