Reputation: 347
One of my first Android application I developed got to a stage that the main activity has more than 50k codes of line, as my first application I didn't really pay attention to managing my activity and methods correctly and as a new project I decided to correct it and to organize the application code.
What would be the best & recommended way to do such a thing? at the moment I am just transferring most of the methods to utility classes and call them when I need as: ClassA.method(boolean, boolean).
Does using so many static methods from utility classes might have a performance impact? (I call the methods without constructing the class usually) Is there a better way to do without static methods in utility classes?
Upvotes: 3
Views: 80
Reputation: 719386
What would be the best & recommended way to do such a thing?
Well my first reaction is to consider starting again. Throw away the current mess and start from scratch. With a proper Object Oriented design, etcetera.
Does using so many static methods from utility classes might have a performance impact?
There shouldn't be a performance impact in general. But there are other problems with overuse of statics.
Is there a better way to do without static methods in utility classes?
Yes. The Object Oriented approach where the functionality has been analyzed and factored into an OO design, and then implemented as Java classes that are instantiated and used appropriately. This often also involves using OO Design Patterns.
It is pretty common for one's first attempt at writing a large program in a new programming language or environment to be a bit of a mess. You are learning and you are likely to make big mistakes.
When you get to point where it is done and you look back at the code, it may look awful. But the question is, what to do about it. The options are:
Leave it alone. If it works, that may be good enough. You can make a note to yourself to do something about it later. (There's a saying that may be apropos to this: "You can't polish a turd".)
Try to fix it up. Except that that's a lot of work, and the end result of the your fixing effort may still be pretty second-rate in terms of ... the criteria that matter to you.
Throw it away and start again. Writing code a second time is often a lot easier and faster than the first time. You should harvest the "good bits"; i.e. ideas and possibly some of the code from your first attempt. But be selective.
Having said the above, you may not yet be ready to fix up the code. As I noted, the best way to implement Java code is to use OO design and design patterns. And the best way to write a specific Android app may other things to void having to write large amounts of Java code.
If you haven't yet learned enough, then it may be too early to rewrite your app. Alternatively, you could treat the rewrite as another learning exercise.
Upvotes: 1
Reputation: 1
Using composition as you are with the utility classes is not a bad idea. It shouldn't do much if anything to performance just because you're calling through another class. But what it will do is make the code more maintainable as you can separate your code into units with high cohesion.
If there is a part of the main activity that you can recognize that it has its own responsibility (outside of the responsibilities of the main activity) you can also give this functionality its own class and perhaps try figure out if it should be a type of component in android.
Overall you want to try and separate as much as the functionality that doesn't have anything directly to do with the main activity into its own class, and make use of that class somehow.
Upvotes: 0