Why are many languages case sensitive?

后端 未结 30 1695
执念已碎
执念已碎 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:06

    My guess would be that case sensitivity enlarges the name space. A nice trick such as

    MyClass myClass;
    

    would be impossible with case-insensitive compiler.

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

    A reasonable answer might be that the designers of the language thought it would make the language easier to understand thinking about the future :)

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

    Learning is always easier by example so here it goes:

    C#(case sensitive but usable from VB.NET which is case insensitive):

    CONSTANT_NAME
    IInterfaceName // Uses I prefix in all case sensitive and insensitive languages
    ClassName      // Readable in both case sensitive and insensitive languages
    _classMember   // sometimes m_classMember or just classMember
    DoSomething(someParam) // Method with action name, params can be _someParam
    PropertyName   // Same style in case sensitive and insensitive languages
    localVariable  // Never using prefix
    

    Java and JS use a style similar to C# but methods/functions/events are declared like variables doSomething, onEvent.

    ObjectPascal(Delphi and Lazarus/FPC are case insensitive, like ADA and VB.NET)

    CConstantName     // One can use Def or no prefix, not a standard
    IInterfaceName
    TClassName        // Non-atomic types/classes have T prefix e.g. TStructRecordName
    PSomePointer      // Pointers have types, safer low level stuff
    FClassFieldMember // F means Field member similar to m
    DoSomething(Parameter) // Older code uses prefix A for parameters instead
    PropertyName
    LLocalVariable    // Older code uses prefix for parameters not local vars
    

    Using only OneCase and prefixes for each type makes sense in all languages. Even languages that started without prefixes have newer constructs like Interfaces that don't rely on case but use a prefix instead.

    So it's really not important if a language is case sensitive or not. Newer concepts were added to case sensitive languages that were too confusing to be expressed by case alone and required using a prefix.

    Since case sensitive languages started using prefixes, it's only reasonable to stop using case with the same identifier name someIdentifier SomeIdentifier SOME_IDENTIFIER, ISomeIdentifier and just use prefixes where it makes sense.

    Consider this problem: You have a class member called something, a method/function parameter called something and a local variable called something, what case convention could be used to easily differentiate between these ? Isn't it easier to just use the most ConsistentCaseStyle everywhere and add a prefix ?

    Fans of case insensitive languages care about code quality, they just want one style. Sometimes they accept the fact that one library is poorly written and use a strict style while the library might have no style or poor code.

    Both case sensitive and insensitive languages require strict discipline, it makes more sense to have only one style everywhere. It would be better if we had a language that used only StrictCase, one style everywhere and prefixes.

    There is a lot of poor C code, case sensitivity doesn't make it readable and you can't do anything about it. In a case insensitive language you could enforce a good style in your code without rewriting the library. In a StrictCase language that doesn't exists yet, all code would have decent quality :)

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

    MyClass myClass; would be impossible with case-insensitive compiler.

    Or you could be smart and actually use 2 different words... that better show what you are actually trying to do, like:

    MyClass myCarDesign;

    Duh.

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

    One interesting thing to consider is that English is also case-sensitive. (I suspect this is true for most natural languages, but it may well not be true for all.)

    There's a big difference (where I live, anyway, near the town of Reading) between:

    I like reading.

    and

    I like Reading.

    Similarly, while many people do capitalise incorrectly, and you can usually understand what is meant, that doesn't mean such writing is considered correct. I'm a stickler when it comes to this kind of thing, which is not to say I get everything right myself, of course. I don't know whether that's part of the inheritance of programming language case sensitivity, but I suspect it may be.

    One distinct advantage of case sensitivity for programming languages is that the text becomes culturally insensitive as well. It's bad enough having to occasionally spell out to a compiler which text encoding is used for a source file - having to specify which culture it's in would be even worse :(

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

    The upper-case of a letter isn't a universal concept. Java uses Unicode, so if you wanted case-insensitive Java, the meaning of your program could change depending on what locale it was compiled in.

    Most languages don't let you put dots or commas (or apostrophes or spaces) in the middle of integer literals, probably because that's also locale-dependent.

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