3

我一直在想一个基本问题。也许它太微不足道了 - 但我还是决定征求意见。

下面是一个示例代码:

class seventeenth{

    public static void appendtolist(List<Integer> i){
        i.add(new Random().nextInt(1000));
        i.add(new Random().nextInt(1000));
        i.add(new Random().nextInt(1000));
    }
    public static List<Integer>  returnlist(){
        List<Integer> i=new LinkedList<Integer>();
        i.add(new Random().nextInt(1000));
        i.add(new Random().nextInt(1000));
        i.add(new Random().nextInt(1000));
        return i;
    }
    public static void main(String[] args){
        List<Integer> l=new LinkedList<Integer>();
        appendtolist(l);//Option 1
        l=returnlist();//Option 2
        for(Integer e:l)
        System.out.println(e);
    }

}

上面哪个选项是一个好的编程习惯,为什么?或者真的不重要?如果有人可以分享任何关于这样的基本良好编程约定的文献,将不胜感激。

4

7 回答 7

3

两者都很好,只要名称使功能清晰。

“附加到”版本更通用一些,因为它可以被多次调用以附加到同一个列表。以这种方式使用“返回”版本需要将所有内容复制两次。

我可能会将“附加到”版本概括为任何Collection

public static void addTo(Collection<Integer> coll) {
    coll.add(random.nextInt(1000));
    coll.add(random.nextInt(1000));
    coll.add(random.nextInt(1000));
}
于 2013-04-09T14:15:32.923 回答
2

一种好的做法是尽可能多地重用对象,而不是

i.add(new Random().nextInt(1000));
i.add(new Random().nextInt(1000));

你会改为使用

Random rand = new Random();
i.add(rand.nextInt(1000));
i.add(rand.nextInt(1000));

使用第一种方法,您有创建和垃圾收集两个 Random 对象的开销,而第二种方法只创建和垃圾收集一个 Random 对象。

于 2013-04-09T14:16:02.213 回答
0

选项 2,

public static List<Integer>  returnlist(){
    List<Integer> i=new LinkedList<Integer>();
    i.add(new Random().nextInt(1000));
    i.add(new Random().nextInt(1000));
    i.add(new Random().nextInt(1000));
    return i;
}

我认为要好得多,除非您想继续向列表中添加更多元素l

于 2013-04-09T14:15:12.857 回答
0

这取决于函数的含义,第一个的工作是改变一个列表,另一个是构造一个。
我会使用方法重载来允许没有代码重复的任一选项:

public static List<Integer> addRandom(List<Integer> i){
    i.add(new Random().nextInt(1000));
    i.add(new Random().nextInt(1000));
    i.add(new Random().nextInt(1000));
    return i;
}
public static List<Integer> addRandom() {
    return addRandom(new LinkedList<Integer>());
}
public static void main(String[] args){
    List<Integer> l=new LinkedList<Integer>();
    addRandom(l);//Option 1
    l = addRandom();//Option 2
    for(Integer e:l)
    System.out.println(e);
}

同样,它们的含义不同,但是如果您真的想比较两者,请注意 returnList 强制执行 List (LinkedList) 和事实

于 2013-04-09T14:20:08.023 回答
0

对方法名使用驼峰式大小写,对类名使用pascal 大小写也是一种很好的做法。 1>您的两种方法都在多次
创建对象。new Random()应该避免。
2> 数字生成不应像 1000 那样硬编码。将来您可能需要 2000。
3> 在 returnList 方法中,您只返回LinkedList。但将来你可能需要它返回 Vector、ArrayList。所以与代码有紧密的联系。它不灵活。

对于哪个好的问题

我总是更喜欢appendToList,因为我可以在该方法中传递任何对象,例如 Vector、ArrayList 或 LinkedList。

对方法进行以下修改可能有助于良好的实践。

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

class Seventeenth {

    public static void appendToList(Random random, List<Integer> list, int[] numbers) {
        for (int i : numbers) {
            list.add(random.nextInt(i));
        }
    }

    public static void main(String[] args) {
        List<Integer> list = new LinkedList<Integer>();
        Random random = new Random();
        appendToList(random, list, new int[] { 1, 2, 3, 1000, 2000 });// Option 1
        for (int e : list)
            System.out.println(e);
    }

}
于 2013-04-09T14:22:14.190 回答
0

IMO 我更喜欢returnlist-kind 因为我尽量避免操纵方法的参数,因为这些更改将在代码的整体流程中“隐藏”,这可能会导致以后出现意外行为。但是,由于方法名称appendtolist很清楚即将发生的事情,所以这两种方法对我来说都很好。

于 2013-04-09T14:41:33.030 回答
0

最好不要改变论点,这就是我选择第二个变体的原因。

你的方法除了逻辑和返回值之外不应该做任何事情

更多关于副作用

于 2013-04-09T14:42:25.560 回答