Why are the "out" options in .NET a bad idea?

Why are the "out" options in .NET a bad idea?

Recently I was asked about this, but I did not have any real answer, moreover, it just unnecessarily complicated the application. What other reasons exist?

+31
Sep 25 '08 at 15:38
source share
21 answers

Well, this is not a bad idea, I think. Dictionary<K, V> has a TryGetValue method, which is a very good example of why retrieval options are sometimes very nice.

You should not overuse this function, but it is not a bad idea for every definition. Especially not in C #, where you need to write the out keyword in a function declaration and call, which makes it obvious what is happening.

+54
Sep 25 '08 at 15:41
source share

This is a good idea. Because sometimes you just want to return several variables from one method, and you do not want to create a “heavy” class structure for this. The structure of the packaging class may obscure the way information is streamed.

I use this for:

  • Return key and IV data in one call
  • Divide the person’s name into different objects (first, second, last)
  • Split address
+16
Sep 25 '08 at 15:44
source share

If you care about writing reliable code, embracing immutability and eliminating side effects, then parameters are an absolutely terrible idea. This forces you to create mutable variables to handle them. (Not that C # supports readonly method level variables anyway (at least in the version I'm using is 3.5)).

Secondly, they reduce the composition of functions, forcing the developer to configure and process variables that receive output values. This is an annoying ceremony. You cannot just use expressions, using them with something similar to ease. Therefore, the code that calls these functions quickly turns into a large imperative mess, providing many places to hide errors.

+14
Nov 13 '12 at 18:23
source share

Out - A parameter is a bad idea, in my opinion. They increase the risk of side effects and are difficult to debug. The only good solution is the results of the function, and this is the problem: for the results of the function, you need to create a tuple or a new type for each complex result. Now this should be pretty easy in C #, with anonymous types and generics.

And by the way: I also hate side effects.

+7
Sep 25 '08 at 16:15
source share

The wording of the question "Have you stopped hitting your wife?" varieties , suggests that parameters should be a bad idea. There are times when options can be abused, but ...

  • They are really very good for C #. They are similar to ref parameters, except that the method is guaranteed to assign a value to it, and the caller does not need to initialize the variable.

  • Normal return values ​​for functions are pushed onto the stack where they are called and exited. When you specify the out parameter, you provide the method with a specific memory address to insert the result. It also allows the use of multiple return values, although using a structure often makes more sense.

  • They are great for the TryParse template, and also provide metadata for other things, for example, in the case of SQL-CLR, where parameters are passed outside the signature parameters of the stored procedure.

+5
Sep 26 '08 at 3:44
source share

I would say that your answer is in place; this is usually an unnecessary complication, and usually a simpler design. Most of the methods that you work with. NET do not change their input parameters, so using a one-time method using syntax is a bit confusing. The code becomes a little less intuitive, and in the case of a poorly documented method, we don’t know what the method does with the input parameter.

In addition, method signatures with missing parameters trigger a / FxCop code violation if this is the metric you care about. In most cases, there is a better way to achieve the goal of a method that uses the "out" parameter, and the method can be reorganized to return interesting information.

+4
Sep 25 '08 at 15:44
source share

It is not always a bad idea to use parameters. Usually, for code that tries to create an object based on some form of input, it is recommended to create a Try method with out and boolean return parameters, rather than forcing the consumer of the method to wrap try catch blocks all over, not to mention better performance. Example:

 bool TryGetSomeValue(out SomeValue value, [...]); 

this is a case where parameters are a good idea.

Another case is where you want to avoid the expensive large structure passing between the methods. For example:

 void CreateNullProjectionMatrix(out Matrix matrix); 

this version avoids costly structured copying.

But, unless required for a specific reason, this should be avoided.

+3
Sep 25 '08 at 15:48
source share

Well, there's no clear answer for this, but a simple use case for the out parameter would be "Int.TryParse (" 1 ", out myInt)"

The task of the method XXX.TrayParse is to convert a value of some type to another type, it returns you a logical flag indicating the success or failure of the conversion, which is one part, the other part is the converted value, which is carried by the out parameter in this method.

This TryParse method was introduced in .NET 2.0 to understand that XXX.Parse will be thrown if the conversion fails, and you should try try / catch around the statement.

So basically it depends on what your method is doing and what pending method calls are expected, if you execute a method that returns some form of response code, then the parameters can be used to execute the returned methods.

In any case, Microsoft says "Avoid using parameters", in this design guide check this page msdn http://msdn.microsoft.com/en-us/library/ms182131(VS.80).aspx

+2
Sep 25 '08 at 17:20
source share

I would say that my reason for avoiding this would be because it is easiest to get the process data from the process and return one logical part of the data. If he needs to return more data, he may be a candidate for further abstraction or formalization of the return value.

The only exception is if the returned data is somewhat tertiary, for example, nullool bool. It can be true / false or undefined. The out parameter can help solve a similar idea for other boolean types.

Another that I sometimes use is when you need to get information through an interface that does not allow you to use the "best" method. For example, using the .Net and ORM access libraries. When you need to return an updated graphic object (after UPDATE) or a new generated RDBMS identifier (INSERT), as well as the number of lines affected by the instruction. SQL returns all this in a single expression, so several method calls will not work, since your data level, library or ORM calls only one common INSERT / UPDATE command and cannot store values ​​for subsequent searches.

Ideal methods, such as never using dynamic parameter lists or parameters, are not always practical all the time. Again, think twice if the out parameter is really the right way to do this. If yes, then go. (But make sure it's good!)

+2
Sep 26 '08 at 3:15
source share

I would say that the answer you gave is about the best that is. You should always design from out parameters, if possible, because this usually makes the code unnecessarily complicated.

But this is true for any template. If this is not necessary, do not use it.

+1
Sep 25 '08 at 15:39
source share

I do not think they are. Misusing them is a bad idea, but this applies to any coding practice / technique.

+1
Sep 25 '08 at 15:45
source share

He likes how the guy Tanqueray loves to talk - everything in moderation.

I would definitely like to emphasize overuse, as it would lead to “writing c in C #” in much the same way that you can write java in Python (where you don't hug templates and idioms that make the new language special, but instead just thinking in your old language and moving on to the new syntax). However, as always, if this helps your code to be more elegant and make more sense, get out.

+1
Sep 25 '08 at 15:56
source share

I do not think there is a real reason, although I have not seen her use it often (except for P / Invoke calls).

0
Sep 25 '08 at 15:40
source share

The out keyword must be specified (see SortedDictionart.TryGetValue ). It implies passing by reference and also tells the compiler the following:

 int value; some_function (out value); 

ok and that some_function does not use a unified value, which is usually an error.

0
Sep 25 '08 at 15:43
source share

I think that in some scenarios this is a good idea because they allow you to return more than one object from a function, for example:

 DateTime NewDate = new DateTime(); if (DateTime.TryParse(UserInput, out NewDate) == false) { NewDate = DateTime.Now; } 

Very useful:)

0
Sep 25 '08 at 15:45
source share

I would agree with GateKiller. I can’t imagine that the parameters are too bad if Microsoft used them as part of the base library. But, like everyone else, it is best in moderation.

0
Sep 25 '08 at 15:53
source share

Some languages ​​outside of .NET use them as do-nothing macros, which simply give the programmer information about how parameters are used in the function.

0
Sep 25 '08 at 16:01
source share

One point that I don't see about is that the out keyword also requires the caller to point. This is important because it helps to ensure that the caller understands that calling this function will change its variables.

This is one of the reasons why I never liked using links as input parameters in C ++. The caller can easily call the method without knowing that its parameter will be changed.

0
Sep 25 '08 at 16:04
source share

' out ' great!

It makes the statement clear that the parameter contains the value returned by the method.

The compiler also forces you to initialize it (if we use it as a return parameter, it must be initialized).

0
Sep 25 '08 at 17:46
source share

I think the main reason is that "... although return values ​​are common and widely used, using out parameters correctly requires intermediate design and coding skills . Library architects who design for a wide audience should not expect users will handle workers with out or ref parameters.

Please read more at: http://msdn.microsoft.com/en-us/library/ms182131.aspx

0
Dec 14 '14 at 15:48
source share

They slightly destroy the semantics of the method / function. Usually a method should take a bunch of things and spit things out. At least this is how this happens in the minds of most programmers (I would think).

0
May 10 '17 at 23:07
source share



All Articles