简体   繁体   English

需要有关编码最佳实践的建议

[英]Advice needed on coding best practices

I want to understand if the following coding practice is good or bad practice. 我想了解以下编码做法是好是坏。

I pass a Value Object to a method as a parameter and the called method is returning the same parameter Value Object. 我将值对象作为参数传递给方法,并且被调用的方法返回相同的参数值对象。 I particulary think since it the same object be referenced we dont need to put it as return type. 我特别认为,由于引用了相同的对象,因此我们无需将其作为返回类型。

Class A
{
  initStudent()
  {
    Student studentObj = new Student();
    //do some processing

    studentObj = processStudent(studentObj);
  }

  processStudent(Student pObj)
  {
    //do something

    return pObj;
  }
}

it is sometimes needed to get a parameter and return a parameter of the same type, even if the manipulation is on the parameter for some reasons: 有时出于某些原因,即使对该参数进行了操作,有时也需要获取一个参数并返回相同类型的参数:
1. if the object is immutable (ie String) 1.如果对象是不可变的(即字符串)
2. if the object actualy can be changed (a good example for it is <T>[] List.toArray(<T>[]) where if there is not enough space in the parameter array, a new one is created, otherwise, the array is written on the parameter) 2.如果实际上可以更改对象(例如<T>[] List.toArray(<T>[])就是一个很好的例子,如果参数数组中没有足够的空间,则会创建一个新对象,否则,数组写在参数上)
3. it also implicitly tells the user the value might be changed... 3.它还隐式地告诉用户该值可能会更改...
4. don't be afraid to do it if needed, many projects use it (ie Apache Lucene) 4.不要害怕在需要时执行它,许多项目都在使用它(例如Apache Lucene)

Yes you shouldn't be returning it. 是的,您不应该退货。 Use access modifiers and return types properly. 使用访问修饰符并正确返回类型。

public class A
{

  public Student initStudent()
  {
     Student studentObj = new Student();
     //do some processing

     processStudent(studentObj);

     return studentObj;
  }

  private processStudent(Student pObj)
  {
       //do something
  }
}

Indeed you don't need to return the object. 实际上,您不需要返回该对象。 Just return nothing. 什么都不要返回。

Class A
{

  void initStudent()
  {
     Student studentObj = new Student();
   //do some processing
    processStudent(studentObj);
  }

 private void processStudent(Student pObj)
   {
       //do something
   }

}

Edit: Also, as adarshr pointed out, the Student object that you created will be of no use if it does not "go out" of the initStudent() method. 编辑:此外,正如adarshr所指出的,如果您创建的Student对象没有“脱离” initStudent()方法,则将initStudent() Probably you may want to return it, or store it in a Collection instance field. 您可能要返回它,或将其存储在Collection实例字段中。

I think it is a good practice to return the object if the method modifies it. 我认为,如果方法对其进行了修改,则返回该对象是一个好习惯。 So the signature of the method makes it clear that the object gets modified and you don't have implicit side effects. 因此,该方法的签名清楚地表明对象已被修改,并且您没有隐式的副作用。

If the method is not intended to modify the object you should not return it. 如果该方法无意修改该对象,则不应返回该对象。

what about setting studentObj as a member variable as it's the same class 将studentObj设置为成员变量,因为它是同一类怎么办

EDIT: I suppose that processStudent will call some methods on Student, which is called feature envy - that method 'wishes to be in Student'. 编辑:我想processStudent将在Student上调用某些方法,这称为功能嫉妒-该方法“希望在Student中”。 This is not a good practice, instead create that processing inside Student class by creating new method 这不是一个好习惯,而是通过创建新方法在Student类中创建该处理

I would rather aim toward a dependency injection combined with the decorator design pattern 我宁愿瞄准结合装饰器设计模式的依赖注入

In the main code I would handle the student initialisation and pass it to get it decorated 在主代码中,我将处理学生的初始化并将其传递给它进行修饰

studentObj = new Student();

decoratedStudentObj = new A(studentObj);

finalStudentObj = decoratedStudentObj.getStudent()

Which would trigger the getStudent() method in all of the different classes and modify all the student object in casacade. 这将触发所有不同类中的getStudent()方法并修改casacade中的所有student对象。

So you could eventually end up with something like this : 因此,您最终可能会得到如下所示的结果:

studentObj = new Student();

decoratedStudentObj = new A(studentObj);

decoratedStudentObj = new A(decoratedStudentObj );

decoratedStudentObj = new B(decoratedStudentObj);

decoratedStudentObj = new C(decoratedStudentObj );

finalStudentObj = decoratedStudentObj.getStudent()

An other advantage from dependency injection would allow you to have different student types defined and you would have to duplicate any of your code as long as all students objects implements the same interface/method set 依赖项注入的另一个优点是,您可以定义不同的学生类型,并且只要所有Student对象都实现相同的接口/方法集,就必须复制任何代码。

IE: IE浏览器:

studentObj = new Student()

decoratedStudentObj = new A(studentObj);

finalStudentObj = decoratedStudentObj.getStudent()


highSchoolStudentObj = new HighSchoolStudent()

decoratedHighSchoolStudentObj  = new A(highSchoolStudentObj);

decoratedHighSchoolStudentObj = new B(decoratedHighSchoolStudentObj );

finalHighSchoolStudentObj   = decoratedHighSchoolStudentObj .getStudent()


collegeStudentObj = new CollegeStudent()

decoratedCollegeStudentObj  = new A(collegeStudentObj );

decoratedCollegeStudentObj  = new C(decoratedCollegeStudentObj);

finalCollegeStudentObj  = decoratedCollegeStudentObj.getStudent()

And finally its easier to implement UnitTesting since you can easily mock the student class to test your processing class independantly. 最后,它更易于实现UnitTesting,因为您可以轻松模拟学生班级来独立地测试您的处理班级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM