Conventional name for return value

Some find that consistently using the conventional name 'result' for the return value of a method can increase legibility.

Example


import java.io.*;

/**
* Simple utilities to return the stack trace of an
* exception as a String.
*/
public final class StackTraceUtil {

  public static String getStackTrace(Throwable aThrowable) {
    Writer result = new StringWriter();
    PrintWriter printWriter = new PrintWriter(result);
    aThrowable.printStackTrace(printWriter);
    return result.toString();
  }

  /**
  * Defines a custom format for the stack trace as String.
  */
  public static String getCustomStackTrace(Throwable aThrowable) {
    //add the class name and any message passed to constructor
    StringBuilder result = new StringBuilder( "BOO-BOO: " );
    result.append(aThrowable.toString());
    String NEW_LINE = System.getProperty("line.separator");
    result.append(NEW_LINE);

    //add each element of the stack trace
    for (StackTraceElement element : aThrowable.getStackTrace()){
      result.append(element);
      result.append(NEW_LINE);
    }
    return result.toString();
  }

  /** Demonstrate output.  */
  public static void main (String... aArguments){
    Throwable throwable = new IllegalArgumentException("Blah");
    System.out.println(getStackTrace(throwable));
    System.out.println(getCustomStackTrace(throwable));
  }
} 



See Also :
Naming conventions
Stack trace as String
Multiple return statements
Would you use this technique?
Yes   No   Undecided   
© 2014 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on September 21, 2013.
Over 2,000,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -