#When should methods be in a different class?
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Methods should be placed in a different class when they are not directly related to the responsibilities or behavior of the class where they are currently located. Here are some scenarios where it is appropriate to move methods to a different class:
-
Single Responsibility Principle: If a method is responsible for performing a task that is unrelated to the main purpose of the class, it should be moved to another class that has that responsibility.
-
Code Reusability: If a method can be reused by multiple classes or if it provides general utility functions, it can be placed in a separate utility or helper class.
-
Encapsulation and Abstraction: If a method needs access to private data or internal implementation details of another class, it can be moved to that class as a member method.
-
Separation of Concerns: If a method is part of a larger functionality that can be separated into distinct modules or components, each with its own set of responsibilities, it is better to move those methods into separate classes.
-
Modularity and Maintainability: If the size and complexity of a class become too large, splitting methods into separate classes based on their functionality can improve code organization and make maintenance easier.
Remember that these guidelines are not strict rules but rather best practices for designing clean and maintainable code. Ultimately, the decision on when to place methods in different classes depends on the specific requirements and design considerations of your application.
what do you mean ?
i think this is your 4th strange question today
you're not going to learn by asking questions
this way
i advise you to open a course or tutorial and learn language basics
i wouldnt call it a strange question. but rather a question that is answered naturally by gaining experience
its all about responsibility
a method called purchaseEgg would rather be in a Supermarket class and not in a CarRepairShop class
which is pretty obvious, isnt it
and thats how it works
first time I heard this
ur the designer of ur code. u have to know what ur classes are supposed to do and responsible for
and then u give them the methods that fit to them
to me it sounds rather like ur just totally new to creating projects with multiple classes and hence are confused
simply bc u lack experience with OOP
the best way to learn that is by practice. get hands on some tutorials and code along
it will click and make sense soon
you mean youtube tutorials?
no
the medium on the tutprial/course doesnt matter
as long as its good
we recommend mooc here
have u gone through the entire mooc already?
yeah im doing mooc
well then just continue with it
im at part 6 from java programming 1
don't take that with bad intentions, im sure firas means the more you do stuff the more it makes sense. For ex i could watch tuts all day and still learn nothing, emphasis is on "more experience/practice".
I like to update my project code when I learn something new
yeah but this seems to be a lecture for another day, ur rushing ahead
the more u do the mooc stuff, the more u will get a feeling for designing ur code and organizing ur classes and methods
I mean Im at the point where im learning to split up the code into methods
which is too early to expect to understand what u asked
simply put, there is no answer to ur question other than "it depends"
u might think that there is some golden rule that u can stick to and life is simple
but there isnt
anything in the direction of code design and organization is very fuzzy and always depends on the case at hand
u always have to judge based on ur experience how to approach it
its always a different solution
classes should have clear responsibilities and the methods that they contain should be about those responsibilities
a class called Bank would not have a method called purchaseEgg in it
thats all there is to it
if u cant apply that advice to ur code/situation at hand yet, thats simply bc u lack experience
dont worry, continue and it will make sense sooner or later