Search code examples
computer-science

Why is underscore preferred over dash in computer science?


It is such a pain to hold down the shift button to type underscore. Why is underscore ('_') preferred over dash ('-') in computer programming?

Edit: my questions is towards object names


Solution

  • The dash is the minus symbol. If it were allowed to be used in variable names it would be ambiguous. Take the following code:

    int a=10;
    int b=9;
    int a-b = 100;
    int c = a-b;
    

    What is c? Is c the value stored in the variable a-b and therefore equal 100?

    Or is it a minus b, and therefore equal to 1?