Skip to main content
24 events
when toggle format what by license comment
Jul 17, 2017 at 12:12 comment added sdenham My apologies for having contributed to a misinterpretation of your answer. I joined in this thread in response to Gillifirca's comment, which seems to provide a justification for a practice that would only superficially (if that) conform to your answer. (I may also be misinterpreting the intent of that comment; it is hard to tell from one word.)
Jul 16, 2017 at 18:55 comment added robert bristow-johnson no. it's not meaningless, nor have i advocated "just wrapping up arguments". you may have missed the reference to SRP.
Jul 16, 2017 at 18:12 comment added Martin Maat This is a meaningless answer, not addressing the issue. Just wrapping up arguments does not fix anything.
Jul 16, 2017 at 18:09 comment added robert bristow-johnson BTW @sdenham, i have not suggested putting unrelated variables in a common struct, just to send it to a function. and then Paul is right in askng "But then WTF is your function doing with ten unrelated pieces of data?" modular design and the SRP applied to structs means that there are only related variables placed into a struct and that stuct have an overall singular purpose or function or role and these variables are there in support of that singular purpose or function or role.
Jul 16, 2017 at 0:56 comment added sdenham @PaulDraper See my comment under the question. The response 'too many arguments? Then put them in a struct' is exactly the sort of judgment-free 'solution' that judgment-free rules can lead to - superficially, it brings the code into conformance with the rule, but stuffing arguments into a struct does not create any relatedness that was not there before. The correct solution is to refactor the design.
Jul 16, 2017 at 0:10 comment added sdenham @PaulDraper Precisely - the point is that putting them in a struct does not make it any better. It does not fix the underlying design problem.
Jul 15, 2017 at 23:45 comment added jamesdlin And in some cases, if you want to add an extra parameter to a function, you can't because they're part of an API and you want to retain backward compatibility. For example, many Win32 ...Ex functions take a pointer to a struct to allow for such extensibility.
Jul 15, 2017 at 23:44 comment added jamesdlin @Ruslan The point of passing tens of parameters in a struct is to pass along state instead of passing tens of parameters as global variables. When you pass them as a struct (which is usually referred to as a context), then it's much easier to add additional state variables without having to modify many different functions.
Jul 14, 2017 at 5:16 comment added TankorSmash I think this answer would benefit from some explanation why the answer because without it, it's effectively boiled down to 'avoid globals like the plague'. This answer offers a solution to the problem the answerer put forward but not the question OP is asking, I think. Doesn't make it an unhelpful answer though.
Jul 13, 2017 at 23:42 comment added Paul Draper @sdenham, that is true. But then WTF is your function doing with ten unrelated pieces of data? Too much.
Jul 13, 2017 at 20:56 comment added robert bristow-johnson no, it's more like i'm saying "stay the hell outa the poison ivy."
Jul 13, 2017 at 20:55 comment added Kenneth K. You're missing the point. I understand why to not use them; the OP may not. If I told you to stop eating fruits and vegetables, would you do it simply because I told you to, or would you want to know my reasoning as to why?
Jul 13, 2017 at 20:55 comment added robert bristow-johnson for instance, @KennethK., using the C paradigm, i can see using static const for unchanging, constant "variables" used in a function like double exp(double x); which would be the Maclaurin or finite power series coefficients. they will be the same no matter who calls exp() and they will occupy the same space as globals, but no one may write to them. but global variables are useless if you want your functions to be callable from multiple contexts. it's the arguments passed to the function that set up the context and that may be different for different function calls.
Jul 13, 2017 at 20:49 comment added robert bristow-johnson @KennethK., i might suggest asking a question to this SE or to the Stack Overflow crowd about "Why are global variables (other than 'System Globals') a problem for clean, bug free, and reusable code?"
Jul 13, 2017 at 20:44 comment added Kenneth K. You can't just say, "avoid global variables" without giving a reason why. I mean, you can, but why should I believe you?
S Jul 13, 2017 at 19:44 history suggested CommunityBot CC BY-SA 3.0
Capitalization
Jul 13, 2017 at 18:43 review Suggested edits
S Jul 13, 2017 at 19:44
Jul 13, 2017 at 16:36 comment added robert bristow-johnson i totally agree @sdenham. SRP applies to structs also.
Jul 13, 2017 at 16:17 comment added sdenham @abuzittingillifirca You do not get cohesion automatically. If the only justification for putting parameters in a struct is to pass them to a particular function, then the cohesion is probably illusory.
Jul 13, 2017 at 12:22 comment added Andrew Dodds That's fine, but you have to ensure coherence in the Structs - make sure that the parameters grouped into a Struct are 'related'. You might use more than one Struct under some circumstances.
Jul 13, 2017 at 12:20 comment added Hans Olsson And you are more likely to refactor the function into smaller functions, since you can just pass along one parameter to the sub-functions instead of tens of parameters. And less risk of mixing up the parameters if you use positional arguments.
Jul 13, 2017 at 8:26 comment added abuzittin gillifirca @Ruslan Cohesion.
Jul 13, 2017 at 8:03 comment added Ruslan What is the principal difference between passing tens of parameters hidden in a struct and passing them explicitly?
Jul 13, 2017 at 5:31 history answered robert bristow-johnson CC BY-SA 3.0