Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Java naming conventions not enforced? [closed]

Java has strong naming conventions for class, method, field and variable names. For instance:

  • Class names should start with an upper case character
  • Methods, fields, and variable names should start with a lower case character

The JDK has only few exceptions to these two rules.

But a convention is not a syntax rule, and therefore classes like the following compile without errors (C# programmers would be happy anyway):

 public class string {
      private char[] C;
      public int Length() { return C.length; }
 }

But the gap between a convention and a syntax rule inevitable leads to violations of the convention by beginners which then leads to lengthy explanations about naming conventions.

If the most fundamental naming conventions (like the one cited above) would be part of the syntax then the Java compiler would enforce them and automatically educate those beginners.

So here is the question:

From the Java language designers point of view: Is there any good reason to leave a gap between syntax and naming conventions which should never be violated? Are there any meaningful use cases for namings (of classes, methods, fields, variables) which violate the convention but make sense beyond the convention?

like image 339
wero Avatar asked Dec 09 '25 18:12

wero


1 Answers

The conventions were written long after the language was defined so they could be retrofitted without breaking compatibility. The problem with conventions are they involve taste. e.g. spaces or tabs, using $ as a variable name, starting field names with m_ or _ etc. even wither to add get and set to getters and setters (which I prefer not to)

Java actually allows you to do things which would make C programmer feel queasy. Why they allowed this I don't know, but I assume they didn't want to limit adoption by imposing more rules than really needed.

Note this is a piece of Java code is valid due to the use of a character which probably shouldn't be allowed but is.

for (char c‮h = 0; c‮h < Character.MAX_VALUE; c‮h++)
    if (Character.isJavaIdentifierPart(c‮h) && !Character.isJavaIdentifierStart(c‮h))
        System.out.printf("%04x <%s>%n", (int) c‮h, "" + c‮h);

Most IDEs will help beginners write code which follows conventions. The only problem with this is most developers don't know how to make full use of their IDEs. ;)

like image 58
Peter Lawrey Avatar answered Dec 12 '25 00:12

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!