Search code examples
javacoding-style

Standard for programming 'beautiful' code in Java?


I'm reading some books about coding standard in Java. I always loved beautiful and clean code.

But there are some things that bother me. For example, a method name should start with a lowercase word, and if it has a second word, it should be start with a uppercase character. But the standard for variables is the same thing. I think this is a little confusing.

So I'm asking you guys, what's your coding standard in Java? Like:

  1. How do you name objects, methods, classes, etc.
  2. If you have more than one object from same class, how do you name the second one?
  3. If you have one object in the argument of a method and you have another object from the same class inside this method, how you do name both of them?
  4. What is the best trade-off for performance/code beauty, a lot of small methods, or some longer methods?
  5. Feel free to say something more. =)

Solution

    1. Mostly following the Java code convention.
    2. I try to not make it matter what kind of class an object is. If I for instance have five different strings, the name of each variable should describe what information/content the variable represents, and not that is is a string.
    3. I find it often silly to try coming up with variations of a variable just because it exists both as a method argument and a class variable. I mostly use the same name with this syntax this.theVariable = theVariable
    4. A method should be as short as possible: as few lines as possible, and as few nested levels as possible (i.e. max one if-statement, and not ifs inside ifs etc.)
    5. Robert Martin's Clean Code is highly recommended!