Timeline for Clean Code: Functions with few parameters [closed]
Current License: CC BY-SA 3.0
47 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jan 3, 2020 at 9:35 | review | Reopen votes | |||
| Jan 8, 2020 at 18:02 | |||||
| Aug 4, 2017 at 12:10 | review | Reopen votes | |||
| Aug 15, 2017 at 14:44 | |||||
| Jul 25, 2017 at 11:46 | comment | added | Mafii | @JaredSmith also additionally, modern lower level languages like rust support macros to fully eliminate method call overhead when used. | |
| Jul 16, 2017 at 23:53 | history | closed | gnat Thomas Owens♦ | Needs more focus | |
| Jul 16, 2017 at 23:53 | comment | added | Thorbjørn Ravn Andersen | @NathanCooper And Address would have a builder. | |
| Jul 16, 2017 at 12:35 | answer | added | jleach | timeline score: 0 | |
| Jul 16, 2017 at 8:06 | answer | added | Martin Maat | timeline score: 1 | |
| S Jul 16, 2017 at 6:23 | history | suggested | BlueWizard | CC BY-SA 3.0 | mentioning book directly |
| Jul 15, 2017 at 22:19 | review | Suggested edits | |||
| S Jul 16, 2017 at 6:23 | |||||
| Jul 15, 2017 at 16:29 | comment | added | Mark Rogers | I usually allow 3 parameters but after that I consider merging parameters into a new parent object created for that purpose. The only exception is dependency injection, which I might allow to go larger in some languages like JavaScript with RequireJs or angular. | |
| Jul 14, 2017 at 8:48 | answer | added | Borjab | timeline score: 0 | |
| Jul 14, 2017 at 8:40 | comment | added | TobiMcNamobi | I suggest reading Michael Feathers great book "Working Effectively with Legacy Code", too. "Clean Code" tells you what clean code is and why you want it. "Working Effectively ..." tells you how to get there from each and every possible situation of bad code. | |
| Jul 14, 2017 at 7:19 | answer | added | CJ Dennis | timeline score: 1 | |
| Jul 14, 2017 at 6:16 | comment | added | CJ Dennis | Reread pages 40-43 in Chapter 3. "When a function seems to need more than two or three arguments, it is likely that some of those arguments ought to be wrapped into a class of their own." | |
| Jul 14, 2017 at 4:48 | history | protected | gnat | ||
| Jul 14, 2017 at 2:44 | answer | added | candied_orange | timeline score: 7 | |
| Jul 13, 2017 at 22:16 | comment | added | Ed Plunkett | This global thing is a perfect example of why inexperienced programmers probably shouldn't read stuff like that. Global "parameters" are a monstrous, nightmarish evil. An extra function parameter or two may not be a great idea, but it's not within two orders of magnitude of the awfulness of passing parameters via globals. The latter approaches professional malpractice. It's BAD. You need a fair amount of horse sense borne of hard-knocks coding experience before these grand statements about programming style can be of any value to you. A noob risks swallowing horses to catch flies. | |
| Jul 13, 2017 at 21:48 | comment | added | Nat | Global parameters are like nuclear weapons; they're wretched and only to be used as an absolute last resort. If you're truly stuck, static singletons are a lesser evil. | |
| Jul 13, 2017 at 20:59 | comment | added | user22815 | If you have too many parameters to a function, odds are some of them are related and should be grouped into an object which then becomes a single parameter encapsulating multiple pieces of data. There is a third option here. | |
| Jul 13, 2017 at 19:45 | comment | added | Darren Ringer | I can see where someone who is a hardcore evangelist of the OOP paradigm would suggest that any time a function has more than 3 parameters, you should think of a new abstraction model that lets you pass a smaller number of more detailed objects or else rework the design to avoid such a call in the first place. In the real world everything is about compromise, so you'll always have to weigh the cost of adding an extra parameter or two against the cost of all that extra refactoring. One thing on which I do agree with all parties, though, is not to resort to using globals. | |
| Jul 13, 2017 at 16:38 | comment | added | Ant P | If you have large amounts of parameters to functions you likely have a composition/abstraction problem. Introducing global variables is just fixing the symptom. | |
| Jul 13, 2017 at 16:26 | comment | added | sdenham | This shows what is wrong with this sort of judgement-free rule: it opens the door to judgement-free 'solutions'. If a function has many arguments, it may indicate a suboptimal design, usually not just of the function, but of the context in which it it is being used. The solution (if one is needed) is to seek to refactor the code. I cannot give you a simple, general, judgement-free rule on how to do it, but that does not mean that 'no more than N arguments' is a good rule. | |
| Jul 13, 2017 at 16:04 | comment | added | Jared Smith | No more than n parameters is a rule of thumb (for any value of n), not etched on a diamond. Don't mistake good advice for a mandate. Lots of parameters is generally a code smell that you've got too much going on in one function/method. People used to avoid splitting into multiple functions to dodge the added overhead of the extra calls. Few applications are this perf-intensive anymore and a profiler can tell you when and where you need to avoid extra calls. | |
| Jul 13, 2017 at 15:55 | comment | added | Aaron | @charlie_pl in some cases you can use composition to mitigate that (although it wouldn't work well with Nathan's example). In some other cases you can't abstract much more and I guess you have to accept that you are working with a complex idea which will lead to complex code. | |
| Jul 13, 2017 at 13:59 | comment | added | Rémi | Anyway later he will also talk about making dependencies explicit vs implicit (global) so no don't do this | |
| Jul 13, 2017 at 13:53 | comment | added | Izkata | @NathanCooper Great example; an object could possibly manage to handle at least some of these variations | |
| Jul 13, 2017 at 11:38 | answer | added | JacquesB | timeline score: 38 | |
| Jul 13, 2017 at 11:33 | answer | added | Dan Levy | timeline score: -1 | |
| Jul 13, 2017 at 10:42 | comment | added | charlie_pl | @Nathan Cooper I agree that this is better, but you still have constructor of Address class, that has same "smelly" number of parameters. | |
| Jul 13, 2017 at 10:33 | comment | added | Doc Brown | @nashwan: for me the question looks like being based on the very wrong assumption Uncle Bob suggests to use global variables to reduce function parameters, and most answers here take that wrong assumption for granted. | |
| Jul 13, 2017 at 10:26 | comment | added | bytedev | @DocBrown I took the question to mean something more like Uncle Bob says dont use more than 3 params so I get round that problem by using global variables right? :-) I think likely author does not know there are better ways to get round this problem - like mentioned in the answers below. | |
| Jul 13, 2017 at 10:01 | answer | added | bytedev | timeline score: 2 | |
| Jul 13, 2017 at 9:10 | comment | added | Doc Brown | @OiciTrap: do you question the use of global variables, or Robert Martin's suggestions for using less than 3 or 4 parameters for a function, as written in the "Clean Code" book? These are two different things, see my answer below. | |
| Jul 13, 2017 at 8:31 | answer | added | Quentin | timeline score: 58 | |
| Jul 13, 2017 at 7:56 | answer | added | Timothy Truckle | timeline score: 1 | |
| Jul 13, 2017 at 7:18 | comment | added | Nathan | I don't think the alternative would be globals, but instead consolidating arguments into objects. It's probably more of a suggestion that postLetter(string country, string town, string postcode, string streetAddress, int appartmentNumber, string careOf) is a smelly version of postLetter(Address address). Continue reading the book, it hopefully will say something like that. | |
| Jul 13, 2017 at 6:46 | vote | accept | OiciTrap | ||
| Jul 13, 2017 at 6:45 | review | Close votes | |||
| Jul 16, 2017 at 23:55 | |||||
| Jul 13, 2017 at 6:29 | comment | added | gnat | Possible duplicate of Parametrize methods vs global variables | |
| Jul 13, 2017 at 6:14 | answer | added | Narender Parmar | timeline score: 1 | |
| Jul 13, 2017 at 5:41 | answer | added | Doc Brown | timeline score: 35 | |
| Jul 13, 2017 at 5:31 | answer | added | robert bristow-johnson | timeline score: 69 | |
| Jul 13, 2017 at 5:04 | answer | added | Dimos | timeline score: 4 | |
| Jul 13, 2017 at 3:47 | answer | added | Samuel | timeline score: 118 | |
| Jul 13, 2017 at 3:40 | history | edited | OiciTrap | CC BY-SA 3.0 | edited tags |
| Jul 13, 2017 at 3:34 | history | edited | OiciTrap | CC BY-SA 3.0 | edited tags |
| Jul 13, 2017 at 3:27 | history | asked | OiciTrap | CC BY-SA 3.0 |