Properties versus methods







At first glance, a question like choosing a property or method seems simple. But that's until you run into misunderstandings in your team. Although there are well-established practices, their wording is rather vague. There is a certain degree of freedom in such questions, which makes our choice difficult, and the apparent simplicity gives fertile ground for disputes.







Background of Java programmers



โ€” . - . , , Java , .







:







public class Point {
    public double x;
    public double y;
}
      
      





, . ? -, , . -, , .







, , , :







public class Point {

    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }
}
      
      





Java, IDE .

get set.







, , .









get set:







class Point(var x: Double, var y: Double)
      
      





, ? : x



getX()



.







:







var x: Double = 0 
    set(value) {
        if (value >= 0) field = value
    }
      
      





, . , .







, . , Java get set , . , . ? , .









, read-only .







, , :







  • (exception)
  • ( )
  • ,


. -, . .







, User:







class User(
    val firstName: String,
    val lastName: String
)
      
      





?







val fullName get() = "$firstName $lastName"
      
      





String, . equals. fullName, .







.









, . ? , , . -: , . , , .







:







class DocumentModel {
     val activePageIndex: Int
}
      
      





, activePageIndex, . , , , :







images.forEach { image ->
   document addImage(image, document.activePageIndex)
}
      
      





, , , . , :







val pageIndex = document.activePageIndex
images.forEach { image ->
   document addImage(image, pageIndex)
}
      
      





, . , , , . , . , - -, , findActivePageIndex.









, , , . , . , . , , ? โ€” . Java . , , get set .







, -, . , API . . , API , .







, IDE, . , . , .







โ€” , . . , . . , .









, , , . . , ?







. , :







  • . , . .
  • . , . .

    .


, .







, get/set, .




, :







interface Point {
    var x: Double
    var y: Double
}
      
      





?







-, x y. , , . , .







x y :







interface Point {
    val x: Double
    val y: Double
    fun setCoordinates(x: Double, y: Double)
}
      
      





Secondly, the interface is not flexible enough. Sometimes it is convenient to work with polar coordinates, but in the interface only rectangular ones. Thus, we implicitly disclose the implementation.







Let's expand the interface:







interface Point {
    val x: Double
    val y: Double
    val radius: Double
    val angle: Double
    fun setCartesian(x: Double, y: Double)
    fun setPolar(radius: Double, angle: Double)
}
      
      





As we can see, it is not so easy to design a good interface. Although it could be limited to the data-class:







data class Point(
    var x: Double,
    var y: Double
)
      
      






All Articles