views:

84

answers:

3

Should I frequently rely on default values?

For example, in PHP, if you have the following:

<?php

$var .= "Value";

?>

This is perfectly fine - it works. But what if assignment like this to a previously unused variable is later eliminated from the language? (I'm not referring to just general assignment to an unused variable.)

There are countless examples of where the default value of something has changed and so much existing code was then useless.

On the other hand, without defaults, there is a lot of code redundancy.

What is the proper way of dealing with this?

+5  A: 

In my opinion, the odds of a language changing drastically once it reaches a certain level of acceptance are pretty low.

To me, each language comes with a (sometimes more or less) unique set of features. Not using those because they just might disappear some day seems shortsighted. Naturally, don't use esoteric features just for the sake of doing so -- make sure you follow usual principles of readability and best practices for your language of choice, but otherwise I see no need to discriminate against particular features.

Anna Lear
So you're saying go ahead if the feature is well-established?
George Edison
And if it's actually documented to be a feature, rather than an accident of the most common implementation of the language: Yes, go ahead.
Brooks Moses
Yes. Depending on what the feature is, how well it's established might not even matter. As Brooks Moses said in his answer, if the feature in question is commonly used, you're likely in the clear. If it's an odd case or a side-effect, than it's more likely to get removed. But in the end, you gotta use something, so you might as well go with what the language gives you. :)
Anna Lear
@Anna: I assume you would use VB6 as a prime example? How about Python 2 vs. 3? Or PHP4 to 5? I think the right perspective is that when there's a committee behind a standard, the languages tend not to change in damaging ways. Otherwise all bets are off.
Ira Baxter
Ya. I started using Python 3 only to discover that it was VASTLY different than Python 2.
George Edison
+4  A: 

Default-value features of a programming language, if actually a documented part of the standard rather than just an accident of the implementation (which many past "default initializations" have been), are no different from any other features of a programming language. You might as well ask if it's wise to rely on anything else in the language, and the answer regardless of wisdom is that there's no choice -- you have to rely on something, and anything could hypothetically be changed in a future version.

Of course, if the thing that you're relying on is a commonly-used feature of the language, rather than an odd corner case, then there's a lot more chance that it will be retained in future versions. In addition, if you're concerned about such things, it's wise to choose a well-established language that has a history of maintaining backwards compatibility. Some languages take great pains to make sure that older code runs in the new version of the language, and some less so.

So, that's the general answer. The specific answer about default values is that it depends on the particular case, the language in question, and so forth. You can have absolute ironclad reliance on the fact that global static variables in C will be zero at program start. Some other cases are rather notably less reliable.

Brooks Moses
Good point, re: documentation.
Anna Lear
I'm guessing you use C a lot.
George Edison
True, I do -- and I know my answer's skewed a bit from that, and have tried to correct for it. But I've also worked on compiler implementations for Fortran, which has changed almost to the point of unrecognizability in the last 50 years, and I'm remembering discussions on what backwards compatibility to support or not support in the compiler when the language changed.
Brooks Moses
PHP doesn't have a standard. It doesn't even have a published grammer.
Ira Baxter
Documentation is good.
George Edison
A: 

If something is a defined feature of a language, it's probably pretty safe to rely on it.

Where you should be wary of relying on functionality is when you get into areas where you're dealing with behavior that is either unspecified, or if you're "misusing" a feature by using it for something completely other than what it was intended for.

Since "default value of a string" is pretty much a mainline scenario, it's probably safe. What's more dangerous is that if you happen to declare that variable earlier, you can get hit by it changing the expected value as an unintended side effect.

Basically, if you're not abusing a language feature, or relying on undefined behavior, you should probably worry more about unintended side effects in your code than you should worry about the language changing - especially if the language is mature.

kyoryu