Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Say I have a method that is used for opening application with an android app from a sidebar
openApplication(Sidebar s, Context c ... )
and now I want to use this openApplication to open from a Topbar
openApplication(TopBar t, Context c ... )
The function openApplication is very similiar but needs to do little-changes based on Sidebar or TopBar attributes/ members
I dont what to make two different functions that basically do the same thing but different in 2-3 lines of code. What is good practice for approaches like this
I was considering passing a boolean or enum to the function to tell the difference but then I would have lots of if statements in the function for little things. Was also condering making private members _topbar, _sidebar but then if statements again ?
Is there a good practice to generalize functions ? or design pattern out there?
Look at the common interface or superclass that both Topbar and Sidebar share, and use that as the type.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So, I just finished my first program, but I haven't use any methods in it, since I'm just beginning to learn how to use them. Here's the original code : http://codepad.org/JiBfJI8Q I started to fractionate it but realised that it would be a method inside another all the way down. Is that actually the way to do it, or did I get the idea wrong?
without having looked at your code:
The general idea of methods is to separate small
portions of code which might be used at multiple other places in your code.
so yes, calling methods from within other methods is a good thing to do.
ideally your so called "composed methods" read out like a little story:
public void transaction(){
openDatabaseConnection();
addRecordsToDatabase();
closeDataseConnection();
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
How important is it to use labels in Java? I haven't seen labels used, except in academic books.
I saw them used with jump statements such as break and continue.
You can use labels, but they are considered bad form in general, sort of unrestrained jumping within a method, it makes the code harder to maintain and can introduce bugs if not handled carefully.
As a rule with OO there is usually an easier/better way to achieve things.
In too many years of coding Java I have never used a label.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Let's say you have some issue to develop. And as recommended practice it is good idea to use interfaces ( I don't mean GUI, I mean interface or abstract class ). And you can apply two ( I'm pretty sure, but for now I noticed I apply two ) ways:
Design interfaces upfront and then implement them.
Implement classes and then on basics of classes discover interface.
Personally I prefer second option, but during discussions with other developers I noticed that somebody prefers first approach. I can say that I prefer second approach for the following reasons:
I can faster write code
I avoid unnesessary code ( something that I never will use )
Interfaces in that case are more binded to "real" life
For me it is more convenient.
I'd like to hear other advices why somebody prefers option 1 or option 2.
As usually I code in C#, but AFAIK java also have idea of interfaces
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am developing an Android unit conversion and calculator application in which I have a 3 fragment ViewPager set up and was wondering what would be the best OOP design practice when attempting to use methods over many fragments. Should I set up a centralized calculate method and call it within each fragment?
I currently have each fragment isolated with their calculations, spinner values, and intermediary values. I am also sending a result of a calculation from a fragment back to another fragment through an intent on the Main Activity.
My code currently works, but as I am learning more Java I would like to start piecing together best design practices and efficient code. Thanks for any insight!
If possible, i'd write something like CalculationUtils.java and define every common method inside, as public static, and later call them with CalculationUtils.method1(...)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am currently working on a project where I found this syntax:
Method m = bluetoothDevice.getClass().getMethod("createBond", (Class[]) null);
What is the purpose of "Method" class in Java and why we use it? Please elaborate with an example
Thanks in advance!
PS: I already saw the Java docs but not able to understand it.
The Method class is part of the "reflection" API which is about meta-programming. That means you can deal with structures of your program as data and process it in a java program. This allows flexible generic or abstract solutions. Method itself just represents a method in a Java class. There are other classes representing other parts of Java programs, too (e.g. Class).