The DX Files: Improving Drupal Developer Experience

I am declaring a personal crusade to improve Drupal’s “Developer Experience,” which I hereby abbreviate as “DX.”

User experience can be defined as “the overall experience and satisfaction a user has when using a product or system.” A product’s UX affects a user’s interest, ability, and enjoyment when using a product. If a product’s UX sucks, people will not use it if they have a choice or hate using it if they do not.

Well, guess what? Software engineers are people, too. When creating a software product that is intended to be used by other developers, the UX includes the experience of those developers as they write code for the product. But software engineers aren’t exactly like normal people and the kinds of issues developers face are very different from the issues the end users of the product face, so it seems logical to me to talk about DX distinctly from UX just to keep the target audience straight.

My first attempt at a definition for DX is “the overall experience, satisfaction, and efficiency a software developer has when using a software development platform.” I’m sure a better definition is possible.

The Drupal community has not historically been great at UX design (mostly we’re just a bunch of hackers) but recently has focused its enormous talents in this direction and started to make great strides. Somewhat surprisingly, we have also not historically been great at DX design. Drupal is quite powerful and flexible but unless you climb a fairly steep learning curve it can be hard to develop for. I have personally spoken with a number of web developers who have evaluated Drupal and found it too annoying, too unlike everything they have done before, and sometimes just too “backwards” for them to want to use it.

There are many, many ways to improve Drupal’s Developer Experience. Some are trivial, some are complex, and many are in between. My plan is for “The DX Files” to be a series of articles describing what I consider to be poor Drupal DX and suggesting solutions.

Since I’ve already rambled on a bit, I’ll start with a very simple DX issue for today: string literals vs. defined constants. Consider this completely typical example from Drupal’s user.module:

<?php
function user_register_access() {
  return
user_is_anonymous() && variable_get('user_regsiter', 1);
}
?>

This function determines if a particular visitor has permission to register for an account. It says the user must not yet be logged in and that the site-wide “new user accounts are allowed” (user_register) setting is enabled. The variable_get() function takes a default value as its second argument which, in this case, is 1 for “enabled.”

The example uses a string literal ‘user_register’ instead of the much more accepted approach of defined constants. Why would a defined constant be better? All the standard reasons: more readable code, the ability to change the constant in one place instead of many, detection of typos at compile time, auto-completion in IDEs, etc.

Case in point: did you notice that user_regsiter is mis-typed? (Note: I introduced the typo for this example; the real code is correct.) The program still “works” but contains a security flaw: If the administrator sets the “user_register” setting to disabled, this code will not see it because it is looking for “user_regsiter” and as a result will use the default value of “enabled.” Oops.

If the user module used a defined constant, the bug could not occur:

<?php
define
('USER_REGISTER', 'user_register');
function
user_register_access() {
  return
user_is_anonymous() && variable_get(USER_REGSITER, 1);
}
?>

Now the code will simply fail to compile and the typo will be discovered immediately.

I actually believe there are two other flaws in user_register_access(), one DX-related and one security-related. Will you be the first to point them out?

UPDATE on May 19, 2008: I’ve submitted at patch to replace all Drupal “variable” names with defined constants; see http://drupal.org/node/260226.

To happier coding!

Comments

Good call, Barry! I'm a big

Good call, Barry! I'm a big fan of defined constants for exactly the reasons you've stated (IDE support means something to me these days too even).

+1

Barry, what you point out is

Barry, what you point out is only half the problem there.

My experience with 'define' in PHP is much less satisfactory than it is with #define in C++. For one, it creates an extra step for the developer, and it's a step that adds a fair bit of code.

But really, that whole variable_get() call is flawed; you put the default in at variable_get() time, which is much worse than making a typo in the name of the string.

Strings are no less searchable than defines (at least, 'user_register' vs USER_REGISTER is going to be pretty much equivalent in my editor's search), and you do gain the typo checking, kind of. (Note: only if you have E_NOTICE on, which Drupal now does but this is new as of Drupal 6).

Of course, you could get the exact same effect by having something that defines the default, and have variable_get() fail if it fails to find both a default AND a set value. And you would gain consistency in the variable_get() default values, which is a much worse problem than the occasional typo in the variable_get string, since changing the defaults actually is kind of a pain.

How about the use of 1 as

How about the use of 1 as opposed to a USER_REGISTRATION_ENABLED constant?

Typo correction: You must

Typo correction:

You must mean define(), not defined().

Earl: I *absolutely* agree

Earl: I *absolutely* agree with you that having to specify the default to variable_get() is a horrible design; that's the other UX flaw I referred to in the penultimate paragraph. I would support a patch to change the variable system so that variables are declared, documented, and given a default in a module hook (yet another registry!). There are several issues in the queue about this; one of them is http://drupal.org/node/145164.

greggles: A USER_REGISTRATION_ENABLED constant wouldn't be a bad idea, but that was not one of the flaws I had in mind. In general, I think using boolean TRUE/FALSE or 0/1 values is fine; we don't need a zillion define()s for TRUE/FALSE. But that's just my opinion.

Benjamin: Thanks for the correction; fixed.

The security flaw I referred

The security flaw I referred to: If the USER_REGISTRATION variable has never been set, the variable_get() call defaults to returning "enabled" so users will be able to register for accounts. I've seen plenty of bugs in Drupal where code assumes that a variable has been set by having the settings form submitted and ends up with unintentional behavior if it has not.

The current behavior reflects Drupal's history as originally being developed for open blogging sites but may not be well-suited to its growing use as a platform for a wider variety of sites.

I’ve submitted at patch to

I’ve submitted at patch to replace all Drupal “variable” names with defined constants; see http://drupal.org/node/260226.

Did you test what you're

Did you test what you're writing?

PHP will (by default) interpret any unknown literal as a string. Thus your claim that "the code will simply fail to compile and the typo will be discovered immediately" is completely wrong.

Yes, I implemented and

Yes, I implemented and tested these changes; see http://drupal.org/node/260226.

Drupal 6 has E_ALL warnings enabled. So, true, the compile will not actually fail, but the warning will be prominently displayed and noticed.

<?php
error_reporting
(E_ALL);
define("A", "a");
define("A", "b");
print
C;
?>
Notice: Use of undefined constant C - assumed 'C' in Command line code on line 1
Notice: Constant A already defined in Command line code on line 1
C

I would support the idea of

I would support the idea of having a hook_variables where the module developer declare, document and define default values for the variables. As you say, this hook would be yet another 'registry hook', like would be hook_blocks (http://drupal.org/node/257032), and others, ...

Having a column in the variables table to store the module that provides the variable would be very useful, the system automatically can delete the variables when a module is uninstalled and modules won't need to worry about this.

Many variable names are

Many variable names are dynamically built (ex: $var1 . '_' . $var2 ). You system leaves that out completely. How would you go about that? Create constants for every number, letter, and word? not such a great solution.

Look at the patch I

Look at the patch I submitted. e.g.:

<?php
define
('NODE_OPTIONS_', 'NODE_OPTIONS_');

variable_get(NODE_OPTIONS_ .'page', ...);
?>

Very good article. I really

Very good article. I really like it very much. What nice way to describe your thought. The potential benefits here far outweigh the potential drawbacks. Certainly this would be perceived as raising the barrier to entry a bit, but the possibility for better and more standardized documentation as webchick mentions is a big mitigating factor there. Actually, for the most basic use-case (and the one that new developers will be encountering most) it's a minor syntactic change (as Barry points out). Is "new TextFieldElement" significantly harder to remember than "'#type' => 'textfield'"? For a developer who is just starting out I would wager it's about the same. The vast majority of devs don't write field-level validators or anything else of that nature so they would never have to touch the potentially more-confusing OO parts but for devs who *do* need to muck around with that sort of thing, the power and clean extensibility that an approach such as this offers is a big advantage.

Here is a perfect example

Here is a perfect example from Drupal core:

<?php
    $output
= node_view($node, FALSE, TRUE);
?>

Good point. As I started of

Good point.
As I started of with drupal five months ago, I had the same remarks as barry on strings that maybe better of as constants. That said, it is also true what merlin said about the variable_get/set system.
I wonder if a variable system with written-to-file constants would work.
I have written a framework in php myself and did some testing on how to use a flexible and extendable variable system. (I stopped developing the framework since i tried drupal). There are three ways of implementing things when you save a form to manage variables:
- defined constants that are saved by writing them to .constant.cache and plugins.constant.cache files.
- ini files that can be parsed, seem a bit nicer but are in fact slower because the defined ini vars still need exist as php variables.
- Save variables to database and pull out what you need at runtime. (lots of standalone sql's are slower of course).
What i chose was to save variables to database, and everytime a variable settings form is saved, i write all of them again in a module based file system. In fact that was the best method for me because it is fast and easy to use.

Conclusion for me is to use constants as much as possible in stead of string variables. It is true that a integer as 1 is the best way, but still I'd choose a conventional system and take the number variables with it to constants.

Hi, This is an interesting

Hi,

This is an interesting issue that I'm currently looking into. Thanks for posting this.

As a side note (and I hate to be that guy but since your main point centers around usability), you might want to consider a font size for you website that is actually readable. A default font size of 1em is horribly unreadable.

Seriously.

Now that is what I call an

Now that is what I call an excellent guide, thanks

Very good article on drupal

Very good article on drupal it's very help for all reader.

Well, this is a very valuable

Well, this is a very valuable post. Thanks for the information you provided. It would be great if got more post like this. I appreciate it.

huhu

huhu

You will surprise to find the high quality tiffany jewelry in much.Everyone will focus on the shinning of
tiffany co jewelry without awareness of
rovide 100% sterling silver Tiffany jewelry,you can buy discount Tiffany & Co jewelry here.Tiffany And Co Jewelry is the best jewellry.
discount tiffany jewelry, Tiffany Jewelry, Tiffany Jewellery, Tiffany Silver, Tiffany, Tiffanys, Tiffany's Jewelry.We Provide a wide range of fashionable
tiffany co jewelry, including
In 1950 Tiffany's had its biggest boost in popularity as Truman Capote's Breakfast at Tiffany's was published. Tiffany's was shot to worldwide fame in 1961 when the film adaptation staring Audrey Hepburn was released. This film and its star became icons, as did Tiffany's
Tiffany Bracelets
Tiffany Rings
cheap tiffany with.Discounted Tiffany & Co silver jewelries are provided in our Tiffany’s online outlet store
Tiffany Earrings
Tiffany Necklaces
Links Of London
Links Of London

Conclusion for me is to use

Conclusion for me is to use constants as much as possible in stead of string variables. It is true that a integer as 1 is the best way, but still I'd choose a conventional system and take the number variables with it to constants.
Engineering Diploma | diploma science | education consultant

What i chose was to save

What i chose was to save variables to database, and everytime a variable settings form is saved, i write all of them again in a module based file system. In fact that was the best method for me because it is fast and easy to use.
fire school | Performing Arts school

That little blue box -

That little blue box - everyone woman dreams of seeing it come their way. tiffany jewelry jewelry is world renowned for its stunning quality and top of the line artisanship; however, it also has a reputation for having a price tag that is way out of most people's leagues.
discount tiffany jewelry That doesn't mean you can't get that Tiffany look you want at a price you can afford, however. Thanks to replica Tiffany jewelry, you can look like you are draped in Tiffany jewelry, and yet still have the money left in your pocket to go out and show off your looks.

That little blue box -

That little blue box - everyone woman dreams of seeing it come their way. tiffany jewelry jewelry is world renowned for its stunning quality and top of the line artisanship; however, it also has a reputation for having a price tag that is way out of most people's leagues.

nice article. Term

its really good. Term Paper

PHP will (by default)

PHP will (by default) interpret any unknown literal as a string.
live scores online
live scoring
latest scores
live soccer scores

Hi. I would support a patch

Hi. I would support a patch to change the variable system so that variables are declared, documented, and given a default in a module hook (yet another registry!)

Tiffanys keys, the series put

Tiffanys keys, the series put out by Tiffany, adopt the familiar shape of silver tiffany jewelry to open a unique kind of fashion. tiffany chain The super model, Karen Elson who takes the leading place in the exhibition of tiffany store, well displays the great designed pith of Tiffanys as a world brand.

Nice articles, but I am not

Nice articles, but I am not creplica handbags
replica bagslear about the point you mentioned about how to distinguish fake and real .

Nice post. This post is

Nice post. This post is different from what I read on most blog. And it have so many valuable things to learn. Thank you for your sharing!

In 2004, Louis Vuitton

In 2004, Louis Vuitton celebrated its 150th anniversary. The brand also LV Messenger Bag in New York City (on Fifth Avenue), S?o Paulo and Johannesburg. It also opened its first global store in Shanghai. By 2005, LV Neverfull MM reopened its Champs-élysées store (reputed to be the largest LV store in the world), and released the Speedy watch collection. In 2006, LV held the inauguration of the Espace Louis Vuitton on its 7th floor.[

How are you. Before we set

How are you. Before we set our hearts too much upon anything, let us examine how happy those are who already possess it.
I am from Republic and also now teach English, give please true I wrote the following sentence: "American more unsafe short attorneys have seen free debts of statement and maximisation using low debt, ware visits, many legal fees and more."

With love 8), Delia.

Very interesting and

Very interesting and informative post. Thanks for sharing. You always come up with interesting stories.
emo girl music groups | punk blog | cool wordpress theme.

Nice articles, but I am not

Nice articles, but I am not clear about the point you mentioned about how to distinguish fake and real louis vuitton handbags.

Nice articles, but I am not

Nice articles, but I am not clear about the point you mentioned about how to distinguish fake and real louis vuitton handbags.

this is exactly what i was

this is exactly what i was looking for and will help me in the future thanks

Well, this is a very valuable

Well, this is a very valuable post. Thanks for the information you provided. It would be great if got more post like this. I appreciate it.

Free vector logo | free wallpapers | Celebrity News | Games News

tiffany jewelry hot sale now

tiffany jewelry hot sale now with discount. Tiffanyhotsale provides the best Tiffany & Co jewelry, including Necklaces, Bracelets, Earrings, Pendants and so on .

Tiffany Sale Provide 100% sterling silver tiffanys jewelry,you can buy discount Tiffany & Co jewelry here.Tiffany And Co Jewelry is the best jewellry

Stunning silver jewellery from tiffany, Gucci, Links of London; quality designer silver jewelry at online prices. We have silver Bracelets, Solid Silver ...

Online wholeasle Clothing

Online wholeasle Clothing store where you can buy clothes accessories also include Ugg Boots,Mac cosmetics,fashion Jewelry,Designer Sunlasses and Gucci shoes,replica handbagsat wholesale price!

The current behavior reflects

The current behavior reflects Drupal's history as originally being developed for open blogging sites but may not be well-suited to its growing use as a platform for a wider variety of sites.

Herve Leger womens fashions

Herve Leger womens fashions at ShopStyle. Shop popular stores to find Herve Leger womens fashions on sale - all in one place. Create and share looks based
Herve Leger
Cheap Herve Leger
Jewelry,Necklaces,Jewelry Necklaces,Links of London Necklaces,online sales a variety of world famous such as Links of London etc,with competitive price.
Links of London , the leading British contemporary jeweller was founded in 1990 by jewellery designer Annoushka Ducas and her husband John Ayton.cheap Links of London jewelry at online linksgif UK store, including Links of London Necklaces, Links of London Charms, Links of London Earrings. Links of London Silver Sweetie Bracelet Medium
Links of London Charm
Links of London

Shop for a nba jersey at

Shop for a nba jersey at SFGate Fan Shop:Why buy a nba jerseys from us? Our competitive prices on discount nba jerseyare a good reason.

Very good article. I really

Very good article. I really like it very much. free games

PHP will (by default)

PHP will (by default) interpret any unknown literal as a string. Thus your claim that "the code will simply fail to compile and the typo will be discovered immediately" is completely wrong.games

thanks for all the marvelous

thanks for all the marvelous internet tips much appreciated

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <i> <h1> <h2> <h3> <blockquote>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options