Coding standards and best practices

This group is dedicated to optimizing performance of Drupal with coding standards. As such it deals with particular coding guidelines, sharing knowledge and benchmarks that might be integrated into Coder module.

If you are seeking help on high performance/scalability Drupal sites and how to gain a better performance for them, you might have a look at the High Performance group.

Guidelines
  • Each coding standard gets its own Wiki page including
    • a one line synopsis in the format XY is faster than YX
    • a proper description of how it improves performance
    • benchmarks from different platforms and PHP versions
    • web links to references and additional resources
  • You may
    • suggest new optimizations by posting a story
    • comment on new optimization proposals with insightful input
    • perform benchmarks on your platform(s) for existing and new optimizations
mfb's picture

Only variables should be passed by reference

As you know if you've fiddled with PHP's error_reporting setting, Drupal 7 is not yet E_STRICT compliant. E_STRICT compliance is a good idea for forward compatibility (PHP 5.3 and 6.0) and also helpful for servers that already log E_STRICT errors.

Login to post comments · Read more
leontong.brightlemon's picture

Regular London code sprints...?

To all London (and UK) folk,
Having just got back from a mind blowing DrupalCon Paris, we were discussing on the train back the possibility of starting a regular London based code sprint. Brightlemon would be happy to host and organise it - provide the logistics etc.

1 comment · Read more
Chris Charlton's picture

Drupal Code +Test Sprint Beginners Guide

THIS WIKI PAGE IS A WORK IN PROGRESS

In helping with LA Drupal's D7 Code+Test Sprint event, I noticed there's some easy points that help newbies feel progressive. There are many ways to help at a Code+Test sprint, and here are the ways that helped us in Los Angeles.

  • Make sure you have a Drupal.org account, with "Contributor links" turned on in your profile. This will expose a sidebar block linking to areas of interest for contributors. For example: "Patches to review (D7)" is what most people click on to find patches needing review.
Read more
webchick's picture

Discussion on PHPDoc summary best practices

http://drupal.org/node/487802

Since the issue is already going strong, probably discuss over there, rather than here. Just letting you know about the discussion. :)

Login to post comments

Beautifier

Just a heads up that I've been working on something of interest to this group. I am making a 'code beautifier' module privately for use by my company - we have some peculiar standards like '3 spaces' for a tab - and I know I will be inconsistent due to me switching between Drupal and other standards.

The idea is that it is a web form that you fill in and it gives you the code you need, all formatted and indented nicely.

Login to post comments · Read more
Chris Charlton's picture

Do you test new modules before uploading them to your live site?

I test every module I download before installing it on my live site(s).
63% (78 votes)
I have tested some modules before deploying.
26% (32 votes)
I have not tested any modules before uploading them to my site.
3% (4 votes)
I have not tested any modules, but really should.
6% (7 votes)
I code every module I use and never, ever need to test my own code.
2% (3 votes)
Total votes: 124
26 comments
zzolo's picture

Is there a File Name Standard or Convention?

Is there a File Name Standard or Convention?

I have set of modules that have lots of includes. Is there any written documentation on how these things should be organized. For instance with JS file I like to put in a JS folder in the module, but even then I am not sure how to name the files. I know if there is one its "modulename.js", but what if there are many?

I guess there seems to be 3 main ways of doing this:

  • Dots: modulename.function.js
  • Underscores: modulename_function.js
  • Dashes: modulename-function.js
Login to post comments · Read more
Chris Charlton's picture

Adobe.com article: Building a Drupal site in 10 steps

I just saw my latest Drupal article is now published on Adobe.com, and this is the first in a series I have produced.

"Read on to see how you can set up a Drupal site in just 10 steps. Learn about adding content, altering menus, modules, and themes, and how to get your site ready for search engines and users."

Read the article now at http://www.adobe.com/devnet/dreamweaver/articles/drupal_site_in_10_steps...

3 comments · 1 attachment
FeedTheBull's picture

Drupal Themer | FeedTheBull, LLC

Employment type: 
Contract
Telecommute: 
Allowed

We are in the need of a Drupal Themer Ninja that can help us knock out a few pages in a relatively short timeframe. If you have the chops to make a killer looking site and have time to do it quickly, contact us at ron (at) feedthebull (dot) com and we will go over the details.

Requirements:
Drupal 5
standards-compliant
render properly in all major browsers (IE 6+, FF 2+, Safari 1+, Opera 5+)
comfortable with php, but mostly css/html
Theming ninja

zzolo's picture

Standards for Namespaces (Sub-Modules and Functions)

The following two things are well known conventions but I was unable to find any documentation on d.o for them:

  1. Sub-Module names.
    These are modules within modules (which are really projects on d.o). The convention, as I understand it is: {core_module_name}_{sub_module}. This basically ensures that no sub-modules will use the same namespace as contributed modules or sub-modules.
  2. Function Names.
    We know that all hooks have to be prefixed with the module name, but it is only common practice to make any other public or private function prefixed with module name (as well as the leading underscore (_) for "private" functions): {module_name}_{action}. Again, this basically handles name collisions with other contributed modules. This standard could also include how to form function names, for example as suggested by webchick: {module_name}_{verb}_{thing_being_verbed}

So, what I need from this great group is:

  1. Are these agreed upon standards?
  2. Where is the best place for this documentation?
    There is: Module Development, but even under that, there is no general module development guidelines (instead of specific version)
    And: Standards and Security
    Or: some other.
5 comments · Read more
Shyamala's picture

IE 6.0.2900.2180

One of our sites is not functioning in IE 6.0.2900.2180 version!

It throws up a Runtime error

Runtime Error!
Program C:\PROGRAM FILES\INTERNET EXPLORER\IEXPLORE.EXE
R6025

The site works perfectly in other versions of IE 6 , IE 7, and Firefox.

4 comments
JamesAn's picture

Clarification on Doxygen formatting conventions

I posted some questions on API formatting conventions in http://drupal.org/node/422084.

webchick recommended I post them here for consideration. The discrepancies I listed are all present in the core API document in the various api.php files.

Let me know what the convention is/should be. Thanks!

3 comments
sun.core's picture

Coding-standard for arithmetic/logical operators

Neither PEAR nor we define a clear coding-standard for the logical negation operator ("!" as in !$foo). However, to my knowledge, all of Drupal core + contrib is using:

<?php
if (!$foo) {
 
bar();
}
$c = !$a;
?>

In a code clean-up issue for Better Formats module, dragonwize proposed to use a space between the operator and following code, as we do with the concatenation operator and arithmetic operators:

<?php
if (! $foo) {
 
bar();
}
$c = ! $a;
?>

6 comments · Read more
webchick's picture

Bikeshed Showdown: Figure out a "one true" DBTNG syntax

I've heard various rumblings in #drupal from people who don't like the current DBTNG code style for one reason or another. Since PDO doesn't have coding standards, we've had to make up our own. Complaints range from it being inconsistent (which wouldn't shock me, since it was decided sort of piecemeal between random conversations between Larry and myself) to not being easily formatted in tools like Coder Format and emacs (which is a big problem) to just being kind of "icky." Not having this nailed down is actively blocking issues such as porting core files to DBTNG.

So let's have it out! Here's the current syntax. Can you do better? And why?

<?php
    $result
= db_query("SELECT name FROM {test} WHERE age = :age", array(':age' => 25));
   
db_insert('test_task')
      ->
fields(array('pid', 'task', 'priority'))
      ->
values(array(
       
'pid' => $john,
       
'task' => 'eat',
       
'priority' => 3,
      ))
      ->
values(array(
       
'pid' => $john,
       
'task' => 'sleep',
       
'priority' => 4,
      ))
      ->
execute();
?>

15 comments · Read more
Morbus Iff's picture

Coder Tough Love module released

Coder Tough Love is a companion to the existing Coder module by Doug Green, and its initial development has been sponsored by Trellon. Unlike Coder, which strives to follow the documented style guidelines of Drupal core, Coder Tough Love takes the tougher tactic of applying finely aged and obsessively anal wisdom from years of Drupal development and persnickety quality control.

Login to post comments · Read more
webchick's picture

Bug: The title of this group <del>makes</del> made no sense. :)

As DamZ mentioned on IRC, this is roughly equivalent to the "Database API and square dance group." ;) There is already the HIgh Performance group for performance optimization.

How about "Coding standards and best practices" which includes coding standards as they relate to performance, security, upgrading, and all the other stuff Coder can do, as well as things it can't yet.

Thanks, this was fixed by sun. :)

Login to post comments
webchick's picture

Casting variables

We should figure out and document a standard for this, then roll a patch to make it consistent.

Please see: http://drupal.org/node/331951

5 comments
Grugnog2's picture

Generic module to find/add indexes

This is a wiki to plan a module to allow admins to add indexes to their site schema - this can be done easily in Drupal 6 and beyond using http://api.drupal.org/api/function/hook_schema_alter/6 - this discussion came from http://drupal.org/node/231453, where it was realized that CCK does not really have the usage context to be able to do this effectively, and also there are tables beyond CCK getting used in new ways by views and other modules that also need alternate indexes.

Read more
stella's picture

SQL coding standards and whitespace

I was just wondering if we have, or if we should have, any SQL coding standards regarding whitespace usage. For example, should we be using a space between column names in IN where clauses or have spaces on either side of comparison operators, etc.

Here are some examples to illustrate what I mean:

SELECT a,b,c FROM {table} WHERE a IN (1,2,3) AND b=4;
vs
SELECT a, b, c FROM {table} WHERE a IN (1, 2, 3) AND b = 4;

and

INSERT INTO {table} (a,b,c) VALUES(1,2,3);
vs
INSERT INTO {table} (a, b, c) VALUES (1, 2, 3);

6 comments · Read more
Morbus Iff's picture

Create a README.txt template

There was a recent discussion about README.txt files in #drupal, and I figured it was time to reinvigorate the README.txt discussion. It appears, however, that one person (hello, you-know-who-you-are! ;)) has been promoting a README.txt that /is not like anything core has ever done/, as seen in the links to examples or as "an increasing list of modules" (which all happen to be maintained or contributed to by the same person, thus making the claim slightly disingenuous).

I submit that the README.txt file this person promotes is entirely wrong.

19 comments · Read more
Syndicate content