سؤال

My project classes are quickly approaching large numbers of lines into the thousands. Is it good programming practice to divide the classes Into smaller classes even if they do the same thing? After all I'd hate to create communication caller functions for the same object.

هل كانت مفيدة؟

المحلول

It is a good programming practice to split up your code so you (and others) don't get lost.

Split it into methods/functions/procedures, classes and packages by meaning, not by size alone.

If several classes do the same thing, have you thought about using inheritance? Don't duplicate code, it makes maintenance harder (and is a waste).

For Java, interfaces and abstract classes can also improve legibility and structure of your code; use with moderation. Many Java IDEs come with handy "refactoring" functionalities which allow you to restructure your code easier and cleaner than copy/paste would be.

( Possibly related topic: "how do you organize your programming work" how do you organize your programming work )

نصائح أخرى

As a rule, each class should have one responsibility that you can clearly state. If you can't state a single purpose for a class, or the narrowest purpose you can define is nebulous and vague, it's time to refactor.

Of course there are exceptions to every rule, and some classes with a lot of utility methods (like String) will be very large. But I generally take a hard look at the purpose of a class when it grows past about 300 lines. (For the second time - I do it the first time before the class grows past 0 lines.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top