News for hackers. Stuff that matters.
Re:Be wary of functions which take several parameters of the... by reifiedgent on Thursday September 26, 2019 @10:07PM (#21087772)
or C#:

CopyFile(src: "foo", dest: "bar");

Re:Be wary of functions which take several parameters of the... by gnud on Friday September 27, 2019 @03:34AM (#21089400)
c# already has the 'chained' API in the standard library:

    new FileInfo(srcPath).CopyTo(dstPath)
On the other hand, it also has the more error-prone

    File.Copy(sourceFileName,destFileName);
Re:Be wary of functions which take several parameters of the... by Lio on Friday September 27, 2019 @04:06AM (#21089557)
I know nothing about C# so please pardon my ignorance but how does:

  new FileInfo(srcPath).CopyTo(dstPath);
provide more protection vs

  CopyFile(src: srcPath, dest: dstPath);
Assuming that both path strings are set correctly isn't a typo here going to cause the same error and be just as visible?

e.g.

  new FileInfo(pathA).CopyTo(pathB);
  CopyFile(src: pathB, dest: pathA);
neither would seem to show which is correct without knowing what the path values are if the variable names are ambiguous.
Re:Be wary of functions which take several parameters of the... by gnud on Friday September 27, 2019 @07:03AM (#21090533)
When using the FileInfo api, it's obvious which argument is the source and which argument is the destination. When using the CopyFile, it's not.

If you can't keep track of your variable names, then using keyword arguments, like others here talk about, won't help much, either.

Re:Be wary of functions which take several parameters of the... by Lio on Friday September 27, 2019 @07:21AM (#21090637)
Sorry, not sure if your parent was edited to remove keynamed parameters from the second example or if I've responded to the wrong comment.

I'd definitely agree with you that the not having named arguments is worse than your method chained example.

To me named parameters seem to be on a par with the chained OO api.

Re:Be wary of functions which take several parameters of the... by gnud on Friday September 27, 2019 @08:27AM (#21091112)
Ah, the reason is simple: C# allows named parameters, but do not enforce them. You can call File.Copy both as

    File.Copy(a,b)
and

    File.Copy(sourceFileName: a, destFileName: b)
And confusingly, also as

    File.Copy(destFileName: b, sourceFileName: a)
Re:Be wary of functions which take several parameters of the... by Lio on Friday September 27, 2019 @10:51AM (#21092623)
OK I see. That explains it and also gives a very concrete reason for using the chained API. Thanks.
Re:Be wary of functions which take several parameters of the... by keithnz on Thursday September 26, 2019 @10:56PM (#21088006)
this doesn't really solve the problem, its a little better, however, as it STILL accepts positional arguments you can still get in trouble if you don't use parameter names.
Re:Be wary of functions which take several parameters of the... by alkonaut on Friday September 27, 2019 @04:08AM (#21089569)
A compile time analyzer that says "always use named parameters when calling functions with similar type of consecuitve arguments" should be pretty easy to write.

It would be better to have a foolproof API, but obviously it can't change now.