Is it a bad practice to give two very different files with the same general purpose the same name, separating them into different directories?

<script src="client_scripts/app/player_stats/generator.js"></script>
<script src="client_scripts/app/coach_settings/generator.js"></script>

I'd like to keep my file names short, and both of the files have the same general purpose without being identical. I'm not sure whether or not this would be considered a bad practice in a professional programming environment. I'd like to know what the best practice is in this situation.

Alternatively, at the expense of the name's short length, I could use:

<script src="client_scripts/app/player_stats/player_stats_generator.js"></script>
<script src="client_scripts/app/coach_settings/coach_settings_generator.js"></script>
  • 7
    Longer names! :) – marko Jul 20 '14 at 6:25
  • 2
    statsgen.js, settingsgen.js – Kroltan Jul 20 '14 at 14:00
  • 1
    DRY! (i.e. shorter names) – Paul Draper Jul 20 '14 at 23:25
  • 1
    Clean Code (i.e. longer meaningful names) – Songo Jul 21 '14 at 9:35

Consider the cost/benefit ratio of your two options:

  1. Would reusing the same name cause confusion or naming conflicts? Probably not, since they're in different folders. The name "player_stats/generator.js" is equivalent to "player_stats_generator.js". However, if you see, in the future, a reason to merge your js files into a single directory (deployment? I dunno), then this should be a good indicator to give unique names.

  2. Would using the longer names involve a lot of extraneous typing? Probably not. Not only do many JS IDEs autocomplete filenames in the project for you, it's also a piece of code that's probably only written - at most - once per file. The code that gets typed a lot is the classes and functions inside the js files, and those (hopefully) don't conflict.

  3. When debugging, what sort of information do you get about an error? If the most common bug report is "Error in line 34 of <filename.js>", then consider giving them unique names, since receiving errors in just generator.js and then trying to divine, through context, which generator it was can be a hassle.

  • 5
    Debugging js usually prints the whole file path. – Bergi Jul 20 '14 at 11:56
  • 1
    @Bergi That depends on the browser (and version), the IDE (if any), the error-logging framework and so on. – Avner Shahar-Kashtan Jul 21 '14 at 8:21

Just as a practical matter, if your IDE shows file names in tabs, if you use the same name for each file, you'll end up with tabs that all show the same name. That can be very annoying. One project I took over maintaining has that problem, and it is a major pain to have 15 tabs open, half of them with the same file name.

So... use more descriptive names.

  • 1
    Most modern text editors will show the path in the tab if files have the same name. – kmiyashiro Jul 20 '14 at 13:53
  • Of course, sometimes it's necessary for multiple files to have the same name [e.g. on many servers, index.html]. I find myself annoyed at programs that make it difficult to determine the path associated with a particular file. – supercat Jul 20 '14 at 19:52
  • 1
    @kmiyashiro - probably so, but if you have many files open, the tabs could be reduced in size (width) to the point where you mostly only see the filenames. Then you have to always hover your mouse over each tab and wait for the "tooltip" to display the full path/file. If you only have a few files open, and only the rare case of a duplicated name, it's probably acceptable. But with lots of files, it can be very annoying. – Kevin Fegan Jul 20 '14 at 20:49
  • 1
    If you have that many tabs open all with the same name, I would switch to the file using a key command instead of trying to find it amongst a sea of tabs even with unique names. – kmiyashiro Jul 20 '14 at 22:42
  • 1
    Using more descriptive names can be rather annoying though... when you get some_super_long_descriptor_that_needs_more_description.js to set it apart from some_super_long_descriptor_that_needs_more_cowbell.js – corsiKa Jul 21 '14 at 0:42

There is a clear deciding factor here: DRY (Don't Repeat Yourself).

Every file name doesn't have to be different; that's what paths are for. Can you imagine how many different system or program files are on your computer? What if each of them had to have a unique name? At some point, we're just making the file name a copy of the path.

If the best description of a Javascript file in the context of client_scripts > app > player_stats really is generator, it's path should be client_scripts/app/player_stats/generator.js.

This question is at programmers.stackexchange.com/questions/250481. There is also serverfault.com/questions/250481. 250481 it is one thing in the context of Programmers questions, and something else in the context of Server Fault questions.

Paths (or URLs) are nice because they are nested identifiers. Let's use them that way :)


Always use descriptive names over short names unless it's something like a mathematical constant or loop variable where conventions of the language in question favor short names. For instance, if you call a variable "pi" and have it be some appropriately precise value of pi, then the name is good and gets the point across. On the other hand, if you have a generator that generates terms of the Taylor series for Pi and adds them together to approximate pi, you want to call it something like "taylorPiGenerator or similar.

Good names now save refactor time later or even worse massive errors later.

The books Clean Code and Code Complete go into considerable detail as the whys and wherefores of good naming but they are by no means the only sources.

  • This answer seems to apply well to this particular example, but it doesn't address the general question. – Paul Draper Jul 20 '14 at 8:44

It depends of technology that you are working with. Names should identify items, and paths should identify context. I agree good naming is important but hey, paths are names too. But from practical point of view, if you are using something like Javascript it is probably better to keep more precise names for end items. If you are working with tools that take this into consideration, like Python, recomended way would be to use same name with different path (module, namespace). If you take look at Java, you'll also find classes with same name and different packages. One could go step further and say methods are named actions in context of class, and we have methods named same in different classes, which themselves can be named same but placed in different packages. The Zen of Python says:

Namespaces are one honking great idea -- let's do more of those!

But javascript has it quirks and perks so I would recommend you go with different names (even if files are in different paths). Also you may look up for module pattern in javascript which could help you write cleaner code:

    var playerStatsGenerator = player_stats.Generator();
    var coachSettingsGenerator = coach_settings.Generator();

You could have your cake and eat it too.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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