#When should methods be in a different class?

1 messages · Page 1 of 1 (latest)

tame path
#

what title says, I opened a post because I wanted to put my classes in a different class and now im confused when a method should be in a different class.

sand forgeBOT
#

<@&987246399047479336> please have a look, thanks.

sand forgeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

golden meadow
#

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

gritty vale
#

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

tame path
gritty vale
#

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

gritty vale
#

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?

tame path
#

yeah im doing mooc

gritty vale
#

well then just continue with it

tame path
#

im at part 6 from java programming 1

upper edge
# tame path first time I heard this

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".

tame path
gritty vale
#

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

tame path
#

I mean Im at the point where im learning to split up the code into methods

gritty vale
#

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