سؤال

What I want to be able to do is write many lines of code just once and copy and paste it into multiple methods.

  1. I could just simply copy and paste but that would double the number of lines of code, making it harder to read.
  2. I could create a method that contains those many lines of code and then call that method whenever I need those lines; however, this means I would not be able to use the local variables I declared.

    A methodA(Object object) {
        doC();
        // I cannot do additional things with the local variables declared in C
    }
    
    B methodB(Object object) {
        doC();
    }
    
    doC() {
        // Delcare and use a bunch of local variables
    }
    

My specific problem is that I need to access a local variable from a method that it calls. It would be great if there was something similar to a class full of constants but instead a class full of lines of code that you can access from anywhere.

A methodA(Object object) {
    B b = extractedMethod(object);
    A a = new A(b);
    Tools.staticMethod(a, file); // file cannot be resolved
    return A;
}

B extractedMethod(Object object) {
    // A lot of code...
    final File file = getSelectedFile();
    // More code...
    return B;
}

Purpose

Originally, only methodA existed, and that method retrieved an URI wrapped it up in an custom object, and then returned it after calling a method to update some progress icons. However, I would now like to create a methodB that would simply return that URI, as I don’t need it wrapped up in a custom object. I thought it was rather extraneous to create a new class when I only needed three lines of code deleted from the original methodA to make it work. In essence, I was looking for some kind of code template like functionality, but at compile-time, so that I could just type “xyz” for the “a lot of lines” (around 20) that I wanted to substitute it with. I now understand the evil of code-duplication.

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

المحلول

In the paradigm of procedural programming (and in OOP as well), such thing is not only impossible, but also wrong. If you need to access the local variables of a block of code outside of it, it means that you structured the whole block in the wrong way; there are in fact two possibilities:

  1. The operation you want to accomplish on the local variables is logically part of the block containing such variables. In this case, simply extend it.
  2. You need the value of the local variables outside the block containing them, to perform different operations every time (I understand this is your case). In this situation, the variables are then logically part of the result of the execution of the block containing them. A clean, correct way to handle this is to encapsulate said block of code, and to return a structured data type that contains every value you need to access outside of it.

نصائح أخرى

If you want to access local variables for reading only, pass their values as parameters. If you want write access also, convert them into instance variables - either of the current class, or better create a new class.

method 1

class ABC{
// a , b , c are variables of any type

A methodA(Object object) {
    doC();
    // I cannot do additional things with the local variables declared in C
  // use a b c here 
}

B methodB(Object object) {
    doC();
  // use a b c here 
}

doC() {
    // Delcare and use a bunch of local variables
  // use a b c here 
}}

method 2

class C {

 // a, b ,c 
 // list<Object> abcList = new arrayList<Object>;

doC() {
        // Delcare and use a bunch of local variables
      // use a b c here 

// abcList.add(a);
// abcList.add(b);
// abcList.add(c);

A methodA(Object object, abcList);
B methodB(Object object, abcList)

}

}

class AB {

 A methodA(Object object, List<Object> abcList) {
        doC();
        // I cannot do additional things with the local variables declared in C
      // use a b c here 
      // get value of a,b,c from abcList;
    }

    B methodB(Object object, List<Object> abcList) {
        doC();
      // use a b c here 
// get value of a,b,c from abcList;
    }

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