Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why we should avoid Five argument from a function and how
we can do.
Can you please explain this stuff with a example.
Posted

1 solution

Hi,

In my opinion, 5 arguments for a function are still too much.
If you try to be a clean-code-developer, you should try to not use more than two arguments, otherwise your function is too less specific (or you should pack the arguments into an object).

The reason why you should not use more than x arguments is the readability.
If you see this function-call, can you imagine what the 4th argument is doing without looking at the destination function?

C#
CalculateResult(3.1f, 1.75f, CalcType.Multiplication, true);


Better would be something like this:
C#
CalculateResult(new Calculation { First = 3.1f, Second = 1.75f, Type = CalcType.Multiplication, RoundResultToInt = true });

or
C#
CalulateResultAndRoundToInt(3.1f, 1.75f, CalcType.Multiplication);


I know, the example is a bit crappy, but I hope you see the point :)

Hope this helps!

Best regards and happy (clean) coding,
Chris
 
Share this answer
 
v2
Comments
Sandeep Mewara 29-Jun-12 7:14am    
My 5!
Christoph Keller 29-Jun-12 7:52am    
Thanks! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900