Why version() ?

Denis Koroskin 2korden at gmail.com
Tue Feb 10 15:27:12 PST 2009


On Wed, 11 Feb 2009 02:16:16 +0300, Walter Bright <newshound1 at digitalmars.com> wrote:

> Nick Sabalausky wrote:
>> This strikes me as throwing away the baby with the bathwater. If your  
>> code starts degenerating towards a versioning rat's nest, then the  
>> solution is to take a moment and refactor it into a larger granularity,  
>> not to throw away features that are useful in moderation.
>
> True, but that never, ever happens. It's always "I can get my few  
> additions to this rat's nest in and just get it working, and worry about  
> proper abstraction later."
>
>>> 1. Why not version(!feature) ?
>>>
>>> Because cognitive studies show that people just don't see the negation.
>>  Isn't that moreso a case against the ! operator in general? I don't  
>> see how "if(!blah)" is any less susceptible to overlooking the ! than  
>> "version(!blah)".
>
> Yes, you're right, it's both a minor and an inconsistent point, despite  
> it being right <g>.
>
>
>>> Secondly, when you see things like:
>>>
>>>     version (!linux) { ... }
>>>
>>> they're almost always wrong. Versions should be positive things,  
>>> because a version is something that is being build - one doesn't craft  
>>> a makefile to build a notLinux.
>>  True, we don't build to a NotLinux, but we do however build to  
>> "everything except platform X".
>
> We do, and they're always wrong. I always find when doing that and  
> porting to another machine that those #if !linux must be reengineered. I  
> have switched entirely over to:
>
> #if Windows
>     ...
> #elif linux
>     ...
> #elif __APPLE__
>     ...
> #else
>     assert(0); // fix when adding a new platform
> #endif
>
> because otherwise I *miss* sections that need updating when porting to a  
> new platform, and have a bug that needs tracking down.
>
> The point is, when writing portable code, especially code that will be  
> ported by others, you have *no idea* what platforms will fall into the  
> "notLinux" section and what peculiarities those platforms might have. It  
> works much better to have the compiler ding me when it encounters  
> platform specific code that doesn't have a section for the platform I'm  
> compiling for.
>
>> And I don't see how a "LinuxOrMac" (or worse yet, a  
>> "LinuxOr_MacAndSomeOtherCharacteristic_") is any more or less of a  
>> realistic platform than a "NotLinux".
>
> Based on my experience porting code, LinuxOrMac is a far better solution  
> (i.e. faster to code, faster to port, fewer bugs) than notWindows.
>
>
>> Also, you didn't respond to the concerns about typos in a version  
>> identifier. Can we assume you agree that's a problem?
>
> It's a problem without a solution unless you propose adding some sort of  
> declaration syntax for all the version identifiers.

Why did you catch that controversial !Linux example? Just imagine it is !HasFeatureX:

version(!HasFeatureX) {
   ...
}

is so much more intuitive than

version (HasFeatureX) {
} else {
   ...
}




More information about the Digitalmars-d mailing list