Friday, May 1, 2026

Polyglote troubles

Being fluent with multiple languages plays a trick on one where a construct exists in many of them, but is worded differently.

Here is an example. All C-like languages have a C style for() loop and a foreach-type loop for iterating in a collection. The former is identical in all of them, the syntax of the latter is all different. Specifically:

  • C++/Java: for(Type element: collection)
  • JavaScript: for(element of collection)
  • C#: foreach(Type element in collection)
  • Objective C: for(Type element in collection)
  • PHP: foreach($collection as $element)
  • † In JavaScript, for(x in y) also exists but goes over indices rather than elements.

    All those are languages that I have to come back to at least once a month.

    Here's another good one - finding the index of the first occurrence of a character/substring in a string. Again, those are all languages that I do active work in, not an academic comparison:

    • C: strchr
    • JavaScript/Java/C#†: indexOf
    • TSQL: CHARINDEX
    • VBA: InStr
    • PHP: strpos
    • Excel: FIND

    † Modulo capitalization.

    So yes, even though I consider myself sufficiently fluent in all of those, sometimes I have to look it up.

    No comments:

    Post a Comment