Re: refactoring
Roedy Green <see_website@mindprod.com.invalid> writes:
should be able to have multiple outputs, but very few language
One idea not mentioned yet is a kind of ?continuation passing
style? (?actor style?). The client's method ?continuation? is
called by the server to ?return? the result. Here the client
gets /two/ random numbers from /one/ call to ?getPair? without
an object or an array holding these two numbers.
class Example implements Client
{ public void continuation( final int x, final int y )
{ java.lang.System.out.println( x + ", " + y ); }
public void main()
{ Server.getPair( this ); }}
class Server
{ static java.util.Random rand = new java.util.Random();
static void getPair( final Client client )
{ client.continuation( rand.nextInt( 11 ), rand.nextInt( 21 )); }}
public class Main
{ public static void main( final java.lang.String[] args )
{ new Example().main(); }}
interface Client { void continuation( int x, int y ); }
4, 3
When the program does not need to be threadsafe, the function
and its result can be made static and public to avoid any
overhead by object allocation and getter methods.
public class Main
{ public static void main( final java.lang.String[] args )
{ Calculator.x = 1;
Calculator.y = 2;
Calculator.sumDiff();
java.lang.System.out.println( Calculator.sum );
java.lang.System.out.println( Calculator.difference ); }}
class Calculator
{ public static int x;
public static int y;
public static int sum;
public static int difference;
public static void sumDiff(){ sum = x + y; difference = x - y; }}