News for hackers. Stuff that matters.
Re:Is naming things really that hard? by fragmede on Saturday March 04, 2017 @06:44AM (#13790468)
I've written code numerous times to deals with files, and if there's anything more than feeding the path as given into open()`, then the code inevitably gets filled with variables of related names, and if you stare at it long enough, they all blur together. 'filename', 'name', 'file', 'files', 'file_handle', 'path', 'dir', 'dirname', 'parent_dir', 'file_ext', 'basename'...

Do I want 'filename', which is whatever was given? Do I want 'file', which is the file object? Do i want 'name', which is just the name of file, with directories, if any, stripped off? Do I want 'files', which is the list of files, as given?

If copying/moving is going on, then there's a source and a destination and it gets even worse.

The obvious way out is to refactor by renaming those variables, but 'unsafe_filename_as_given_by_user' or 'list_of_files_as_given' doesn't quite roll off the keyboard.

Re:Is naming things really that hard? by crdoconnor on Saturday March 04, 2017 @09:02AM (#13790906)
>The obvious way out is to refactor by renaming those variables, but 'unsafe_filename_as_given_by_user' or 'list_of_files_as_given' doesn't quite roll off the keyboard.

That's absolutely the way to do it.

"unsafe_filename_as_given_by_user" is clear, precise and a much better name than just "filename" when used in a large scope. It's a stepping stone to better code.

The length and unwieldy nature of the specifically named variable is a code smell, but it's a code smell indicating a different problem this time - the problem is that the variable's scope and/or context is too large.

When I set out to refactor a large code base I almost always increase the variable name length to begin with to prevent name conflation, ending up with the 'unsafe_filename_as_given_by_user' kind of name and then bring it down again later when short variable names in a short scope are precise enough.

Re:Is naming things really that hard? by nine_k on Saturday March 04, 2017 @07:02AM (#13790528)
One word: autocompletion.

You only need to type unsafe_file_name_from_user once. After that, even the simplest, language-agnostics autocompletion of identifiers from the current buffer goes a.long way. A proper IDE makes it even simpler.

Re:Is naming things really that hard? by mikekchar on Saturday March 04, 2017 @07:44AM (#13790654)
I have found that when I start reaching for long variable names it's because there is a design problem. Ideally the use of the variable should be obvious from the context. As you find yourself adding more and more context to the name, you should realise that it's because you lack context in your design.
Re:Is naming things really that hard? by randallsquared on Saturday March 04, 2017 @09:09AM (#13790932)
Deriving meaning from the context requires understanding the context. That's fine for the original author or the refactoring dev, but the troubleshooter swooping in to fix that problem that's obvious in the UI needs to be able to understand the variable names with as little context as possible. "Well, if you'd just spent a few hours to truly grok the intent of the code, ..." is not an acceptable answer when it's possible to make the solution transparent as easily as making more descriptive names.
Re:Is naming things really that hard? by kryptiskt on Saturday March 04, 2017 @09:19AM (#13790963)
Long names that are constantly repeated makes the code very tough to read, there are many Java codebases that show that effect.
Re:Is naming things really that hard? by nine_k on Saturday March 04, 2017 @09:59AM (#13791132)
In Java, you just have to repeat types twice very often:

    DescriptivelyNamedFoo = new DescriptivelyNamedFoo();
In a language with even limited type inference, e.g. C#, this repetition is gone.
Re:Is naming things really that hard? by skybrian on Saturday March 04, 2017 @02:02PM (#13792318)
Descriptive names are great when a symbol is used in many scattered places. Context is okay when the definition is right in front of you. When a function is short enough, context for a parameter should mean reading the function's documentation written immediately above it. Or if the scope of a local variable is three lines, its initializer often explains what it is, and it's right there.
Re:Is naming things really that hard? by natch on Saturday March 04, 2017 @09:32AM (#13791022)
Very well put. I wish everybody could get this concept. With this kind of attitude, you are going to write code that is easier to read, is easier to update and debug, and has fewer bugs in the first place.
Re:Is naming things really that hard? by barrkel on Saturday March 04, 2017 @02:52PM (#13792609)
Code with long identifiers is harder to read, not easier to read. It's harder to see the flow of data and the flow of control, because there is invariably a soup of common prefixes throughout, performing the contextualization.
Re:Is naming things really that hard? by natch on Saturday March 04, 2017 @10:35PM (#13794450)
The identifiers don't all have to be long. Just long enough to be clear.
Re:Is naming things really that hard? by barrkel on Saturday March 04, 2017 @02:51PM (#13792599)
If you're in the middle of the spreadsheet reader, it should be self-evident that 'row' means a row in the spreadsheet; if you're in the middle of the database insertion logic, it should be self-evident that 'row' means a row in the database.

I simply don't accept your priors.

Re:Is naming things really that hard? by randallsquared on Saturday March 04, 2017 @07:42PM (#13793895)
I use 'row' as a variable name quite a lot, especially in loopy situations where it's obvious. The more problematic times are when you have things like a source and a destination row dealing with, say, cows, and you call one of them 'row' and the other one 'cow\_row', and in different places pick which kind of row is which at apparent random.

Or the situation with the file names, as someone else mentioned at more length.

Re:Is naming things really that hard? by lloeki on Saturday March 04, 2017 @08:37AM (#13790828)
> I have found that when I start reaching for long variable names it's because there is a design problem.

I've come across that rule (Gerrand's rule?) which sums up my personal experience pretty well:

> The greater the distance between a name's declaration and its uses, the longer the name should be.

[0]: https://talks.golang.org/2014/names.slide#5

Re:Is naming things really that hard? by marcosdumay on Saturday March 04, 2017 @09:02AM (#13790901)
I do really like that rule that states that name_length is proportional to log(scope_size).
Re:Is naming things really that hard? by jghn on Saturday March 04, 2017 @09:30AM (#13791008)
Agreed although I've found that come code review time there'll always be someone demanding a more descriptive name regardless
Re:Is naming things really that hard? by barrkel on Saturday March 04, 2017 @02:49PM (#13792594)
Long variable names are a smell: they mean multiple concerns have been mixed in a single body of code / scope, and the long names are required to disambiguate them. They're a sign that something has been designed incorrectly.
Re:Is naming things really that hard? by 50CNT on Saturday March 04, 2017 @08:00AM (#13790713)
But then your code easily gets really wide, and you start committing heresy against the holy rule of 82.
Re:Is naming things really that hard? by nine_k on Saturday March 04, 2017 @08:09AM (#13790742)
If your functions are short, and logic straight, you usually don't end up with overly long lines and deep indentation. It's kind of similar to enforcing 8-space tabs, only with space consumed by a more expressive approach.

I often notice that I can remove a comment line after renaming a few identifiers in the code below it, because the code starts to read self-explanatory enough.

Re:Is naming things really that hard? by barrkel on Saturday March 04, 2017 @02:54PM (#13792619)
Identifiers have the same problem that comments have: the thing identified can grow different responsibilities than its name. You don't get magically updating identifiers any more than you get magically updating comments.

I've found that people who believe too much in the ability for identifiers to tell the story of the code factor too much stuff into separate non-reused functions. The result being that you can't take the name at face value; you need to push the current context onto your mental stack, drill into the identifier being called, and if necessary continue drilling, until the unstated side-effects and extra bits and bobs that always accrete in continuously maintained code become clear. This code is ironically harder to read than inlined code.

Re:Is naming things really that hard? by noobiemcfoob on Saturday March 04, 2017 @10:03AM (#13791145)
> I often notice that I can remove a comment line after renaming a few identifiers in the code below it, because the code starts to read self-explanatory enough.

Careful with that. I'm of the mind that the code author is incapable of describing how readable any codebase is. It's too easy to mistake your innate understanding of your own creation for readability.

Re:Is naming things really that hard? by chrisweekly on Saturday March 04, 2017 @02:26PM (#13792471)
Also, while good names might obviate the need for "what" comments, they can't replace often-invaluable "why" comments.
Re:Is naming things really that hard? by nine_k on Saturday March 04, 2017 @02:34PM (#13792521)
Good comments are usually about "why" and "what", while the code is about the "how".

Code with descriptive identifiers often make code read like plain English.

Re:Is naming things really that hard? by jstimpfle on Saturday March 04, 2017 @08:37AM (#13790830)
I've come to not see it as a big problem: It's like in the real world, the same words mean different things in different contexts.

Opening a file, i.e. interacting with the filesystem, is a totally different thing than writing out stuff, so I try to separate these tasks into different procedures. It's not a big problem if "file" might actually mean "filepath of the file in the filesystem" in one place, and "file handle" in another.

Re:Is naming things really that hard? by z3t4 on Saturday March 04, 2017 @09:00AM (#13790899)
Don't put everything in the same object .... A file object should not be used to open files, make something else to open the file, like editor.open(file), editor.clone(file), editor.move(file) ... And the file doesn't need to have all those properties, make functions that derive from file.path like dirName(file.path), fileExtension(file.path), fileName(file.path) ... But object oriented (OOP) is not the only paradigm. Don't make classes if you don't have to. Once you have discovered OOP you tend to make everything into classes, and spend a lot of time building cloud castles.
Re:Is naming things really that hard? by jpswade on Saturday March 04, 2017 @04:43PM (#13793072)
I used to have difficulties with this, but if you apply the system metaphor those problems become less of a problem.

In your example, you're dealing with a file system, so the file is the entity and everything else is just a property of that.

With regards to source and destination, I find that people tend to struggle with this, so instead I tend to think about the entity in question (in this case a file) and the target entity (eg a remote file system).

You don't need to add more words to make it obvious. Just be concise.