Is it a bad practice to give two very different files with the same general purpose the same name?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/250481

  •  04-10-2020
  •  | 
  •  

Pregunta

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>
¿Fue útil?

Solución

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.

Otros consejos

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.

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.

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.

Licenciado bajo: CC-BY-SA con atribución
scroll top