Parameter naming: filename or fileName?

前端 未结 9 2018
情歌与酒
情歌与酒 2020-12-29 19:26

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

相关标签:
9条回答
  • 2020-12-29 19:46

    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 = ....

    0 讨论(0)
  • 2020-12-29 19:46

    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

    0 讨论(0)
  • 2020-12-29 19: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:

    • Naming Conventions at Wikipedia
    • General Naming Conventions at MSDN

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

    • Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries
      (2nd Edition) by Krzysztof, published on Nov, 2008
      (personally we don't use 100% recomendations from this book, but in overall there are pretty good guidelines)

    And also check some stuff at IDesign.net

    0 讨论(0)
  • 2020-12-29 19:50

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 19:55

    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!

    0 讨论(0)
提交回复
热议问题