1. 34
  1.  

  2. 5

    The worst I’ve experienced are the (usually Ruby) code reviews where people object to simply named, small-scope variables that simply don’t matter: performance_metrics.map! { |n| (n / 100).to_i } gets the comment that “n isn’t a clear name!” Uh, is it not? I’m also had people complain when I use _ for an unused variable in a loop, or using a single character for a short for loop, as in for (Car c : cars). It’s really odd to me.

    1. 4

      “n isn’t a clear name!” Uh, is it not?

      Should’ve used the canonical name i instead ;)

      I fully agree with Rob Pike here. The shorter the variable’s life span, the shorter its name should be, and a clear consistent notation is best.

      I’m also had people complain when I use _ for an unused variable in a loop

      I don’t know Ruby, but a quick web search tells me that’s what it’s supposed to be used for, right? People are weird. I blame education.

    2. 3

      Back when I used to do Java code reviews (I’ve long since moved on to Python, thankfully), I used to say to people who wrote these ridiculously long variable names: “You’ve set the type. So why do you now need to type the type? Once the type is set, it doesn’t need to be typed! Stop typing types!”

      1. 4

        (I’ve long since moved on to Python, thankfully)

        Dynamic languages like Python are certainly more concise in a lot of ways, but don’t they require writing longer variable and method names than Java does?

        The first guideline in the article is “Omit words that are obvious given a variable’s or parameter’s type”. But variables and parameters in dynamic languages have no explicit type. So instead of defining a function merge(List<TableCell> cells) like you might in Java, you have to name the function mergeTableCells to document the parameter.

        I remember writing very long names like that in JavaScript before, because I couldn’t see any better way to document the type of the variables. I wasn’t proud of it, but it seemed necessary. Do you know any better practices in dynamic languages for helping other developers understand how to use variables and methods?

        1. 1

          Dynamic languages like Python are certainly more concise in a lot of ways, but don’t they require writing longer variable and method names than Java does?

          Only if your definition of “require” is “reproduce what I’m used to in my statically typed language”.

          To take your example, List<TableCell> just wouldn’t exist in Python. Use a list ([]), a list of lists ([[], [], ...]), a np.array, or a pd.DataFrame.

          I name all my lists items or records, and I tend to use item or record for a dict or namedtuple entry.

          It’s typical for me to use lists of lists, or lists of dicts. My loops typically look like for item in items or for record in records, or even for key, val in record.iteritems(). Type information wouldn’t add much for my code here.

          I’d name the function merge. If it took parameters, I’d annotate those parameters in docstrings, or, better yet, with default values.

          I probably wouldn’t write the TableCell class – that sounds like something that can be better-suited for built-in data structures.

          Do you know any better practices in dynamic languages for helping other developers understand how to use variables and methods?

          Yes. Code clarity and docstrings. I wrote extensively about Python style, where you can see some examples, over at The Elements of Python Style.

          1. 1

            I do find however, that I get lost in the code. A variable ‘x’ could be scalar, list, numpy array, dict or pandas dataframe. This of course happens in statically typed code too. In this respect C++ is great, because the type is self documenting in the call signature. In Python I need to make an extra effort to document type in my docstring.

            1. 1

              I name all my lists items or records, and I tend to use item or record for a dict or namedtuple entry.

              When writing general-purpose data structure code, or even in code that’s coupled to the class of the objects in the structure?

        2. 2

          Pike would agree.

          1. 1

            Nice one. Manager is main, but class in my mind, by the way.