Call site 'ref'

Robert Jacques sandford at jhu.edu
Sun Jan 15 17:18:11 PST 2012


On Sun, 15 Jan 2012 12:27:46 -0600, Peter Alexander <peter.alexander.au at gmail.com> wrote:
> On 15/01/12 5:57 PM, Robert Jacques wrote:
>> On Sun, 15 Jan 2012 07:36:53 -0600, Alex Rønne Petersen
>>> Thoughts?
>>
>> In no particular order:
>>
>> 1) Adding ref to the call site is lexically similar to Hungarian
>> notation and has all the drawbacks and advantages thereof. I know
>> invoking Hungarian notation is almost an invocation of Godwin's law when
>> it comes to programming syntax discussions, but Hungarian notation was
>> introduced for a reason; there was a time when for large software
>> projects it dramatically increases code comprehension (and thus quality)
>> for the code reviewer and/or code maintainer. And that argument still
>> stand today for anyone _not_ using a modern IDE. The primary reason
>> Hungarian notation is disparaged today is that IDEs evolved beyond
>> emacs, vim and notepad. Once they could tell the programmer with a
>> tooltip what every variable's type was, the need to encode the type in
>> the variable name vanished. Or more to the original point, modern IDEs
>> already list function parameter's type and type modifiers (i.e. aka) as
>> you type the function in so it's _always_ obvious what is ref/const/etc
>> and what is not. And for the code reviewer, ref parameters could be auto
>> highlighted/underlined/etc to easy their job. Yes, this issue does need
>> to be addressed, but I don't think that this is fundamentally a language
>> problem; it more a lack of modern tools for D. That reminds me, I need
>> to check out the latest revision of Visual D. :)
>
> A fair point, but:
>
> a) D doesn't have much in terms of *free* highly-featured IDEs.
Both DDT and Visual D are a *free* highly-featured IDE.

> b) Even if it did, lots of people still use emacs/vim.
So, you're proposing to "fix" the language instead of using a better dev environment? Like stick shift drivers and *nix sys admins, coders who use emacs/vim actively acknowledge that the onus for writing correct code is on their shoulders; there are not going to be any red-squiggles, line re-formatting or code completion to "get in the way"/"help them".

> c) That does not help when glancing at code to find where a variable is
> modified.
First, if the IDE colors every variable passed by ref blue, those are definitely going to jump off the page when glancing through code. Second, since when does _glancing_ at an unfamiliar piece of code every give meaningful results? Any reasonable code review (using a good IDE) will entail the reviewer checking the doc comments on any unfamiliar function.

>> 2) It is perfectly possible to write functions in D that require
>> explicit demarcation at the call site:
>>
>> int foo( int* v ) { return *x; }
>>
>> int y;
>> int z = foo(@y);
>
> True, but these aren't the same. Pointers can be reseated, ref
> parameters cannot.

Well, if the final storage class was brought back into the language...

> Pointers may also be null, encouraging needless null
> pointer checks.

Needless null pointer checks are only a possible problem in release code; assert solves that problem nicely.

>> 3) Mandating ref at the call is currently how C# handles reference
>> parameters. Anytime someone suggests a feature from another language,
>> particularly C# or Java, two questions are immediately raised in my mind.
>> 3a) Does the asker simply want D to be more like language X?
>
> Perhaps, perhaps not. I think it is better to evaluate a suggestion on
> its merit and not on the suggester's motivation.

Before I can evaluate your use case, let alone your suggestion I need to understand and filter your biases. It's really important to understand how much of a poster's frustration simply come from the cognitive disconnect between two different programming languages and the actual features and capabilities of D.

>> 3b) What do programmers experienced in X and in D/C++/C/etc think about
>> that particular feature (good/bad/ugly)?
>
> Mandated ref is well-received in C# as far as I'm aware.

And C# has never had non mandated ref; this is both a good and bad thing. It's a bad thing as a positive response to ref gets mixed with mandated ref. It's good thing because there's probably a developer blog or two you could link too providing good support by industry experts for your argument.

> I also know that people dislike using reference parameters in C++ due to
> the lack of visibility at the call site (causing them to use pointers).

 From the C++ FQA on references:
"Note: Old line C programmers sometimes don't like references since they provide reference semantics that isn't explicit in the caller's code. After some C++ experience, however, one quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g., programmers should write code in the language of the problem rather than the language of the machine."

 From Google's Style Guidelines:
Within function parameter lists all references must be const: void Foo(const string &in, string *out);
In fact it is a very strong convention in Google code that input arguments are values or const references while output arguments are pointers. Input parameters may be const pointers, but we never allow non-const reference parameters.


More information about the Digitalmars-d mailing list