Results 1 to 2 of 2
- 06-07-2009, 12:18 PM #1
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) System.out.println(f); } //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; }
- 06-08-2009, 07:22 AM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
JSP Design.
By makpandian in forum NetBeansReplies: 0Last Post: 04-20-2009, 01:21 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks