If a parameter isn't used, what should I pass? - The Old New Thing
If a parameter isn't used, what should I pass? - The Old New Thing
It doesn't matter what you pass, but if you have to ask, then just pass zero.
If a parameter isn't used, what should I pass? - The Old New Thing
It doesn't matter what you pass, but if you have to ask, then just pass zero.
You're viewing a single thread.
I am a sky-high developer and know little of such low-level APIs. Please humor my ignorance: Isnโt it bad practice to write a god-method that sometimes uses these parameters, sometimes others? Isnโt this better refactored into multiple dedicated functions?
The answer, as with everything in software development, is that it depends.
A god method with 100 optional params that is usually bad practice. But a common pattern is to allow for an options object to be passed, and that object may contain 0-n supported parameters. This pattern is used everywhere, see graphql as a widely used library that is based on this.
Totes this. I've refactored far too many of these things in my day so I just about always reach for an options object if there's the slightest uncertainty about adding more params in the future.
I'm just guessing, but what about backwards compatibility? Or cross-system compatibility?
For example, something like a syscall that's existed for 20 years. Changing it would break old apps.
Of course you could just keep the now "old" syscall and add new methods that replicate it's behavior, but haven't you then introduced bloat? More ways to do the same thing, meaning (eventually) more bugs, more fragmentation, memory usage, etc.
Let's say you currently have an overloaded "god method" that is used for twenty different things. The proper thing to do would be to create twenty different interfaces that each do one specific thing and then call the syscall behind the scenes. That allows you to update and modernize your apps, allows for better testing of specific use cases, etc.
The original exposed syscall is deprecated and then eventually you force all the reliant code to adopt the proper new interface. The original overloaded code is still there, but locked up in a black box where no one has to worry about it.
Yeah, but people don't like change, and I'd expect low level engineers to like it even less.
And looking at Linux, that shit still supports ancient hardware, being able to actually get rid of old code (that now has to be maintained alongside the new code) is gonna be a PITA.
And then those methods grow and grow, or stop making sense, or start meaning something else, and you would have to go through the same abstract-deprecate-remove again. Rinse and repeat and if you do this regularly enough you have web development where you get your feet swept from under you every couple of years.
It's a bit of a pick your poison situation, for me the backwards compatibility path is the right call here though.
I know where you're coming from here. All I can say is there is something else wrong in this case and this is how it's being expressed. I've seen it several times myself and sometimes no amount of good coding can fix bad architecture.
But I will say that if the twenty use cases all grow to the point of needing their own abstraction, I think you'd be damn glad there was at least a point of separation instead of having to maintain all possible permutations in a single method signature.
Obviously taken to an extreme it's bad, but I think it's fine to have a function that can do one thing two or more different ways and ignore a certain parameter if one of the ways doesn't need it. I've done some programming against the Win32 API and this is what jumped to mind for me, and I think it's the typical case here. If I were designing from scratch I might split it into n functions that do it one way, but it's such a small difference I wouldn't fret over it. And of course making a change to the Windows API is an undertaking, probably not worth it in most cases.
Yes, but with things like syscalls it's easier to do this than require every high-level thing building on the syscall to be modified and recompiled. Very few people need to use such low-level APIs.