84

I try to be grammatically correct in my naming*. I've always used filename instead of fileName. The java convention also seems to use this, but FxCop prefers fileName.

There's a discussion on WikiPedia about it. The more I read, the more I feel I'm right (which is quite usual! :) ). Does anyone have a definitive answer or is this merely something subjective?

* I just hope there are no grammar errors in this post!

Steve Dunn
  • 21,044
  • 11
  • 62
  • 87

9 Answers9

49

Lower camel case is recommended for fields and parameters.

Example 1:

fileName // for fields, parameters, etc.
FileName // for properties, class names, etc.

Generally, fileName is used and NOT filename; you can verify that by reading source code of open source stuff created by Microsoft, such as Enterprise Library.

Reasons:

  1. The main point behind this is that names are more readable in this case.
  2. Also this approach adds consistency when several parameters (fields, variables..) are used in the same method (class..) and the with same prefix "file", as demonstrated below:
  3. ...there are a few other reasons, but they are more subjective.

Example 2:

fileName, fileSize... // instead of filename AND filesize

See also:

For a full set of naming convention rules, I recommend checking this book:

And also check some stuff at IDesign.net

JohnB
  • 18,046
  • 16
  • 98
  • 110
Konstantin Tarkus
  • 37,618
  • 14
  • 135
  • 121
  • 7
    @Koistya - you're missing the point, the poster is asking about whether "filename" (1 word) is better than "file name" (2 words), not about whether to use camel case or not. – Dan C. Apr 12 '09 at 18:54
  • 1
    @Dan, I don't. Even if put camel case apart, "fileName" is recommended way for naming a parameter, not "filename". – Konstantin Tarkus Apr 12 '09 at 19:13
  • 3
    @Koistya: You still give no reason *why* we should use fileName instead of filename. Good compilation of information, though. – Treb Apr 12 '09 at 20:31
  • I always wonder what the convention is for concatenating, e.g. PathandFileName or PathAndFileName? – RobS Apr 15 '09 at 06:43
  • 2
    @Koistya: if you put camel case apart (since filename and fileName are both camelcase-compliant, depending on how you look at the word), then we need a good reason for choosing one over the other. The standards do not speak about "filename" vs "file name", so they have no place here (although the .. – Dan C. Apr 15 '09 at 07:10
  • .. links are quite useful). In the end, it's all about consistency with .NET framework, and readability. Personally I also prefer fileName, I just didn't see the relation with any coding guidelines. – Dan C. Apr 15 '09 at 07:21
  • @Rob: using title case (the first form) is less readable than the second. Pascal casing asks to capitalize the first letter of each word. – Dan C. Apr 15 '09 at 07:24
  • Ough, this answer can be stripped down to `2. [...] this approach adds consistency when several parameters (fields, variables..) are used in the same method (class..) and the with same prefix "file", as demonstrated below:`. The remainder has nothing to do with the question and totally distracts the reader from understanding. – Sebastian Mach Sep 27 '17 at 15:00
31

'filename' assumes that this word describes a singular object like 'cow' or 'chair'
'fileName' assumes that this is a complex object, that there is an object called file and that this object describes the name of that file.

Two philosophical approaches, take your pick.

shoosh
  • 76,898
  • 55
  • 205
  • 325
22

It is acceptable English to write "filename" or "file name". When you translate that into coding, capitalizing the "n" or not capitalizing the "n" can go either way (assuming camelCase or PascalCase).

By the way, you did make a grammatical error in the question--ironically, in the very sentence in which you were expressing your hope that there were no grammatical errors. You said, "I just hope there's no grammar errors in this post!" But "errors" is plural, therefore the "is" of "there's" represents a subject-verb disagreement.

* I just hope there are no grammatical errors in this post!

mannyglover
  • 2,139
  • 14
  • 18
12

As far as I am concerned,

thisIsMuchMoreReadable than readingthis.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Naveen
  • 74,600
  • 47
  • 176
  • 233
  • 33
    But is ReadAble more readable than Readable? That's a bit closer to the point. Read is a word, Able is a word and Readable is a word. So assuming the first letter of each word is capitalized, how should we write it? – jalf Apr 12 '09 at 18:05
  • 17
    SoIBetYouThinkThisIsSuperReadable, well_I_actualy_think_you_are_wrong – David Lehavi Apr 12 '09 at 20:14
  • 3
    But this is not what the OP was concerned about. – Sebastian Mach Sep 27 '17 at 15:05
9

I think the answers here are spanning two issues.

  • 'FileName' vs 'Filename' (should 'name' be a separate word)

    and

  • 'fileName' vs 'FileName' (should first character be lower case).

In most cases, I prefer to treat this word as a single whole word 'filename'. I also prefer starting variables/methods with lower case for easier code completion menu navigation.

I guess the issue of camel case is here too which I think should be used to distinguish multi-word names.

Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
  • 2
    From wiktionary (http://en.wiktionary.org/wiki/filename), it seems like *filename* is a word, so I would use *filename* or *Filename* depending on if it was public or not. There is an alternative spelling, *file name*, which would be *fileName* or *FileName*, but I prefer the first spelling. – Svish Jan 20 '10 at 11:29
4

Isn't the obvious answer that FxCop is an automated tool? It recognizes that "name" is a word, so it suggests starting it with a capital N. We happen to know that "filename" is also a word, and so only the first F should be capitalized.

jalf
  • 243,077
  • 51
  • 345
  • 550
2

Filename ~ an identifying name given to an electronically stored computer file, conforming to limitations imposed by the operating system, as in length or restricted choice of characters.

In the past this was considered two words but now is defined as one word so

var filename = ......

if it was two words it would be

var fileName = ....

M pollack
  • 64
  • 4
  • 1
    Good answer, but it's a word by who's definition? For example Filename is not a word according to: Merriam Webster. Unless the dictionary has it as a single word, I would go with it being two words by general standards most people would go by. In which case it should be fileName or FileName. – Tolga Apr 19 '18 at 23:10
2

There can be no real right or wrong here.

This is something that is purely subjective and relates completely to the community you are working in. If FxCop and StyleCop and the .net code that you regularly encounter is using fileName, then use fileName. If it is using something else, then use whatever that is.

Your first priority should probably be to be consistent to the pattern in your own code and then consistent with your community.

In this particular case, .net Reflector shows a lot of .net code using fileName so I would go with that pattern personally.

If you were in the java world and running PMD and checkstyle and their apis made frequent use of filename, then I would go with that.

In addition to the wikipedia naming article, there is also The Practice of Programming by Kernighan and Pike. The first chapter in it touches on a lot of naming and code consistency issues.

Larry Olson
  • 214
  • 1
  • 7
0

If you are writing c/c++ there is a strong tendency to use names that people can actually read; i.e. filename is good, and so is yet_another_file_name (assuming you are not considering filename as a proper english word - I usually do).

See google coding standards

David Lehavi
  • 1,186
  • 7
  • 16