Hipreme's #7 Tip of the day - Using the out parameters

Hipreme msnmancini at hotmail.com
Sat Dec 31 17:13:16 UTC 2022


So, after some time using D, I found out that `out` isn't used in 
so many cases, but when used, it can be quite confusing, because 
if you don't read the documentation, it will be unclear that 
something is an `out` parameter, specially if you're reading a 
code that is not yours. Before using `out`, I found myself using 
more the address operator (&), because it made clear that the 
target variable would be initialized, but there is an even better 
and safer way to do that:

So, the best practice when using `out` parameters that I found 
right now and it becomes way clearer and can increase your 
performance if you're in a hot path is by void initializing your 
out variable, e.g:

```d

void initializeFloat(out float f){f = 0.0f;}
void main()
{
     float myFloat = void;
     initializeFloat(myFloat);
}

```

See that without the `void` initialization, all that rests is the 
function name which is quite readable `initializeFloat` so, 
something should happen to it. But there are cases which your 
naming won't be enough expressive to say that you're using an 
`out` parameter. This situation, void initialization (no 
initialization at all), is both faster and self documenting for 
your code, so, try to remember that feature, it took me some time 
to start using and I just remembered it existed when I was 
dealing with returning float vertices and there was so many that 
it appeared on my profiler.


More information about the Digitalmars-d-learn mailing list