7

This is a function in the d3.v3.js file (the graph library D3.js):

  function d3_geo_areaRingStart() {
    var λ00, φ00, λ0, cosφ0, sinφ0;
    d3_geo_area.point = function(λ, φ) {
      d3_geo_area.point = nextPoint;
      λ0 = (λ00 = λ) * d3_radians, cosφ0 = Math.cos(φ = (φ00 = φ) * d3_radians / 2 + π / 4), 
      sinφ0 = Math.sin(φ);
    };
    function nextPoint(λ, φ) {
      λ *= d3_radians;
      φ = φ * d3_radians / 2 + π / 4;
      var dλ = λ - λ0, cosφ = Math.cos(φ), sinφ = Math.sin(φ), k = sinφ0 * sinφ, u = cosφ0 * cosφ + k * Math.cos(dλ), v = k * Math.sin(dλ);
      d3_geo_areaRingSum.add(Math.atan2(v, u));
      λ0 = λ, cosφ0 = cosφ, sinφ0 = sinφ;
    }
    d3_geo_area.lineEnd = function() {
      nextPoint(λ00, φ00);
    };
  }

I was completely taken aback that the programmers used π, φ and λ as variable names. Surprisingly, these variables were accepted even by Notepad (ie: it didn't turn into junk/unrecognized characters).
Is it good practice to use such variables? I can see that they're very intuitive and searchable too, but a bit unnerving.

6
  • 3
    I've in the past (jokingly) proposed that third party contractor code always prefix their variables with 💩 so that it can be more descriptive.
    – user40980
    Mar 4, 2014 at 15:16
  • It's a norm to write such code in, say, Wolfram Mathematica. So why should it be any different in JavaScript?
    – SK-logic
    Mar 4, 2014 at 15:30
  • 2
    @SK-logic: It's the norm to write mutable code (arguably) in C. So why should it be any different in Haskell? Different languages. Different cultures. Mar 4, 2014 at 15:40
  • @ThomasEding, do not mix semantic issues with purely syntax-related ones. As for me, all the languages are pretty similar in this aspect, because I'm using a latex-based literate programming.
    – SK-logic
    Mar 4, 2014 at 15:43
  • @SK-logic: This has nothing to with syntax and everything with semantics. For example ρ can mean density or resistivity or possibly even something completely different. If you are writing code for people who see these symbols frequently and can recognize on the spot what you are calculating, then you get a lot of terseness by using these symbols, because they carry semantic meaning. But otherwise people will be happy to read a descriptive variable name that they can google to freshen up their memory.
    – back2dos
    Dec 15, 2014 at 19:11

5 Answers 5

10

If I saw one of my coworkers writing code with such variable names, I would ask them their rationale for such a decision, but ultimately I think it would have to be changed. Variable names should be descriptive, but also easy to read/write/modify. I don't even know how to type π, λ, or φ on my keyboard. I shouldn't have to go about copy/pasting symbols in my editor when I write my code.

In general, unless the symbol is universally understood, is easily produceable, and is ultimately descriptive to its purpose, I would not use it. I would shy away from foreign words, overly large/advanced words as well. The better solution in this case would be to write out the symbols as their names: "pi", "lambda", and "phi".

5
  • 7
    You listed pi, lambda, pi in your answer. The symbols are understood by many; especially those working with specific mathematical equations. Typing them in is another issue, and that's what makes them problematic to use.
    – user53019
    Mar 4, 2014 at 15:21
  • @GlenH7 I'll update my answer, but I am talking in the general case here.
    – user7007
    Mar 4, 2014 at 15:22
  • 1
    And I agree with you in the general case. They're cumbersome to type in and they don't add the clarity that one may think they do. I'm not sure I would even ask a co-worker why they used the special characters instead of typing out "pi" or "lambda". :)
    – user53019
    Mar 4, 2014 at 15:24
  • 6
    In this case, greek letters are certainly more understandable and easy to read than a latin transliteration.
    – SK-logic
    Mar 4, 2014 at 15:29
  • @SK-logic Maybe, maybe not. It depends on how well versed you are in mathematical symbols. It actually took me a minute to remember what the symbol for phi actually was. One could make the argument that some idiom/expression is better, but then someone comes along with no idea what it means and suddenly you have a problem.
    – user7007
    Mar 4, 2014 at 17:47
4

When the code is written in an environment where greek keyboards are standard (like in greece), this maybe acceptable (assumed the letters have some mathematical meaning for the people who have to read and maintain the code). Otherwise editing greek letters will IMHO become very cumbersome, so I would not recommend it in general, even when the symbols have some certain meaning to the reader.

1
  • 4
    At first I thought you wrote "geek keyboards" :)
    – Nav
    Mar 4, 2014 at 15:25
1

It's entirely dependent on the environment you find yourself in. These symbols are unicode, so you need a development environment that supports unicode. Also, they are dependent on special keystrokes or copy-pasting. This means that they are easier to do on a Mac since Macs have built in support for generating such things via the option key. Windows can do it but you've got to remember all of the various unicode numbers for each symbol.

I suspect the reasoning here is one of minification since φ takes up slightly less space than phi. It also ensures that the browser using D3.js is fairly modern.

-5

Is it good practice to use such variables? I think that it is especially in this case where you can write formulae in their native-looking form. Though i think it's better to say that it's good practice to declare that your HTML documents are unicode (providing, of course, that they are unicode) using at least:

<meta charset="utf-8">

-6

It's a good idea for someone else to use full unicode variable names.

Eventually, after someone else has found all the problems in all sorts of tools and environments and all of those problems have been fixed; it might even be a good idea for you or me to use them. Of course when that time comes, I still won't know how to type them quickly and would have to refuse.

Not the answer you're looking for? Browse other questions tagged or ask your own question.