3 ms·
The functional optional pattern thing seems fine for constructors, but I wouldn't want to have to implement it for every single function. A lot of the times it
by milch 2y ago
The functional optional pattern thing seems fine for constructors, but I wouldn't want to have to implement it for every single function. A lot of the times it is just about readability at the call site, e.g. (using the author's example)
string.Replace(urlString, " ", "%20", -1)
is less readable than
string.Replace(urlString, replace: " ", with: "%20", maxReplacements: -1)
Of course it's not often necessary, especially when you have a named variable
string.Replace(urlString, charToReplace, escapedChar, REPLACE_ALL)
But it can also be awkward to have to declare a variable for every arg, which is probably why python allows you to call it both ways. Plus the variable names and positional arg could not match up, which can be a source of hard to find bugs
- deleted 2y ago[deleted]
- cratermoon 2y agoI don't quite get the strings.Replace example when there's strings.ReplaceAll
- kokada 2y agoAuthor here. The fact that `strings.ReplaceAll` exists I think is a good argument for having default arguments, because I can't think of a good reason why we need a separate function for this case. But maybe it was a bad example. I remember that there is one function in `strings` that I almost always use the same value in every call I do because it is the correct value in 80% of the cases. I thought it was `strings.Replace`, but probably it is something else. If I ever remember which function it is, I will update the post.