-1

Is there any way of somehow reducing or compacting the amount of arguments passed to main they these are many?

Let's say I have a Ludo game, and I need to pass number of players, names, colors, type of player (human, computer), type of AI for computer players. That's a lot of arguments.

I don't think I can use any of the techniques described in Effective Java. Is there any smart way of achieving this?

garci560
  • 2,993
  • 4
  • 25
  • 34
  • There are many ways to get information from somebody, so what do you expect from us? Tell you how to read input from the console (already answered here), how to read a file (already answered here), how to read from a database (already answered here) or how to read from a webpage (already answered here)? And may the upvoters please stop upvoting unclear and too broad questions? – Tom Aug 30 '17 at 11:12
  • You can store the settings in property file. – Alexey R. Aug 30 '17 at 11:13
  • So you want to return less parameters? – Alexander Heim Aug 30 '17 at 11:16
  • Does it matter if you're passing lots of arguments to main? Start your binary from a script, which passes in all the relevant parameters; then you just need to run that script, which takes no (or a small number of) parameters. It can be a problem if you are relying upon their position in the `args` array, however - but then, just use named flags. – Andy Turner Aug 30 '17 at 11:17
  • Joshua Bloch text is about parameters of (every/normal) method, really is good to reduce. Has nothing (not too much) to command line arguments. I have not seen "Java pattern" if someone has religious relation to "patterns". @Tom give directions – Jacek Cz Aug 30 '17 at 11:25
  • @Tom I'm asking for strategies to to reduce or compact the amount of parameters passed to `main`. I don't see how you get from that that I'm asking how to read from the console or a file, or who are you to decide by yourself what's too broad and what people can and can't vote. – garci560 Aug 30 '17 at 14:59
  • That's not hard to find out why your question is too broad, you just need to read the corresponding help pages. – Tom Aug 30 '17 at 15:34
  • @Tom Again, that's not to be decided all by yourself. – garci560 Aug 30 '17 at 15:47

4 Answers4

1

Well you could reduce everything to just one parameter if you are willing to denormalize things and accept, e.g., a CSV string:

"3, Jack, Jill, Jon, ..."

Then in your main() method just parse the CSV:

public static void main(String[] args) {
    String input = args[0];
    String[] params = input.split(",\\s+");

    int numPlayers = Integer.parseInt(params[0]);
    String name1 = params[1];
    // and so on
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

How about storing parameters in file and just path the file name as parameter

urag
  • 1,228
  • 9
  • 28
0

In case you want to aquire those informations at the start of the programm it would be the best if you just read them from a csv file as they are very easy to access and manipulate in java.

In case you want to pass on those informations on from one method to another I would recommend to use a model class which stores all the valuable information you need while the programm is running. That would increase the performance as you don´t need to allocate new space for the variables each time you pass the infos onto a new method.

0

I like to pass long parameter lists as associative array in PHP or as object literal in Javascript. Inside my function I have an array with default values. I then take the passed params array and overwrite the default values with the passed. This way I don't have to remember any order, I can leave out parameters that I dont want to pass/where I want to use the default values, and the array keys make the code more readable in the place where the function is called.

Example (Sorry, PHP):

function talk(array $params)
{
    $defaults = [
       'foo' => 'Hi,',
       'bar' => 'my ',
       'baz' => 'name is ',
       'foobar' => 'Benni'
    ];

    $eff_params = array_merge($defaults, $params);

    return $eff_params['foo'] . $eff_params['bar'] . $eff_params['baz'] . $eff_params['foobar'];
}

Then call the function like this:

echo talk([
    'foobar'  => 'Ted',
    'foo'    => 'Hello '
]);

// Hello my name is Ted 

BTW, JQuery uses this technique a lot, e.g. for it's $.ajax() function

Benni
  • 1,023
  • 11
  • 15