Why optional parameters are bad
Prefix works with. With APM, server health metrics, and error log integration, improve your application performance with Stackify Retrace. Try your free two week trial today.
Click here to read more about the acquisition. Try Our Free Code Profiler. Try Our Code Profiler. By Role. By Technology. By Language. Documentation Support Ideas Portal Menu. Start Free Trial. Tip: Find application errors and performance problems instantly with Stackify Retrace. Troubleshooting and optimizing your code is easy with integrated errors, logs and code level performance insights.
About the Author Latest Posts. Get In Touch. Facebook Twitter Youtube Linkedin. What is APM? Subscribe to Stackify's Developer Things Newsletter. Sign Up Today. Start Your Free Trial Now. But there's one small trick: you can't do a default value for the 1st and 3rd arguments only.
Can I give a default value to parameters or optional parameters in C functions? Asked 5 Months ago Answers: 5 Viewed 41 times. Can I give default parameters in C? Add 10, 20, c ;. That is exactly how you do it in C , but the feature was first added in. NET 4. Where is the drawback? What about a placement new?
Ah, us clean coders! Such a good movement, but sometimes we tend to overdo things. But yes, that could be a code smell, especially if that one thing would be the conditional logic we talked about earlier. Does it still do more than one thing? Is that bad for your codebase? Just look at the first client example above and compare it to this one:. To conclude this argument, know your clients!
Daniel Olszewski, in his article titled Java 8 Optional Use Cases , argues that the case of Optional method parameters is much worse in the case of multiple such parameters. He does not provide any examples or reasoning for such state of things, just:. Let me augment those with some of my recent reasoning.
The data from your API is supposed to go from the controller, through application and domain services, down to the depths of your domain model. Obviously, you could just make this default value a public constant and pass it in the controller. When using optional parameters, it will show you all references; but you might be interested in just one particular "parameter-use" and would have to scan through the whole "Find Symbol Results"-List.
Sascha, Unfortunately, I did not find a single rational argument in these set of statements. I would like to touch only the last one, as the most obvious one: if you click "Find all references" on a method with optional parameters, it will still give you all references. But the definition reference will be only one! Isn't that better then more than one? You see, this is the point when you have been invited to replace your claims with some logical arguments not optional in this case For example, if you mention "why to take the risk?
And so on Sergey, I think you misunderstood my point about "Find All References" which might be my fault. For methods with optional parameters, "Find All References" will show all references, regardless how many and which parameters were explicitly specified. For overloads, it will show only the references of that particular overload.
Regarding my statement why take the risk [of optional parameters] if you don't have to? It's versioning issues, subsequent insertion of additional [optional] parameters "in the middle" and the dependency of default values on the type of reference the method is being called on. Okay, that's true and yes, I misunderstood, but not your point, but just did not take into account that only one signature will be shown , but how can it be considered as a benefit?
All, right, I understand now what you mean by risk, but I still don't think it's anything convincing. I see the benefit in that I'm often interested in a particular overload of a method when inspecting code.
When the "Find Symbol Results"-List is a screen tall or more because of optional parameters, I have to wade through all of it. I'm not saying the risks of optional parameters are so high that it would be irresponsible to use them. But on the flip side I don't see very impressive benefits of optional parameters. The boiler plate code of proxy-overloads can be visually hidden and the time required to write it is negligible in my opinion. Your argument that the insertion of a new parameter "in the middle" has the same effect for the overload-solution is correct, I have to admit :- But there remain two other arguments for overloads: Caller-reference-type-depending default values and another source for versioning issues see Richard's 2nd link.
All right, this reduced discussion to something close to the personal taste, and on more principle issues we understand each other.
Top Rated Most Recent. Accept Solution Reject Solution. They certainly won't produce the same IL - the overloads version will generate four methods, and the optional parameters version will only generate one. With the overloads version, it's easier to change the default value of a parameter. With optional parameters, the default value is "baked in" to the caller, so you would have to recompile everything for the new default to take effect.
Posted Apr am Richard Deeming. Good points and explanation, a 5. But I would be curious: don't you see some obvious benefits of optional parameters?
You never suggested any preferences Richard Deeming Apr pm. I can certainly see the benefits, and I've used optional parameters to greatly simplify some of my own code. You just have to be careful, particularly with the versioning issues. Very good. Having all that in mind, I still recommend the inquirer to consider using optional parameters. The biggest possible concern I had would be the need to down-grade code to older C versions not supporting the feature, but the inquirer stated it won't be a concern; please see the discussion of Solution 3.
In this case I would vote for optional parameters because what you are doing seems "orthogonal" to me: there's no significant variation. You could also consider using Enumerable.
0コメント