By default, to pass reference types using the ref keyword

I work with a developer who by default passes reference types such as StringBuilder, string and MemoryStream using the ref keyword. They do this regardless of whether they actually need to change the link.

public void ExampleMethod(ref MemoryStream ms) { byte b=ms.ReadByte(); ... // No changing of actual ms reference such as: ms=new MemoryStream(); } 

Almost always, methods simply use the object and return without changing the link. For immutable types, that is, strings, this is sometimes necessary, but why for mutable types?

For me, this is a bit of a β€œcode smell” in the sense that it can lead to a decrease in the number of supported code, since it is more permissive than what it really needs.

However, is this something serious enough that I can talk to the developer? My inner feeling is yes, but maybe it's too pedantic?

+4
source share
5 answers

Perhaps you could ask this developer why he does it ... Maybe he (erroneously) believes that he is more perfect? Maybe this developer is a C ++ developer who thinks that using ref is like using a pointer to C ++?

If it comes from the C ++ background, I would ask the developer why he does this, and explain to him that this is completely optional. This does not improve performance and that instead it gives different semantics to the method, since it allows you to change the link, and that you should use the ref keyword when it is really necessary for the method to change the link.

I do not think this is pedantic. Even an experienced developer does not know all the inputs and outputs of the new language that he uses. When you explain this to him, he will learn something new, and maybe he will be grateful. :)

+17
source

It is definitely worth the raise. I have seen this several times, and this has always been explained by the fact that the developers did not understand the type system, which is an important part of writing correct and supported code.

I would find an example where this is clearly not needed, but where they want to change the contents of the object itself - something that is added to StringBuilder would be ideal. Then just ask, politely, why did they decide to use the ref modifier. Indicate that it will work just as well.

Feel free to link to my article and my reference / value type article link.

+6
source

I do not think this is too pedantic, because the developer may have misunderstood what the keyword "ref" is suitable for. It should be used where absolutely necessary , indicating that the method will be guaranteed or, most likely, replace your link with something else - and not "just for fun."

Refresh
As for how to report this ... hmm, maybe maybe it depends on the motivation, why does he put ref on everything. If he thinks it will improve performance, run a small program using StopWatch.StartNew () and passing things into the method with and without ref . I have not tried, but I think that the difference in performance may lie in creating a new link, and this should be pretty darn little.

In addition, these functions should be used in the way they were intended, and express intention. When I see a method with ref or out parameters, I expect them to either completely change the passed instance, or the method will initialize the passed object accordingly. Not using these keywords for these purposes is just a way to confuse programmers, confusing method calls with unnecessary keywords and possibly setting up false expectations about the API used.

+4
source

It seems like a misunderstanding; that the ref keyword will be needed to pass method references. You should ask the developer if so.

The only good reason to use the ref keyword when this is not required is to allow the replacement of the method later to replace the object. This should only be used if you really expect that you will need it. Usually you should just code the current requirements, not all the possible requirements you can imagine.

If you find a ref that is not required for any reason, you should remove it from the code. Following the principle of encapsulation, the method should not be given more power over parameter values ​​than necessary.

Typically, the ref keyword is rarely used. In most cases, a more object-oriented approach may be used instead. It is sometimes used with value types for performance reasons, for example, in the Double.TryParse method, but it never has a reference type.

+3
source

The ref keyword is only useful when changing value types (everything that comes from ValueType ). Value types are things like int , byte , char , struct , float , etc. Using ref to refer to objects or classes, such as Stream descendants or StringBuilder , makes no sense and, of course, is the smell of code.

The only time I saw the need to pass a StringBuilder reference as ref was when sorting string values ​​in and out of Win32.

-one
source

All Articles