Перейти на главную страницу
Поиск по сайту

Too too much too many enough правило

Stack Overflow is a community of 4. Join them; it only takes a minute: Join the Stack Overflow community to: Ask programming questions Answer and help your peers Get recognized for your expertise Routines can have parameters, that's no news. You can define as many parameters as you may need, but too many of them will make your routine difficult to understand and maintain. Of course, you could use a structured variable as a workaround: putting all those variables in a single struct and passing it to the routine. In fact, using structures to simplify parameter lists is too too much too many enough правило of the techniques described by Steve McConnell in Code Complete. But as he says: Careful programmers avoid bundling data any more than is logically necessary. So if your routine has too many parameters or you use a struct to disguise a big parameter list, you're probably doing something wrong. That is, you're not keeping coupling loose. My question is, when can I consider a parameter list too big? I think that more than 5 parameters, are too many. What do you think? If this question can be reworded to fit the rules in theplease. When is something considered so obscene as to be something that can be regulated despite the 1st Amendment guarantee to free speech? According to Justice Potter Stewart, "I know it when I see it. I hate making hard and fast rules like this because the answer changes not only depending on the size and scope of your project, but I think it changes even down to the module level. Depending on what your method is doing, or what the class is supposed to represent, it's quite too too much too many enough правило that 2 arguments is too many too too much too many enough правило is a symptom of too much coupling. I would suggest that by asking the question in the first place, and qualifying your question as much as you did, that you really know all of this. The best solution here is not to rely on a hard and fast number, but instead look towards design reviews and code reviews among your peers to identify areas where you have low cohesion and tight coupling. Never be afraid to show your colleagues your work. If you are afraid to, that's probably the bigger sign that something is wrong with your code, and that you already know it. How would you reduce this? Would you want to reduce the number more to the point? I'm voting this up. I too too much too many enough правило amazed at all the other answers saying "3" and "4"! The correct answer is: the minimum necessary, which can sometimes be quite a few. Now you have only 8 parameters. It already has been redesigned. This is procedural style. Windows are objects, so this is obsolette now. Today, this would be solved with aggregated classes, not with bunch of parameters. I believe this is the case. Martin devoted four pages to the subject. Next comes one monadicfollowed closely by two dyadic. Three arguments triadic should be avoided where possible. More than three polyadic requires very special justification - and then shouldn't be used anyway. I doubt it includes "this" because that is the context of execution. Also, if you'd include "this" in the parameter list, then it would be impossible to have 0 params ideal. Half of takes more than 3 parameters, because just one iterator range is 2 already. It's just the nature of programming with iterators too too much too many enough правило. If algorithm and collection were to be redesigned, I would make algorithms always take a whole collection, and you'd get a way to slice a view of a collection to allow algorithms to work on parts; alternatively I'd also define a shorthand override to make it easy to work on the common case. Collections would be ranges, but not all ranges would be collections. And indeed, boost has done that already. Anyway, my point is that a massively widely used library ignores this advice, so the worst that's guaranteed to happen to you if you do too, is that many of your millions of users will tinker with minor simplifications to your interface ;- — Aug 14 '13 at 14:36 Some code I've worked with in the past used global variables just to avoid passing too many parameters around. Please don't do that! If you start having to mentally count off the parameters in the signature and match them to the call, then it is time to refactor! Thank you very much for all your answers: It was a bit surprising to find people who also think like I do that 5 parameters is a good limit for the sanity of the code. Generally, people tend to agree that a limit between 3 and 4 is good rule of thumb. This is reasonable as people usually have a bad time counting more than 4 things. Ason average people can keep more or less 7 things in their head at a time. Some people consider that a routine should have as many arguments as it needs to. I agree, but only for a few specific cases calls to OS APIs, routines where optimization is important, etc. I suggest to hide the complexity of these routines too too much too many enough правило adding a layer of abstraction just above these calls whenever possible. Nick has on this. If you don't want to read his comments, I summarize for you: in a nutshell, it depends: I hate making hard and fast rules like this because the answer changes not only depending on the size and scope of your project, but I think it changes even down to the module level. Depending on what your method is doing, or what the class is supposed too too much too many enough правило represent, it's quite possible that 2 arguments is too many and is a symptom of too much coupling. The moral here is don't be afraid of showing your code to your peers, discuss with them and try to "identify areas where you have low cohesion and tight coupling". Finally, I think wnoise much agrees with Nick, and concludes his satirical contribution with see comments below of the art too too much too many enough правило programming: Programming is not engineering. Organization of code is an art because it depends on human factors, which depend too much on context for any hard rule. This answer assumes an OO language. If you're not using one-skip this answer this is not quite a language-agnostic answer in other words. Look for groups of parameters that get passed into more than one method-even a group passed into two methods almost guarantees that you should have a new object there. Not all languages allow passing structures. Also, passing a structure means that the receiving method has to have the code to handle the structure and may, therefore, need more parameters. Programming is not engineering. Organization of code is an art because it depends on human factors, which depend too much on context for any hard rule. For mewhen the list crosses one line on my IDE, then it's one parameter too many. I want to see all the parameters in one line without breaking eye contact. But that's just my personal preference. If someone else has given classes ridiculously long names I wouldn't let that influence how I define or call my routines. Furthermore, there's no common link between too too much too many enough правило to justify bundling them. Maybe you could define "struct mmapargs", but that would worse. Seven things in short term memory? It's just a guideline, but in our shop that's what we try to stick to. I stop at three parameters as a general rule of thumb. Any more and it's time to pass an array of parameters or a configuration object instead, which also allows for future parameters to be added without changing the API. A length restriction on a parameter list is just one more restriction. And restriction means applied violence. It sounds funny, but you can be non-violent even when programming. Just let the code dictate the rules. And big code snippets usually can be refactored and split into smaller chunks. So you get solution against having many parameters as free bonus, since they are split among the smaller refactored pieces of code. A related question you should be considering is how the routine is. A large number of parameters may be a smell that's telling you that the routine itself is trying to do too much and hence it's cohesion is suspect. I agree that a hard too too much too many enough правило fast number of parameters is probably impossible but I would guess that a high cohesion routine would imply a low number of parameters. One thing I would point out from a performance perspective is that depending on how you pass parameters to a method, passing lots of parameters by value will slow the program down because each parameter has to be copied and then placed on the stack. Using a single class to encompass all of the parameters would work better because a single parameter passed by reference would be elegant and cleaner, and quicker! Incheck the second one, "Is this a constructor". My rule of thumb is that I need to be able to remember the parameters long enough to look at a call and tell what it does. So if I can't look at the method and then flip over to a call of a method and remember which parameter does what then there are too many. For me that equates to about 5, but I'm not that bright. Your mileage may vary. You can create an object with properties to hold the parameters and pass that in if you exceed whatever limit you set. See Martin Fowler's too too much too many enough правило and the chapter on making method calls simpler. According to me there could be cases where you will exceed 4 or some fixed number. Things to lookout could be Your Method is doing too much and you need to refactor. You might want to consider using a collection or some data structure. Rethink your class too too much too many enough правило, maybe some things do not need to be passed around. From an angle of ease of use or ease of reading code, I think when you need to kinda "word wrap" your method signature, that should make you stop and think,Unless you feel helpless and all efforts of making the signature smaller lead to no result. Some very good libraries in past and present use more than 4-5 prams. It heavily depends on the environment you're working in. Take for example javascript. In other systems the sweet spot will be at three or four. In the end, it all boils down to personal taste. I'll agree with 3 is okay, 4 is too many as a guideline. With more then 3 parameters, you are inevitably too too much too many enough правило more then one task. More then one tasks should be split into separate methods. However, if I looked at the latest project I've worked on, the exceptions would abound and most cases would be hard to get down to 3 parameters. If Too too much too many enough правило have 7-10 parameters in one routine I look at bundling them into a new class but not if that class would be nothing but a bunch of fields with getters and setters - the new class has to do something other than shuffle values in and out. Otherwise I'd rather put up with the long parameter list. I will bundle it with a data-only class if it is used in more than one place, but even then I too too much too many enough правило create both constructors. I like to use that principle with parameters. Assuming that programmers are all above-average intelligent people, I'd say too too much too many enough правило 10+ is too many. BTW, if parameters are similar in any way, I'd put them in a vector or list rather than a struct or class. If it is called a bunch of times per frame then I tend to make a structure and just pass a pointer to it since that tends to be faster assuming that too too much too many enough правило are not rebuilding the struct too too much too many enough правило time as well.


Другие статьи на тему:



 
Copyright © 2006-2016
avgplus.ru