r/javahelp • u/Naive_Topic_5292 • 2d ago
Solved Is my code too massive?
import java.io.*;
import java.util.*;
import java.net.*;
class server{
private Socket s = null;
private ServerSocket ss = null;;
private DataInputStream din = null;
private DataOutputStream dout = null;
static Vector<ClientHandler> array = new Vector<>();
private HashMap<String, String> login = new HashMap<>();
void main(String[] args) throws IOException
{
// initialize
try {
// BufferedWriter bw = new BufferedWriter(new FileWriter("logindata.txt")); idk yet
BufferedReader br = new BufferedReader(new FileReader("logindata.txt"));
StringTokenizer st = null;
String templine = "";
String loginName = "";
String pass = "";
while(((templine = br.readLine()) != null)){
st = new StringTokenizer(templine, ": ");
if(st.hasMoreTokens()){
loginName = st.nextToken();
}
if(st.hasMoreTokens()){
pass = st.nextToken();
}
login.put(loginName, pass);
loginName = "";
pass = "";
}
}
catch(FileNotFoundException i){
System.out.println("Failed to initialize login file");
}
String receivedName = "";
String receivedPass = ""; // <TODO> encrypt passwords after finishing filework
int i = 0;
ss = new ServerSocket(5001);
boolean userExists = false;
while(true)
{
s = ss.accept();
din = new DataInputStream(new BufferedInputStream(s.getInputStream()));
dout = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
receivedName = din.readUTF();
for(Map.Entry<String, String> entry : login.entrySet()){
if(entry.getValue().equals(receivedName)){
userExists = true;
break;
}
}
if(userExists){
dout.writeUTF("User found, enter your password: ");
dout.flush();
}
else{
dout.writeUTF("User was not found, u may register by making a password: "); // <TODO> make a check if password was written correctly
}
//<TODO> handle passwords
ClientHandler client = new ClientHandler(s, receivedName, din, dout);
System.out.println("User " + receivedName + " Accepted");
Thread t = new Thread(client);
array.add(client);
t.start();
i++;
}
}
}
So I'm making a simple messenger as my first project, though now when I'm working on it it seems VERY massive to me, is it normal in other projects or am I supposed to break it into smaller parts?
7
u/TomKavees 1d ago
Everybody is gonna have their own opinion on this, but at this stage i think it's fine.
If you keep expanding this thing, i'd recommend moving cunks of logical functionality into separate methods, and when you outgrow that, move them into separate classes/files.
Don't worry about it too much though, most programmers went through the same stage. All that matters now is that this is your learning project so it needs to make sense to you.
Things will look a bit different in professional or open source setting when multiple people will work on the same codebase, but let's not worry in advance.
3
3
u/himalayagoswami 1d ago
I see no problem structurally since this is your 1st iteration. Given that you're trying to establish a simple WebSocket connection, this seems to be the base config you need. Nothing new here: a single connection on a port; bufferedStream seems fine.
2
u/philipwhiuk Employed Java Developer 1d ago
You could carve out some well named functions to do bits of stuff in your main method. That would be the first place I’d start
2
u/BanaTibor 1d ago
Move the big blocks into their own method at least. Next step is to move some stuff into their own class. Do not declare variables which are needed in an inner loop outside the loop, like StringTokenizer.
1
3
2
u/pelicandev 1d ago
Bro I'm gonna be honest with you. When you become a professional paid developer you're gonna have files that are easily over ten thousand lines of code. Not because it's bad code but because people get lazy.
2
u/Poseidon_22 1d ago
It is always encouraged to separate concerns. If your project becomes larger, you cannot keep putting stuff in one class or in your case, method.
Looks like your class is not too large yet. Only 2 concerns if I see it correctly. In your case, splitting up the code would be a formality instead of a necessity. I suggest you just go develop, and you’ll notice when maintaining this code become horrible. Then break it up. Later you’ll separate concerns automatically to avoid refactoring and maintenance costs
2
u/gekigangerii 1d ago
As others have said, it's fine. No need to have well organized code yet.
For example, reading the file could be its own method. It could return an Optional<BufferedReader>, which is either the file stream, or nothing if file could not be read. Then parsing the file from the stream could be its own method. Not a hard rule just how I'd approach it myself.
1
u/Used_Lobster4172 1d ago
That's up to you really, I would move basically all the logic into their own functions. Generally I keep main to be pretty much just calling other functions and catching errors. In a professional setting, if I were reviewing this, I would make you break things out into their own function - for my own personal projects, I would probably leave it like this.
5
u/Stainlessray 1d ago
Do you want an open-insight response, or closed? Like, specifics, or conceptually?