Why are many languages case sensitive?

后端 未结 30 1697
执念已碎
执念已碎 2020-11-29 04:35

Why are many languages case sensitive?

Is it simply a matter of inheritance? C++ is case-sensitive because C is, Java is case-sensitive because C++ is, etc.? Or is t

相关标签:
30条回答
  • 2020-11-29 05:09

    By typical coding standards, Person would be a class, person a variable name, and PERSON a constant. It's often useful to use the same word with different capitalization to mean something related but slightly different.

    So, if you had three staff members in your business all called Robert, you'd refer to them as Robert, robert and ROBERT would you? And rely on people to know exactly which one you meant?

    Give them email addresses such as Robert@widgets.com, robert@widgets.com, and ROBERT@widgets.com if your email system was case sensitive?

    The potential for an unauthorised breach of personal data would be huge. Not to mention if you sent the database root password to the disgruntled employee about to be sacked.

    Better to call them Bob, Robbie, and Robert. Better still to call them Robert A, Robert B and Robert C if their surnames were e.g. Arthur, Banks, and Clarke

    Really - why on earth have a naming convention that invites mistakes or confusion, that relies on people being very alert? Are you so short of words in your volcabulary?

    And as for the person who mentions the supposedly handy trick "MyClass myClass" - why, why why? You deliberately make it difficult to see at a glance whether a method used is a class method or an instance method.

    Plus you lost the chance to tell the next person reading your code more about the particular instance of the class.

    For instance.

    Customer PreviousCustomer

    Customer NewCustomer

    Customer CorporateCustomer

    Your instance name needs to ideally tell your colleague more than just the class it's based on!

    0 讨论(0)
  • 2020-11-29 05:11

    Case sensitivity adds to language readability by the use of naming conventions. You can't write

    Person person = new Person("Bill");
    

    if your language is case insensitive, because the compiler wouldn't be able to distinguish between the Class name and the variable name.

    Also, having Person, person, PersoN, PeRsOn, and PERSON, all be equivalent tokens would give me a headache. :)

    0 讨论(0)
  • 2020-11-29 05:12

    Lots of people here have said that it would be bad for several forms of capitalization to refer to the same thing, e.g.:

    person
    perSoN
    PERSON
    

    What would be really bad is if these all referred to different objects in code. If you've got variables person, perSoN and PERSON all referring to different things, you've got a problem.

    0 讨论(0)
  • 2020-11-29 05:13

    Unix.

    Unix was case sensitive, and so many programming languages developed for use on Unix were case sensitive.

    Computers are not forgiving - an uppercase character is not the same thing as a lowercase character, they're entirely different. And back when processing cycles, RAM and so forth were expensive it wasn't seen as worth the effort to force compilers and computers to be "forgiving", people were just trying to get the things to work.

    Notice how case insensitivity didn't really become something useful until things like Visual Basic came along - once companies started to get invested in the concept that getting the masses to program was a good thing for their bottom line (i.e., Microsoft makes more money if there're more programs on Windows) did the languages start to be friendlier and more forgiving.

    0 讨论(0)
  • 2020-11-29 05:13

    Case sensitivity doesn't really help case consistency.

    Foo.Bar  
    foo.Bar  
    fOO.bAR  
    

    In a case insensitive language that can be fixed automatically by the editor easily. In a case sensitive language fixing it it's harder as it may be legal. The editor first has to ckeck if foo.Bar and fOO.bAR exist and also has to guess that you typed with the wrong case rather than forgetting to declare the variable (as Foo is different to fOO).

    0 讨论(0)
  • 2020-11-29 05:13

    Because people seriously overthink things.

    Case insensitivity works best when it's also case-preserving and combined with a separation between type and variable namespaces. This means that:

    • If you declare a class as 'TextureImage' and then try to use it as 'textureImage', the IDE can autocorrect you. This gives you the advantage that you'll never have to hit the shift key unless you're declaring an identifier or using an underscore.

    • Just like in Java and several other languages; it's perfectly valid to type "MyClass myClass". The IDE and the compiler should have no problem differentiating between the use of a type and the use of a variable.

    In addition, case insensitivity guarantees that 'o' and 'O' will never refer to different objects. Common arguments include:

    • "sOmEoNe wIlL tYpE cOdE lIkE tHiS"; => and that someone will _never_ be allowed to join a programming team, so this is a strawman argument. even if they did manage to do so, case insensitivity is more the solution than the problem, because it means that you don't have to remember whatever crazy uppercase/lowercase combination they use.

    • "you can't internationalize case insensitivity easily!"; => over 95% of programming languages are written in english for a very good reason. there are no competing character encodings and the vast majority of keyboards on earth are english based (in partial or whole). supporting unicode identifiers is perhaps the dumbest idea anyone has came up with in the 21st century; because a good portion of unicode characters are frikkin invisible surragates, reading code is hard enough without having to use google translate, and writing code is hard enough without having to copy-paste identifiers or use a character map.

    • "but case sensitive languages have more identifiers!"; => no, they have grammatically overloaded identifiers, which is substantially worse.

    I don't use any case-insensitive languages, but the advantages are blatantly obvious if you think about this sort of thing seriously.

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