daniel-uzunu / daniel-uzunu.github.io

0 stars 0 forks source link

Naming Things #1

Open daniel-uzunu opened 8 years ago

daniel-uzunu commented 8 years ago

I want to write an article about why the names are important and how to come up with good names.

https://github.com/daniel-uzunu/daniel-uzunu.github.io/wiki/Naming-Things

Everyone agrees that it is important to choose good names, but what a good name is varies a lot. In this article, I will try to prove that some general guidelines exist. I should probably start from searching about what makes the code easier to understand. One issue is, that names are only a part of the equation.

In this article, I will focus on guidelines for high-level object-oriented languages (Java, C#, Scala, JavaScript, Python ....). Should I explore naming for functional programming?

daniel-uzunu commented 8 years ago

http://stackoverflow.com/questions/421965/anyone-else-find-naming-classes-and-methods-one-of-the-most-difficult-part-in-pr:

A good naming convention should minimize the number of possible names you can use for any given variable, class, method, or function. If there is only one possible name, you'll never have trouble remembering it.

One lesson I heave learned, is that if you can't find a name for a class, there is almost always something wrong with that class: you don't need it, it does too much or it does too little.

The book Code Complete by Steve Mcconnell has a nice chapter on naming variables/classes/functions/...

"There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton

In short: I agree that good names are important, but I don't think you have to find them before implementing at all costs.

Of course its better to have a good name right from the start. But if you can't come up with one in 2 minutes, renaming later will cost less time and is the right choice from a productivity point of view.

Long: Generally it's often not worth to think too long about a name before implementing. If you implement your class, naming it "Foo" or "Dsnfdkgx", while implementing you see what you should have named it.

Especially with Java+Eclipse, renaming things is no pain at all, as it carefully handles all references in all classes, warns you of name collisions, etc. And as long as the class is not yet in the version control repository, I don't think there's anything wrong with renaming it 5 times.

Basically, it's a question of how you think about refactoring. Personally, I like it, though it annoys my team mates sometimes, as they believe in never touch a running system. And from everything you can refactor, changing names is one of the most harmless things you can do.

daniel-uzunu commented 8 years ago

Some things to read:

http://c2.com/cgi/wiki?GoodVariableNames http://c2.com/cgi/wiki?BadVariableNames http://c2.com/cgi/wiki?SelfDocumentingCode http://c2.com/cgi/wiki?SystemOfNames http://objectmentor.com/resources/articles/Naming.pdf http://objology.blogspot.ro/2011/09/one-of-best-bits-of-programming-advice.html http://caseysoftware.com/blog/useful-naming-conventions

daniel-uzunu commented 8 years ago

http://c2.com/cgi/wiki?GoodVariableNames:

The most obvious benefit to GoodVariableNames can reduce the need for extensive comments, producing SelfDocumentingCode. When reading code peppered with well thought out variable names it's like reading poetry or music.

Some variable name choices may at first glance seem odd or an unnecessary waste of time, but closer inspection can reveal subtleties and depth behind a program's inner workings as well as the programmer's future intent for direction of the program (if any), and state of mind (if any).

The philosophy behind DonKnuth's LiterateProgramming methodology is that code should be written primarily to communicate its purpose to humans.

What constitutes a good variable name is wholly dependent upon many different factors. Programming language, programmer's language (native spoken tongue), limitations in hardware, software, and time constraints often lead to humorous BadVariableNames.

Choose a name that describes WHAT the object does, instead of how it does it.