I’m brand-new to coding. I’ve tried for years but could never understand passing information between functions and classes. With Mosh’s intro to Java, I think I finally understand this concept.
I created this simple “terminal command” program to create a user and password, and load that info. It was VERY messy (procedural programming), but after watching Mosh’s intro course # 2, I think I’m getting the hang of OOP.
My question now is: How many classes are too many? How do I keep these organized? Is there a way to organize them into directories; or is this a bad idea?
I’ve uploaded my Class Files for you to view. They’re pretty small.
Feel free to point out any errors in my code if you are willing. Please just understand that I’m BRAND new so I ask for an explanation as to why it’s wrong / should be adjusted.
This is similar to asking “how many functions are too many” - it depends on the application. I mean, on the one extreme if you have each class basically doing one line of code, that is probably not pulling its own weight. On the other extreme, having one class do everything is too few. I think you basically need to determine a reasonable breakdown into individual responsibilities. It is similar to the breakdown of functions, but just at a higher level in the design. A good rule of thumb is the Single Responsibility Principle: “is this class responsible for exactly one thing.” The other thing we are trying to be wary of is coupling - usually these can be addressed by using good design patterns.
Again, this is somewhat up to you. For a small application, just keep everything in the same package. For larger applications you should break things up into domain-specific packages. At a company, there is probably some canonical guidance on when to create shared packages to be used across teams vs code that is specific to your application.
To a certain extent, the packages are the directories. If they are in the same package, just put them in the same directory if you do not want to confuse people.
Smaller is generally better. It is easier to reason about the class in isolation when it has a narrow responsibility. It also helps highlight code smells: “class A is about X, but it has method doThing that should be the responsibility of class B since it is about Y.”
It can help to think about things in terms of the verbs that it does. If it is a different verb, it is probably a different class that should be responsible. For example, a class called MortgageCalculator should have methods with corresponding verbs (“calculateX”, “calculateY”, etc) but it should not have methods for displaying things (“dispayX”, “printY”, “showZ”). This is just a rule of thumb so be careful not to assume this is an inflexible rule.
I do not have access to your class files at this point, but I hope that helps!