📜 ⬆️ ⬇️

Distracted about input / output arguments

Once, a long time ago, when I was still learning to program in Pascal, I had to redo one of my own functions in the laboratory - I had to return not just a number, but a number and a string in the results. As it would be good, I thought then, if as a result it was possible to return a set of variables, as is done for the input parameters. I thought, sighed, and began to create a structure to transfer back to the calling function two parameters at a time.



The next time I thought about the input arguments when I first encountered the need to parse command line parameters in C / C ++. Well, remember this classic:


int main(int argc, char* argv[]) 

Only two input parameters of the function, but they contain all the possible combinations of CLI parameters of the program. Yes, I had to pay for the universality of the call with the complexity of parsing, but there were no options to solve the problem radically differently.


Next time I had to think about the input / output arguments when analyzing promises :


 get('story').then(fnSuccess1).then(fnSuccess2). ... .catch(fnError); 

In the promise chain, the output parameters of one function are passed to the input of the other (the first argument), modified and transmitted further, further, further ... It turned out that universalization was important for such a conveyor: one output argument — one input argument.


Further more. In JavaScript, there is no overloading of functions , so the following code doesn't hover there:


 function sayHi() { for (var i = 0; i < arguments.length; i++) { alert( ", " + arguments[i] ); } } sayHi("", ""); // ', ', ', ' 

After a long talk with the Java mistress, this code looks like a stunned hippy sixties next to a special forces officer on a combat mission - " This function has no arguments at all? So, relax, dude! Come on, I'll twist your arguments ... Now, take it, and no matter how much you give it there, you'll see everything . "


Moreover, in JavaScript, I have a function such as apply :


 fun.apply(thisArg[, argsArray]) 

the purpose of which is to apply the function in the specified context with the given arguments. Here, too, nobody cares how many input arguments the function itself (or its author) expects to see - ask as much as you want.


In my PHP practice, there was a case when in the third-party module, when the base method was overloaded, two arguments were reversed (the eighth and the doer, or even the ninth and the tenth are not important).


 public function doGood($a, $b, $c, $d, $e, $f, $g, %h, $i, $j) {} 

 public function doGood($a, $b, $c, $d, $e, $f, $g, %h, $j, $i) { parent::doGood($a, $b, $c, $d, $e, $f, $g, %h, $i, $j); } 

For simple cases, their values ​​were equal, so "the sum did not change from changing places." I had a more complicated configuration, so the bug got out (or rather, I snatched it with a debugger for a long time). After the debug dive, I somehow feel more warm or something, I look at constructions like:


 func(opts) 

or


 Response resp = service.call(req); 

By the way, the last construction I have strongly associated with Bdsm BPMS - was somehow an interesting experience linking intalio to a web application. Each process consisted of separate operations that were implemented by web services according to the scheme "y = f (x)", where x and y were reduced to XML documents (sometimes simple, sometimes not so much), and "programming mouse processes" to the mapping of elements Games of some services on the elements of X of other services.


Perhaps that is why in my current practice (PHP) I tried to reduce some groups of functions to the "y = f (x)" scheme. In JavaScript, this is done easily and pleasantly.


It was


 function showWarning(width, height, title, contents) { var width = width || 200; //    width,  width = 200 var height = height || 100; //   height,  height = 100 var title = title || ""; var contents = contents || " ."; //... } 

It became:


 function showWarning(opts) { var opts = opts || {}; //     var width = opts.width || 200; //    width,  width = 200 var height = opts.height || 100; //   height,  height = 100 var title = opts.title || ""; var contents = opts.contents|| " ."; //... } 

In PHP you have to pay by developing the structure of input / output arguments in separate classes. Basically it turns out a chore, but sometimes it's fun.


Why am I all this? And to the fact that now I don’t think that it would be good to return a set of parameters instead of a single " y ", but think about in which cases it would be good to have only one " x " at the input.


')

Source: https://habr.com/ru/post/321862/


All Articles