Results 1 to 2 of 2
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Malta (EU)
    Rep Power

    Default design? return output within methods

    Do you consider it as good design to return results to the console within the methods, other than for debugging?

    Java Code:
    public static int factorial (int n)
    		if (debug)
    			System.out.println("factorial(" + n + "):");
    			Throwable t = new Throwable();
    			StackTraceElement[] frames = t.getStackTrace();
    			for (StackTraceElement f : frames)
    		//calculate factorial and display result in console		
    		int r;
    		if (n <= 1) r = 1;
    		else r = n * factorial(n -1);
    		[B]System.out.println("return: " + r);[/B]
    [B]//I'd rather use the calling application to display the results in real world...[/B]
    		return r;

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Rep Power


    Printing data in methods is not necessarily a bad thing. However, you want (Javadoc) comments with the method stating that it prints data, and possibly even name the method in such a way that it is obvious. As an example you could rename the method factorial to printFactorial(int n)

    Many libraries contain debugging methods, and the regular methods do NOT print data. If you are creating a code library, even just for yourself, you will not want methods to print data. However, a method that is used for a program (and only a program) could print data.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. JSP Design.
    By makpandian in forum NetBeans
    Replies: 0
    Last Post: 04-20-2009, 01:21 PM
  3. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts