wiki:GoodCodeExamples

Version 22 (modified by meddle, 16 years ago) (diff)

--

Here we will be adding examples of good code, if you want to add somethings, please talk with the integrators first.

  • Example for good equals method.
    • You can use the mouse-right-button menu in the source code area of eclipse -> Source -> "Generate hashCode() and equals()..." for template, but refactor it to look like the example bellow:
              @Override
      	public boolean equals(Object object) {
      		
      		if (object == null) {
      			return false;
      		}
      		
      		if (object == this) {
      			return true;
      		}
      		
      		if (object.getClass() != this.getClass()) {
      			return false;
      		}
      		
      		// Custom code here...
      	}
      
  • Writing if, else, else if, for, while, do while, try, catch, finally statements, blocks.
    • First of all always put curly brackets after the special word that opens the statement, even if the statement contains one row.
      		if (some_condition) {
      			System.out.println("pass");
      		}
      
    • Insert spaces between the brackets and the words. The opening bracket of a statement is on the row where the statement is!
      		while (some_condition) {
                              // something...
      		}
      
    • When writing conditions put spaces between the operands and the operators!
      		if (a != b) {
      			System.out.println("a is not the same as b");
      		}
      
    • if-else if- else example:
      		if (a != b) {
      			// something...
      		} else if (c != d) {
      			// other thing...			
      		} else if ((b == c) && (a == d)) {
      			// third thing, I put the brackets for easier reading, they are not mandatory.
      		} else {
      			// four...
      		}
      
    • for examples
                      for (i = 0; i < n; i++) {
      			// do something...
      		}
      
                      for (Some_iterable_type some_var : some_iterable) {
      			// do something...
      		}
      
    • try-catch-finally... The same as the above as spacing and etc...
                      try {
      			// some code
      		} catch (Exception e) {
      			// handle exception
      		} finally {
      			// some stuff
      		}
      
  • Don't use @SuppressWarnings("all") and @SuppressWarnings("synthetic-access") at all, other like @SuppressWarnings("synthetic-access"), @SuppressWarnings("unchecked"), etc.. you can use, but rarely only when needed!
  • We don't have special conventions for naming but don't use long name or stupid ones like "stuff".
  • Don't leave commented code where you tried something to be reviewed, we will be not very happy.
  • Avoid writing expressions on several lines, instead use variables... but if it's needed for example in statements here are some examples :

Write &&,
to be first on the new line.
		                a == b
				&& c == d
				|| b != g
				&& !flag
  • If you Ctrl+Shift+F, please refactor your code not to have something like:
    				res
    						.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    
    to something like
                                    res.setDefaultCloseOperation(
    						WindowConstants.DO_NOTHING_ON_CLOSE);
    
    but again it is advisable to use variables, something like:
                                    int closeOperation = WindowConstants.DO_NOTHING_ON_CLOSE;
                                    res.setDefaultCloseOperation(closeOperation);