Is my first Java messenger server too large, or should I split it up?
Asked by
MellowPine47
I'm building a simple messenger server as my first project, but the code is starting to feel much larger than expected. It currently handles loading login data from a file, accepting socket connections, checking usernames, creating client handlers, and starting threads, all inside one server class and one main method. Is this a normal amount of code for a beginner project, or should I break it into smaller classes and methods? I'm also planning to add password handling and encryption later, so I'd like to organize it properly before it grows further.
Answers
Before adding more features, split the server into logical pieces: one component for reading user data, one for authentication, one for accepting connections, and one for handling an individual client. Also be careful with the login check in the current code: it appears to compare the received username against each stored password rather than the stored username. Passwords should eventually be stored using a secure password-hashing method instead of plain text.
Reply to this answer
Formatting helps a lot with judging code size and structure. Once the indentation is fixed, this doesnโt look enormous, but putting all of the setup and connection logic into one main method will become difficult to maintain. Extract sections into clearly named methods, and move responsibilities such as login storage and client handling into their own classes.
Reply to this answer
The program isnโt unusually large, especially for Java, but the main issue is that itโs doing too many unrelated jobs in one place. Consider separating file loading, authentication, networking, and client management into smaller methods or classes. That will make each part easier to test and change as the project grows.
Reply to this answer
Add your answer
Answers are reviewed before publication.
MellowPine47: That makes sense. I was having trouble posting the code with proper formatting, but Iโll clean it up and start separating the different responsibilities.