Tag Archives: method

Web

Overriding methods in Javascript

js_override
To override any method in JS+jQuery, use:

$(function(){
  SomeObject.prototype.someMethodName = function() {
    alert('Is overriden !!!');
  }
});

Ex is http://code.google.com/p/primefaces/issues/detail?id=4291
https://gist.github.com/sgruhier/1086231

Method signature in Java

method_declaration
Are know about method signature?
That’s quite simple:

The method signature — the method’s name and the parameter types.

Noting more, return type or exceptions are not the part of signature.
See also, http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Methods must be stupid

3se4um
Find small error in architecture sense.

Wrong realization is as following:

public class Login {

  private String username;

  public boolean isAuthenticated() {
    return username != null;
  }

  private void init() {
    if (username == null) {
      // Some actions to get data...
      username = "Johnny Depp";
    }
  }
}

As you see, method init() checks the value of username field. That’s not right.
Methods must perform some action regardless to any external state. But decision to call or not must be accepted by caller.

Here is right implementation:

public class Login {

  private String username;

  public boolean isAuthenticated() {
    if (username == null) {
      init();
    }
    return username != null;
  }

  private void init() {
    // Some actions to get data...
    username = "Johnny Depp";
  }
}