Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is this bad programming ? scanner as global variable [closed]

Is it considered bad programming practice to have an input scanner (such as a keyboard ) declared as a global var for a class? such like:

private static Scanner input  = new Scanner(System.in);

Im working with alot of input from various methods, and just seems alot easier then having to send the keyboard to each method

like image 552
Ris Avatar asked Jan 18 '14 00:01

Ris


2 Answers

It seems a lot easier to use a global variable, but in the long run, it can make the code very hard to maintain, have you thought about creating a class to handle the keyboard input? By having a good separation of concerns, you end up with cleaner code.

https://en.wikipedia.org/wiki/Separation_of_concerns

like image 139
LuRsT Avatar answered Nov 15 '22 01:11

LuRsT


It's best if you created a special class for getting Inputs and/or produce Outputs for example

class IO{
Scanner scanner ;
public Scanner getScanner(){

return new Scanner();
}
public Scanner getScanner(File file){
return new Scanner(new FileReader(file));
}
... // other types of scanners
}
like image 36
Exorcismus Avatar answered Nov 15 '22 01:11

Exorcismus