How to refactor method?

Let’s learn how to refactor method. The most accurate or helpful solution is served by Stack Overflow.

There are ten answers to this question.

Best solution

How to refactor a method to make it easier to test

Below is a method that I'm having a hard time figuring out how to test using JUnit. This method is difficult to test because it depends on the results of other methods (e.g. getClosestDcoumentCode). Based on my reading of JUnit, this suggests I should refactor the method. But how? And if refactoring is not necessary, how do you test a method that depends on other methods? Thank you, Elliott private static String findPrincipal(List<DocumentKey> documentkeys_) { Hashtable<String, Integer...

Answer:

It sounds to me like getClosestCode and getClosestDocumentCode belong to a different set of responsibilities...

Read more

Elliott at Stack Overflow Mark as irrelevant Undo

Other solutions

What is a good way to refactor Java methods with common code and a method call in the middle?

Given a method like this: private void longMethod() { // Long series of code anotherMethod(); // Long series of code } What is a good way to support a need to make the anotherMethod() call replaceable? Hypothetically, if Java supports functions as...

Answer:

Maybe what you're looking for is as simple as the pattern called Template Method? Wikipedia has it,...

Read more

Kjetil Valstadsve at Quora Mark as irrelevant Undo

How do I call fetch method from within a render method in Backbone.js?

Hi I am trying to call fetch method to  get a collection of models from inside a render method but the before  the fetch method gets called other lines of the render method are  executed and finally the fetch method is called. Can  anyone help me to...

Answer:

You could override the fetch method and trigger callbacks on success and error of your fetch method...

Read more

Yasser R Shaikh at Quora Mark as irrelevant Undo

How come making a local variable final would solve the compiler error issue inside the method local inner class in Java?

The SCJP book by Kathy Sierra says,  However, the inner class object cannot use the local variables of the method the inner class is in. Why not? Think about it. The local variables of the method live on the stack, and exist only for the lifetime of...

Answer:

The compiler could just allow access to local variables from an inner class; it could do so by copying...

Read more

Daniel Kinzler at Quora Mark as irrelevant Undo

How can you use the compareTo method with static variables?

right now i'm trying to progam a program where you enter two radius' and in the object file it prints out the circumference and area using the toString method and then compares the two circles using the compareTo method. my problem is if i don't make...

Answer:

Try to avoid using static variables unless you mean to share that data between classes (not objects...

Read more

Grace L at Yahoo! Answers Mark as irrelevant Undo

How to use this method in java? Using a method in java?

hello there, i been having a problem in my grading program and i need some feedback some help setting up a method so it will return to its original value...wat i need to do is to remove the nested if expression for letter grade calculation from the main...

Answer:

Use a case statement.

Read more

Misiekj at Yahoo! Answers Mark as irrelevant Undo

How do you do the calendar method? and the cervical mucus method?

i really don't get how to use this method... and i have another question... in the failure rate of calendar method? is ejaculating inside part of the % of the failure rate? or is the 15% failure rate of calendar method only consist of withdrawal method...

Answer:

I think you need to explain more on what your question is I'm so lost

Read more

Molly Jane at Yahoo! Answers Mark as irrelevant Undo

How do i call a string method in main?

I was given instruction as follow: String inintials( String first, String last): this mothod should return a String that consist of of the first letter of the first formal parameter, first, and the first lettter of the second formal parameter, last....

Answer:

You need to write the method before you can invoke it. You've posted this question just a few minutes...

Read more

◄Ferroci... at Yahoo! Answers Mark as irrelevant Undo

How do i call a string method in main?

I was given instruction as follow: String inintials( String first, String last): this mothod should return a String that consist of of the first letter of the first formal parameter, first, and the first lettter of the second formal parameter, last....

Answer:

You basically already did that, just need to create a new method // this given method returns a string...

Read more

◄Ferroci... at Yahoo! Answers Mark as irrelevant Undo

How do I call my method in Java?

So I have a method that takes in a integer as a menu selection, and then runs a loop based on the selection. The loop is always the same, but the menu selection changes an integer in the loop. Anyway, how do I call this method in my main method? I prompt...

Answer:

Can it be your function is declared "public int compute(...)" for now? Try "public static...

Read more

justme at Yahoo! Answers Mark as irrelevant Undo

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.