Skip to main content
GameDev.net gamedev.net
🔒 Locked

Unicode variable naming

Started by kSquared Aug 6, 2005 at 10:43 AM 20 replies 2.5k views
Original Post
kSquared
kSquared
With the advent of the next-generation programming languages (C++, Java, C#) that are more accessible to the global community, and the explicit support of Unicode code pages, has the time come to rethink our variable naming to include Unicode symbols? Here's a real-world example. This is from some C# code I wrote that implements T. Vincenty's iterative algorithm to find the path between two geodetics on an ellipsoid:
            lambdaCurrent = deltaLambda +
               ((1 - C) * GeoLineCalculator._flatteningEllipsoid * Math.Sin(alpha)) *
               (sigma + C * preComputedSinSigma *
                  (preComputedCos2SigmaM + C * preComputedCosSigma *
                      (2 * preComputedCos2SigmaM * preComputedCos2SigmaM - 1)
                  )
               );
Here's the same code, after being Unicode-ized:
λc = δλ + (1 - C) * ω * Math.Sin(α) * (σ + C * sinσ * (cos2σm + C * cos2σm * (2 * cos2σm * cos2σm - 1)));</tt>
Clearly the second is more readable than the first, and it looks closer to the sort of thing you might see in a textbook. To be fair, I didn't include the explanatory comments in the first version that might have helped the reader understand what was going on. But the point stands -- with widespread platform, language, and compiler support for Unicode, isn't it time we started rewriting our old rules for [0-9a-Z_] as the only valid set of identifier characters?
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<
Gorax
Gorax
It's not a bad idea, since people'd be able to do things in their own languages, but there is a simple way to do it. All you'd have to do is parse the entire file, saving each part to a new file, while finding words that aren't using ascii characters, and generate replacements for them ('variableNumber1' for example). Then if the compiler complains, you can look in the list of replacements for the backwards conversion into the unicode text values. It'd require a little bit of work, but it wouldn't be too hard to implement, and it'd give you want you want.
Yann L
Yann L
Quote:
Original post by kSquared
With the advent of the next-generation programming languages (C++, Java, C#) that are more accessible to the global community, and the explicit support of Unicode code pages, has the time come to rethink our variable naming to include Unicode symbols?

Definitely. This would also solve the annoying problems when using foreign language identifier names that include non-english characters.
SiCrane
SiCrane
I think it's a horrible idea to extend identifiers to arbitrary unicode characters. Case in point:

int pathfinding;

int pathfinding;

Maybe a limited subset of Unicode like the math symbols would be ok.
Manip
Manip
I can't speak for anyone else but I for one do not have those on my keyboard and do not want to have to remember ALT-1234 just to type each one. Do you suggest we stop coding, open the character map, find these characters, copy and paste them in? Woot what a time saving idea!

Also it is LESS readable.
kSquared
kSquared
Quote:
Original post by SiCrane
I think it's a horrible idea to extend identifiers to arbitrary unicode characters. Case in point:

int pathfinding;

int pathfinding;

Maybe a limited subset of Unicode like the math symbols would be ok.

I see your point, but I'm not convinced. I don't think it's right to extend identifiers to arbitrary Unicode characters, but certainly we should take advantage of the tremendous flexibility and power of such a well-defined standard. At a minimum, I'd say the math symbols and Greek letters would be a good start. I tend to use those a lot.

Quote:
Original post by Manip
Do you suggest we stop coding, open the character map, find these characters, copy and paste them in? Woot what a time saving idea!

Since it'd be optional, I can't see how you'd possibly lose time to it. Every developer or organization would develop their own style and see fit to promote or discourage the use of Unicode in their style guidelines.

On an unrelated note, fans of literate programming (like Knuth) would go nuts over this; the documentation and source code are becoming more tightly coupled. In general, the more self-documenting your code is, the less work it is for you to maintain and the better the situation as a whole.
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<
Ilici
Ilici
It would definitely go hand in hand with using a keyboard like this. It would be awesome! Currently the only problem is typing the characters, otherwise i'd definitely use them.

bytecoder
bytecoder
You consider C++ a next-generation language? Good luck with that. Personally, I think everything should be in plain english, not counting those math symbols, unless you're 99.99% sure that only people that speak that language are going to see and modify it.
kSquared
kSquared
Quote:
Original post by bytecoder
You consider C++ a next-generation language? Good luck with that.

In the sense that it supports Unicode with not many complaints, yes. That's more than you can say for Ruby (clunky Unicode support), Python (quiltwork patch of Unicode support, though most of the core modules have it), and most C compilers (gag!).
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<
Nathan Baum
Nathan Baum
Quote:
Original post by kSquared
With the advent of the next-generation programming languages (C++, Java, C#) that are more accessible to the global community, and the explicit support of Unicode code pages, has the time come to rethink our variable naming to include Unicode symbols?

Here's a real-world example. This is from some C# code I wrote that implements T. Vincenty's iterative algorithm to find the path between two geodetics on an ellipsoid:

            lambdaCurrent = deltaLambda +               ((1 - C) * GeoLineCalculator._flatteningEllipsoid * Math.Sin(alpha)) *               (sigma + C * preComputedSinSigma *                  (preComputedCos2SigmaM + C * preComputedCosSigma *                      (2 * preComputedCos2SigmaM * preComputedCos2SigmaM - 1)                  )               );


Here's the same code, after being Unicode-ized:

λc = δλ + (1 - C) * ω * Math.Sin(α) * (σ + C * sinσ * (cos2σm + C * cos2σm * (2 * cos2σm * cos2σm - 1)));</tt>


The "C * cos2σm" is meant to be "C * cosσ", no?
Quote:

Clearly the second is more readable than the first, and it looks closer to the sort of thing you might see in a textbook.

I'm not so sure it is:


Oh, and
lc = dl + ((1 - C) * lineCalc.FlatEllip * Math.Sin(a))          * (s + C * sins * (cos2sm + C * coss * (2 * cos2sm * cos2sm - 1)));

I'm pretty sure that a vaguely competent programmer who understands your second expression will understand mine.
Quote:

To be fair, I didn't include the explanatory comments in the first version that might have helped the reader understand what was going on. But the point stands -- with widespread platform, language, and compiler support for Unicode, isn't it time we started rewriting our old rules for [0-9a-Z_] as the only valid set of identifier characters?

[lisp whoring]
Of course, in Lisp you've been able to use absolutely any characters in identifiers for decades.
[/lisp whoring]
Quote:
Original post by SiCrane
I think it's a horrible idea to extend identifiers to arbitrary unicode characters. Case in point:

int pathfinding;

int pathfinding;

That's not why it's a bad idea.

Any idiot can give two variables names that look similar. In some badly designed fonts, the variables Fool and Foo1 look identical.

The actual reason it's a bad idea is that whilst every programmer has a font which contains the alphanumeric characters, not every programmer has a font which contains absolutely every Unicode character. Case in point: Me.
Quote:

Maybe a limited subset of Unicode like the math symbols would be ok.

If my font had a lambda symbol and whatever symbol he's using to represent "GeoLineCalculator._flatteningEllipsoid", I'd agree. And if there was actually a single symbol which most programmers would understand as "GeoLineCalculator._flatteningEllipsoid".
Nathan Baum
Nathan Baum
Quote:
Original post by kSquared
Quote:
Original post by bytecoder
You consider C++ a next-generation language? Good luck with that.

In the sense that it supports Unicode with not many complaints, yes.

A language has unsufficient support for Unicode if I can't pass a Unicode string to any string-handling function. Show me how C++ makes it possible for me to pass a Unicode string to string-handling functions in any C++ library I like.
Fruny
Fruny
hmmmmm (scroll down for pictures)

TOP, GREEK, SHIFT, CONTROL, META (ALT), SUPER, HYPER modifier keys...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Dmytry
Dmytry
Quote:
Original post by Yann L
Quote:
Original post by kSquared
With the advent of the next-generation programming languages (C++, Java, C#) that are more accessible to the global community, and the explicit support of Unicode code pages, has the time come to rethink our variable naming to include Unicode symbols?

Definitely. This would also solve the annoying problems when using foreign language identifier names that include non-english characters.


Why not just have IDE option that display lambda (if it appears in variable name) as λ and so on? Like deltaLambda as δλ
Very easy to implement, no need to change programming language, no need to change language standards, is 100% compatible with people not having brand new IDE and keyboard.
Of course, that's not something one would propose in lougne :\ Lounge is for projects like doing syntax highlighting by embending colors tags into source.

(note: variableNumber1 as variable1 is not very good idea because of arrays)

Also, how you are going to type variable names using non-latin characters? The only sane way seems to type lambda for λ if you don't use Greek keyboard layout (and just type it if you do).

as about non-greek: would you like if I would name my variables in for example Russian, or better yet Chinese and you would have to use my library?

[Edited by - Dmytry on August 7, 2005 12:22:58 PM]
markr
markr
In order to use non-ascii variable names, you only need a consensus as to what encoding you are using *at compile time*.

Some programming languages specifically allow unicode naming of things at runtime for stuff like reflection. However, this is only true of languages which *support* reflection anyway (Java, .NET CLR)

Plus, it's your program, you can make it however you want it.

For example, you can use any ordinary C++ compiler, provided that you ensure that your editor(s) are always using UTF-8 and your compiler is 8bit safe (which I believe it probably is).

In C# programs are usually written in UTF-8. The C# compiler is capable (I believe) of detecting UTF8 source code and compiling it accordingly.

Java can also compile UTF8 source code, but you'll need to set an option to tell the compiler that your source is in UTF8 (java.file.encoding or something?).

I don't see any problem with doing this, except it makes things more difficult for people maintaining your code. For example, if they don't know how to type λ, they are going to have to copy and paste your lambda which will slow them down.

So if you are confident that this is sensible for *everyone* who will ever have to maintain your code, go for it.

It should certainly work in C# and Java, maybe also C++

Mark
capn_midnight
capn_midnight
Quote:
Original post by Dmytry
Why not just have IDE option that display lambda (if it appears in variable name) as λ and so on? Like deltaLambda as δλ
Very easy to implement, no need to change programming language, no need to change language standards, is 100% compatible with people not having brand new IDE and keyboard.

horrible idea, "lambdata" (information about baby sheep) becomes "λta"
Nathan Baum
Nathan Baum
Quote:
Original post by Dmytry
as about non-greek: would you like if I would name my variables in for example Russian, or better yet Chinese and you would have to use my library?

I've got it. I'll make a new programming language with keywords and operators in Tengwar.
TheGibberingFool
TheGibberingFool
Quote:
Original post by Manip
I can't speak for anyone else but I for one do not have those on my keyboard and do not want to have to remember ALT-1234 just to type each one. Do you suggest we stop coding, open the character map, find these characters, copy and paste them in? Woot what a time saving idea!

Also it is LESS readable.


Seconded.
__________________________________Peter Lewis ([email=me_AT_pjblewis.com]E-mail[/email] | Portfolio)
Yann L
Yann L
Quote:
Original post by Dmytry
as about non-greek: would you like if I would name my variables in for example Russian, or better yet Chinese and you would have to use my library?

If I was Russian or Chinese, yep, I would indeed like that. There is absolutely no reason why closed source should be kept in english. Especially here in France, where few people speak good english, all code is usually written in French. And that can sometimes be a little problematic, if you use tools on your sources that are not aware of the fact that US-ASCII is not the only encoding on the world...

It would be nice, if Unicode identifier support was officially added to the standards of all currently used programming languages (although most compilers will silently accept raw 8bit ISO 8859-15 encodings, UTF-8 support is not as widely available).
Dmytry
Dmytry
Quote:
Original post by capn_midnight
Quote:
Original post by Dmytry
Why not just have IDE option that display lambda (if it appears in variable name) as λ and so on? Like deltaLambda as δλ
Very easy to implement, no need to change programming language, no need to change language standards, is 100% compatible with people not having brand new IDE and keyboard.

horrible idea, "lambdata" (information about baby sheep) becomes "λta"

wnat's about using several neural cells and considering it is very easy to automatically differenciate "lambdata" from LambdaTa, lambdaTa, lambda_ta, etc?
It's just option of editor, again. It is not meant to never break; just it will not introduce as much pain as other stuff.

as about propietary code written in Russian or Chinese, fine as long as you don't need to use it, eh? My point is that if you would _have_ to use it you would be real annoyed.

BTW: I believe there is tools that can convert unicode names to some weird stuff like AGrdfEfewFJ prior to compilation. In case of cyrillic, it is easy to convert to latin alphabet, despite that there's not enough letters (some just gets encoded as 2 letters). I even had seen that on some distro of Linux when I viewed cyrillic page without having font.

edit: see post below. Exactly. If you want it you can use it, just if you'll put greek letters in variable names like kSquared did, you'll annoy anyone who will have to use it.

Also, note that for OP's example to be at least somewhat non-speculative, it should be:

before:
Lc = dL + (1 - C) * w * Math.Sin(a) * (S + C * sinS * (cos2Sm + C * cos2Sm * (2 * cos2Sm * cos2Sm - 1)));

after:
λc = δλ + (1 - C) * ω * Math.Sin(α) * (σ + C * sinσ * (cos2σm + C * cos2σm * (2 * cos2σm * cos2σm - 1)));
Now, please compare typing times.

OP's example is merely example of how by using shorter variable names you can get shorter code. Indeed, you can make names look shorter by using bigger charset, but it will not be faster to type or easier to read.

[Edited by - Dmytry on August 7, 2005 3:30:41 PM]
vNistelrooy
vNistelrooy
Visual C++ 2005 Beta 2 supports Unicode source files, doesn't it?
"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.