Is there any reason that $default is a required variable for variable_get? Why not optional like this so we don't have to specify NULL in our code where that is logically the default?

Comments

coreb’s picture

Version: x.y.z » 6.x-dev

Moving out of the "x.y.z" queue to a real queue.

cburschka’s picture

Version: 6.x-dev » 7.x-dev

Yes please.

Changing to D7, but this could still get copied into the D6 branch. It's an API function, but the change is fully backward-compatible since all existing calls provide a default value.

robloach’s picture

Status: Active » Needs review
StatusFileSize
new5.79 KB

The attached patch will make variable_get's second parameter have a default value of NULL. I searched for variable_get calls that passed NULL as the second parameter and removed the argument.

heine’s picture

@arancaytar, this cannot go in D6, as modules that depend on this behavior will be minor version specific (only work with 6.2+). That kind of version dependency is not supported by the core attribute in .info, nor is a minor version dependency desired.

Freso’s picture

Patch applies cleanly to HEAD, and not being sure of what to test, I simply tried to do various. Nothing broke. I'm not confident enough to mark this RTBC though, but I'm fairly certain that's the status it is worthy of. :)

pasqualle’s picture

+1 null is used to mean "A value isn't available"
if it still applies then it is RTBC

Freso’s picture

Status: Needs review » Reviewed & tested by the community
freso@nayru /s/h/l/h/drupal7> patch -p0 < 88264.patch
patching file includes/bootstrap.inc
patching file includes/file.inc
patching file modules/color/color.module
patching file modules/locale/locale.install
patching file modules/profile/profile.module
patching file modules/system/system.install
Hunk #2 succeeded at 2425 (offset 110 lines).
patching file modules/user/user.module
freso@nayru /s/h/l/h/drupal7>

Still applies (though with some offset), so marking RTBC.

dries’s picture

In what cases there would be no default?

robloach’s picture

Hi Dries, all the cases introduced by the patch are examples of when there doesn't need to be a default.

robloach’s picture

StatusFileSize
new5.55 KB

It's nice to have the default NULL because it saves you some typing when you don't care what the default is (NULL).

I've updated the patch to HEAD.

rszrama’s picture

Re: use cases, #8, I suppose it's technically just making it easier for when the default ought to be NULL. Besides those in core, I use this for many settings fields where I'm expecting a user to configure something without an out-of-the-box setting (like a store's mailing address). When it gets down to it, though, it's not incredibly difficult to add those NULLs or null strings. : )

catch’s picture

Status: Reviewed & tested by the community » Needs work

Only applies with fuzz now. I'd rather see #145164: DX: Use hook_variable_info to declare variables and defaults get in though.

robloach’s picture

Status: Needs work » Needs review
StatusFileSize
new6.86 KB

Updated to latest HEAD, did a file search for all variable_get calls passing in NULL for the value, and removed the argument.

Catch, I would rather have #145164: DX: Use hook_variable_info to declare variables and defaults in as well, but I don't see it happening anytime soon. Would be good to at least get this small patch in.....

Anonymous’s picture

Status: Needs review » Needs work

The last submitted patch failed testing.

kscheirer’s picture

Status: Needs work » Needs review
StatusFileSize
new8.39 KB

I'd still really like to see this patch go in, it's a sensible default.

Rerolled the patch against HEAD, caught a few more NULLs.

We could probably catch a few more, in places where the default is '' or 0, but NULL would be functionally equivalent.

senpai’s picture

Status: Needs review » Reviewed & tested by the community

I reviewed the code in the patch, and all looks well. It applies to the latest HEAD, and I tried several pages which call some variables, which were fine. In particular, I manually deleted the string in {system} cron_last, and the function still properly passes the if (!is_numeric($cron_last)) { $cron_last = variable_get('install_time', 0); }

I spotted several places where a function's param 2, i.e. "(, 0)" could be removed, but those don't hold up this particular patch from being committed. Go for it!

robloach’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new9.03 KB

Sorry for setting it back to Code Needs Review, but I thought it wasn't awesome enough and needed a test. Hopefully this gets webchick's attention now....

It also seems like this is the appropriate first step towards #145164: DX: Use hook_variable_info to declare variables and defaults.

jose reyero’s picture

+1 for this simple feature.

As Rob Loach mentions, there's this other patch about declaring variables, which I know from my own experience (I put a lot of hours on it for Drupal 6) it needs to always be a much bigger patch because of this issue.

There are a lot of places in the code where defaults are not really needed and any patch attacking the issue of providing global defaults for variables will always bump into this issue, which is one of the reasons it always seems to get stuck.

webchick’s picture

Status: Needs review » Needs work
Issue tags: +Needs documentation

A small step towards resolving one of our biggest DrupalWTFs (#145164: DX: Use hook_variable_info to declare variables and defaults)

Thanks for the tests. Committed to HEAD. Not sure about back-porting this to D6, because it means a (small, backwards-compatible) API change.

Please add to the upgrade docs.

robloach’s picture

Status: Needs work » Fixed

Thanks Angie, you're my hero........... Update docs are up, if you're a better writer, then please feel free to hack at it.

webchick’s picture

Issue tags: -Needs documentation

Yay! Thanks, Rob Loach. :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.