views:

1393

answers:

20

Title says it all... we've debated this without any progress.

Closest related questions I could find:

+9  A: 

Ask him try to setup a unit testing harness for it where HE has to pass in all of the real/mock objects and account for every possible testing scenario.

whaley
A: 

There could be a few ways to do this.

  1. Ask someone else to try to make heads or tails of it. A kind of hallway test for the code.
  2. Wait three months and then see if this person can still use the method (at 26 parameters, they're probably going to require some time to figure out what does what and why).
  3. Is there any way the parameters can be vectorized? Are they all of different types, or all ints? Because if they can be vectorized, would a vector (or a dictionary/hashmap) be such a bad compromise?
mmr
+3  A: 

Ask him, what happens if one data passed as parameter becomes obsolete in the future and have to be deleted from the parameter-list. Who will change all calls to this method, especially if the parameter was in the middle of this list?

Mnementh
My IDE does that for me automatically... doesn't yours? It renames, adds parameters in any position with or without default values, removes parameters, etc. And it can save the operation history so that you can apply the same change to a disconencted codebase, in case you can't refactor all the code at once. (I program in Eclipse).
Mr. Shiny and New
come on Mr. Shiny, its one of the best arguments. many people dont know how to use refactoring in eclipse(really, i saw many people changing stuff by hand).
01
Even if you use the refactoring-features of your IDE. What if the code is us4ed in another project. They take the new source, and their stuff is broken - in a hard to change way.
Mnementh
IDE auto-refactoring may remove all occurrences of the parameter from calling methods, but does it remove the variables themselves? In other words, if you have "int x = 10; somefunc(x);" then the first statement is unnecessary overhead once the call is changed to "sonefunc()".
DisgruntledGoat
The other problem is what if you have some code not under source control yet? Like some kind of template file or whatever, or *gasp* code in comments...
DisgruntledGoat
Anything that impedes refactoring, automated or not, is not a good thing. My compiler emits warnings about unused fields, and I take them seriously. I'd rather have warnings to clean up than broken code that no longer compiles (as when you just change the constructor manually).
Mark Renouf
+3  A: 

Ask him to describe the API in his own words. The parameter order, the meaning of all the parameters.

Look for past bugs that were caused by API users failing to grasp this monstrosity. Maybe some bugs were caused by invalid parameters (True instead of False as parameter 13, which is someImportantFlag).

Tadeusz A. Kadłubowski
A: 

It all comes down to API confusion ... imagine if you were the developer that had to call a method with 26 parameters. What if the method throws an exception, is there a problem with the 9th argument or the 23rd?

The API should be simpler than that

Joel Martinez
A: 

I'd suggest just trying to demonstrate that if they're creating methods with that many parameters, chances are they really should be breaking it down into much smaller methods, which results in code that is, overall, far more maintainable and readable. Sure, you'll end up with a lot of small methods, but modern IDEs have easy-to-navigate method trees for a reason.

HappyCodeMonkey
+29  A: 

You don't... you have him explain why it's NOT bad practice.

Listen to his points and tell him the reasons you would do it another way.

Robin Day
If you can't compel someone to stop writing 26-parameter methods, surely you're not going to have much more luck compelling them to explain their use beyond "the method needs 26 arguments".
Kylotan
You're also not going to have much luck saying, "26 parameters is bad". As with most "bad practices", I'm sure there was a reason for doing it. Listen to those reasons, then you can counter each one until a better solution can be found. If you can't counter each one with anything more than "it's bad" then you're not going to have much luck convincing anyone.
Robin Day
There can be legitimate reasons to have "a lot" of parameters: Construction of a complex object, creation of a new database record, parameters for new machine installation, new user account creation. The best approach is a discussion about why he made this particular trade off, and what some alternatives might be.
semiuseless
+3  A: 

By showing your coworker a better way and explaining why it is better. I hope you don't think I'm being facetious, but with no further information from you I find it difficult to diagnose exactly why 26 parameters are bad for your situation. Rarely, I admit, but I have written functions and subroutines with many many arguments when that was the best design choice.

Regards

Mark Westwood

High Performance Mark
A: 

I would suggest to him that it is likely that there is a concept/domain entity/other abstraction that is being passed to this function, and that should be explicitly represented as an object. Passing the object is simpler, and allows things such as validation of the object, helper functions, etc. be attached to the class.

Besides, it is better (more maintainable/better organized/simpler) to explicitly represent these objects.

I bet if you look at the examples of 26-parameter functions, that you can come up with some concept/entity to represent what is really being passed.

Larry Watanabe
A: 

Maybe try to read some literature on the subject together, at my company we read this book:

http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882

and had a session where all the developers sat down and had a talk about how to work together.

It helped in bringing the different developer cultures together.

Alexander Kjäll
+3  A: 

You might find this of interest:

Barry Gallagher
Thanks for the link, I've added it to the question so people can find it.
Mark Renouf
A: 

Are there any metrics you can run that show the amount of calls to the said method? (Just wondering whether it's a question of being too generic a method, that should probably be broken down.)

Hmm... I'd love to see the docblock for that one. :-)

middaparka
Unfortunately, there are no @param tags in that JavaDoc comment.
Mark Renouf
+1  A: 

Wait for a lot of people to reply to this post, and then give him a link to it. I guarantee he won't be able to argue with that; and if he does, it's not worth your time.

Tres
+1 You stole my idea.
Lieven
A: 

Well, 26 parameters or not, it's very debatable if it's a good idea or not. Anyways, I've seen someone deal with this problem by replacing the 26 parameters with a single structure that contained the 26 parameters. Actually, the Windows API has a few functions that have quite a few parameters, although 26 is quite a lot. (But several are between 10 and 16 parameters.)

So explain to him that 26 parameters are bad and he'll replace them with random structures with 26 fields each...

Workshop Alex
I don't see how that's any improvement? I think maybe the fields could be grouped into some relevant sub-structures. Maybe the original function down to 5 or so parameters of these.
Mark Renouf
sarcasm is often lost in the literal-mindedness that programmers share; +1 to counter
Steven A. Lowe
@Mark, it's definitely NOT an improvement. But from my own experience, it is what will happen when you explain someone who is this inexperienced.
Workshop Alex
+3  A: 

What is the size of the method body? I would guess that with that many parameters, the method body is big, complex, and thus, bug-prone. It is difficult to argue that big methods are easy to maintain.

How cohesive is a method with 26 parameters? Even if you could put all those parameters in one class, would you still access 26 different fields in a method? Hint: the method is probably doing too much.

Barthelemy
A: 

Corporate Coding standards are very convincing usually. We use structures when it is necessary to pass such a lot parameters.

Kirill V. Lyadvinsky
A: 

Show him Atwood's Code Smells. Nuff Said.

If that doesnt work. show your colleague A Taxonomy for "Bad Code Smells"

MikeJ
+19  A: 

Methods with lots of parameters are not necessarily bad. It depends on what the parameters are, for instance. I maintain code with some methods that take dozens of parameters, because those parameters represent scalar values in an object, and the object's creation is the purpose of this method.

You could reorganize the parameters if you wanted; you could put them all in a map, but then there's no way to be sure that they're all present. You could create the object manually, but then you either have a constructor with 26 parameters or you have 26 setXXXX method calls. If your object requires all those parameters to be complete, I'd say you're stuck with the constructor approach or the factory method. Otherwise you are relying on the user setting up the object correctly every time.

What's better?

createTransation(date, username, transAmt, currency, accountNum, resultCode, ...);

Transaction trans = new Transaction(date, username, tranasAmt, currency, ...);
createTransaction(trans);

Transaction trans = new Transaction();
trans.setDate(date);
trans.setUsername(username);
// etc
createTransaction(trans);

Now, if your method has a handful of data parameters and a handful of control parameters, such as flags to chose some mode of operation, then it becomes problematic to use these methods, and you should explain to him that he should keep his code simpler and more focused. A method with lots of control parameters probably has a complex control path as well, and might be difficult to maintain. However, it's common for a single entry point to a complicated process to have lots of arguments; consider the command line for your compiler: you might have dozens of flags easily. But in this case it should be possible to group the options into a single structure; this makes it easier to determine which options must be specified, which have default behaviour, etc.

Transaction trans = new Transaction(date, username, tranasAmt, currency, ...);
Options opts = new Options(DEFERRED, VALIDATE, STORE);
createTransaction(trans, opts);

The original question is rather vague but it's important to remember that sometimes code complexity is warranted, or at least not wrong.

Mr. Shiny and New
Excellent counterpoint. This is certainly valid an in some cases true of this particular code, but not all of them. Some methods have a lot of parameters but these are not necessarily simple data points but other objects with behaviors, etc. I'm very interested in others' view on this answer.
Mark Renouf
As I said, the Windows API also has several functions with a dozen or so parameters, which are used to construct new objects like windows or processes. It is useful to group related data together in structures but even then you could still need lots of parameters to create some new object.
Workshop Alex
Without entering into a deep discussion, I don't think Windows API is a good example of quality code. So this is not argument:)
Kamarey
+1 If the foo that is being done really does need 26 parameters, then 26 parameters have to be provided. I would much rather the parameters were passed to a function than they were made global.
semiuseless
Well, any discussion about the quality of Windows is like comparing VHS with BetaMax. Of course, BetaMax was much better but it just wasn't popular. The Windows API might not be the best example, but it is very popular. (Then again, most sane developers write simplified wrappers around such complex code.)
Workshop Alex
+1 - It's spurious to say "it's bad because there are 26." Though it's often/usually the case that "many parameters = bad," one cannot make a blanket statement
DarkSquid
+9  A: 

It Depends

It depends on what the parameters are, and what they mean.

While 26 parameters seems offhand to be excessive, it may be acceptable in some cases (DAL methods that map to stored procedures come to mind).

The semantics of the parameters are more important than their number - look at the parameter list and ask "if this was a database table's columns, would I need to take projections to normalize it?". Also ask "if this was a database table's columns, would they make sense as attributes of a single entity?"

In many cases, the presence of excess parameters indicates missing classes in the design.

Steven A. Lowe
The question is not about "Is it good or bad 26 params", but "How to explain...". There are some difference....
Kamarey
But to explain why it's bad, you're assuming that it is bad while there are situations where it's not that bad. Without knowing the purpose of this function it would make no sense claiming it's bad because in the context where it's used, it might just be the best alternative.
Workshop Alex
Agree, but debating here if it's good or bad makes this question another duplicate one to already existing.
Kamarey
True, but without knowing the full context of where this method is used and what it's supposed to do, we just can't blindly assume it must be bad. And I've learned to not assume things, so I won't assume it's used in a bad way, although it's likely to be bad since it got another developer upset. :-) General rule: if your code upsets a more experienced developer then your code is very likely bad...
Workshop Alex
Another point, somewhere you may need 26 params to a method or constructor. Fine, but it becomes a smell when you see that same set of parameters transfered through more than one method, or that method is invoked in from multiple locations throughout the code.
Mark Renouf
+3  A: 

Make him write the unit tests.

Stephan Eggermont