I am new to software development. I did the Mars Rover Kata. I evolved the code by trying to apply some of the S.O.L.I.D principals as described in Uncle Bob's books. Right now I am not confident in my understanding of the principals. In order to develop my understanding and confidence, I am experimenting with adding additional functionality to the Rover code to see if the principals hold true.
The first new functionality I decided to add is new movement. Currently the code only provides for movement in a 2D plane: Right, Left, Forward, Backward. I thought I would add Up and Down. The first change I made was to my Point class. The Point class was defined by x and y. In order to provide the additional functionality I needed to add z.
Thanks for any feedback to help me learn,
Brian
For this one class did I properly apply the principals?
SRP — Seems like this is covered. The class only describes a single point. Or is the fact that it defines both 2D and 3D points a violation?
OCP — Are the modifications I did consistent with: software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification? I did not change a single character of the existing code. I only added new code. Or have I fundamentally changed the definition of Point and, therefore, essentially rewritten the whole class?
LSP — I will be honest I do not understand this one. But since I changed the definition Point instead of subclassing I am not sure this applies. NOTE: After changing the definition of Point all my tests still passes so maybe it does apply since I substituted a 2D point for a 3D point and the code was unaffected. Did I mention I do not really understand this principal?
ISP — I did not try to implement this principal yet. Later I may add a different user interface beyond the tests and get into it. If there is anything in the existing code that could benefit from this, I would welcome comments, but that is only for the ambitious who want to go through all the code.
DIP — I did not try to implement this principal yet either. If there is anything in the existing code that could benefit from this, I would welcome comments, but that again is only for the ambitious who want to go through all the code.
package rover;
public class Point {
private int x;
private int y;
private int z = 0; // New code
public Point(int x, int y) {
this.setX(x);
this.setY(y);
}
public Point(int x, int y, int z) { // New code
this.setX(x); // New code
this.setY(y); // New code
this.setZ(z); // New code
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
private void setZ(int z) { // New code
this.z = z;// New code
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() { // New code
return z; // New code
}
}