Hacker News new | ask | show | jobs
by schrototo 3798 days ago
I know exactly what the first one does, without knowledge of the underlying API. I have no idea what the second one does. Does it replace occurrences of the first parameter with the second one, or vice versa? I assume it returns a copy of the string? What do I need to do to perform a case insensitive search? etc.
6 comments

Do you also pronounce every abbreviation? Do you say "International Business Machines" instead of "IBM"? How about "Répondez s'il vous plaît" vs "RSVP". "As soon as possible" or "ASAP". Ever use a contraction? How about a keyboard shortcut?

I certainly agree it's potentially more readable at first. I'm not yet convinced, without actual data, that anyone, given a month or so experience with a given and common API wouldn't be more productive with shorter names up to a point. I would be good to get that data. Maybe I'd be better off with

    a = Math.numberFromAddtionOf2Numbers(c, d)
    e = Math.numberFromDivisionOf2Numbers(numerator: b, denominator, c);
vs

    a = c + b;
    e = b / c;
which is best

    s = str.stringByReplacingOccurrencesOfString(" ", withString: "", options: .LiteralSearch, range: nil)
    s = str.replace(" ", "");
    s = str.r(" ", "");
I'd do

    s = str.replacing(" ", with: " ")
which seems to go along well with the "sorted" and "appending" mentioned in the new API design guidelines:

https://swift.org/documentation/api-design-guidelines/#be-gr...

This is a bit more verbose if you need to chain a lot of replacements... but chances are you shouldn't be doing that in the first place! Instead there should be a version that takes a dictionary, like

    str.replacing(["<": "&lt;", "&": "&amp;"])
This is better because most of the time you don't actually want characters produced by earlier replacements to match later ones, and it can be faster for large strings.
That's a true statement. Yet I still think the second way is superior.

There's a steeper learning curve sure. But brevity has value.

Is grep a stupid ass name that makes no sense to the layman? Of course! Can even moderately complicated regular expressions actually be read by anyone after the fact? Hell no. And yet it's a fantastic tool.

If there were a dynamic toggle between verbose and brief newbies would start with verbose. I bet most users would eventually switch to brief. Not all, but most.

String.replace is now a builtin standard in most languages, there is no real need to explain it anymore (apart from documentation). It would be like replacing 'grep' with 'fetchAPieceOfStringInFiles'
Agreed, and also: the insane long version is just str-<Complete> and a couple arrow taps and it auto-completes.

So it's just as short, from a typing perspective. With a lot of free bonus clarity.

I lived through the bad years when Apple's dev tools were half-baked and made you almost pine for the first dot-com bubble when you were writing Java (but in IDEA IntelliJ, which made it almost worth it).

But today Xcode is state-of-the-art, at least in terms of autocomplete, and it makes a big difference.

It replaces the (nonoverlapping) occurrences of " " with "" in str.

Regexes have facilities for case insensitive replace, as in most languages.

Maybe I've been programming in Python too long, but str.replace is intuitive to me (unlike, say PHP's str_replace). I'm guessing you don't program in Python much, judging by your question of string copying.

I started to consider you had a valid point then I went back and read "stringByReplacingOccurrencesOfString" again.

Can you really defend that as an identifier in any sane API? It's utterly abhorrent to my eyes, in the same way typical Microsoft bastardized Hungarian looks. Actually - it's worse.