This page outlines our best practices for programming within Drupal. It concerns higher level issues than those dealt with in the Coding Standards document or the Configuration & Usage Best Practices page(s).
The purpose of these pages is not to teach one how to program, but to teach one how to become a better programmer within this framework. Many people can write code, but many of those people do not know that there is a science to programming for Drupal. Why are speed and performance so important? Why document code? Why is this commonly used code snippet actually a mistake? These questions and so many more are answered and explained below.
Proposed topic outline
- Writing efficient code
- Using recursive functions
- Engineering fast code
- Avoiding unnecessary coupling
- Writing code that comments itself
- The necessity of documentation
- Naming your functions
- Using private functions
- Using global variables
- Making your modules extensible
- Reference bigger logic in hook functions rather than placing all logic in hook functions.
- Writing hook functions and theme functions
- When should you build an API for your module and how to do this
- OO code versus procedural and where Drupal stands on this
- Links to useful documentation on PHP programming best practices, e.g. http://www.odi.ch/prog/design/php/guide.php
- Database specific considerations
- e.g. how to store information efficiently, use of the system table, the watchdog table, etc
- Programming pitfalls to avoid
- Creating code that already exists
- Overusing if-else, switch-case, and try-catch
- The dichotomy of the ternary operator
- Not using a code repository to keep track of changes to your files.
- Creating duplicates of files in order to prevent overwriting changes.