我是一个初学者,我总是读到重复编码是不好的。但是,似乎为了不这样做,通常必须有额外的方法调用。假设我有以下课程
public class BinarySearchTree<E extends Comparable<E>>{
private BinaryTree<E> root;
private final BinaryTree<E> EMPTY = new BinaryTree<E>();
private int count;
private Comparator<E> ordering;
public BinarySearchTree(Comparator<E> order){
ordering = order;
clear();
}
public void clear(){
root = EMPTY;
count = 0;
}
}
对于我来说,只复制并粘贴 clear ()方法中的两行代码到构造函数中,而不调用实际的方法,是否更好?如果是这样,那又有什么区别呢?如果我的构造函数调用了10个方法,每个方法只是简单地设置了一个值的实例变量,结果会怎样?最好的编程实践是什么?