Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OnkelTem/5cacc8660c8a4170cfd2d4704e3d2559 to your computer and use it in GitHub Desktop.
Save OnkelTem/5cacc8660c8a4170cfd2d4704e3d2559 to your computer and use it in GitHub Desktop.

Standard naming for variables with files, directories and paths

I suggest to use the following approach in the naming:

  • filename - file name: readme.txt
  • dirname - directory name: mydir
  • filepath - file name, including any path to it: path/to/readme.txt
  • dirpath - directory name, including any path to it: path/to/mydir

As for the words file and dir themselves, I'd recommend not using them for paths at all, because:

  • file is a good name for storing file objects.
  • dir can be used for DirEntry objects of readdir/opendir etc.

However, since the latter usage is not very common, to save typing dir can also be used as an alias for the a dirname based variable in a local context.

Examples

Suppose we have a baz project directory with a readme.txt file.

Then we could define the following variables:

camelCase snake_case path/to/baz/readme.txt
readmeFilename readme_filename readme.txt
readmeFilepath readme_filepath path/to/baz/readme.txt
readmeDirname readme_dirname baz/
readmeDirpath readme_dirpath path/to/baz/
projectDirname project_dirname baz/
projectDirpath project_dirpath path/to/baz/

NOTE: The slash character after directories above is optional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment