Questions about S.O.L.I.D from Novice

112 views
Skip to first unread message

Brian Gibson

unread,
Mar 19, 2015, 12:50:32 PM3/19/15
to clean-code...@googlegroups.com

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.yy;

}


private void setZ(int z) { // New code

this.zz;// New code

}


public int getX() {

return x;

}


public int getY() {

return y;

}


public int getZ() { // New code

return z; // New code

}

}

Norbert Nemes

unread,
Mar 20, 2015, 9:19:23 PM3/20/15
to clean-code...@googlegroups.com
Yes, you violated OCP and ISP. But only if your code contains classes that only use 2D points, while others only use 3D points. Look at the OCP definition you wrote. By adding the new field, you modified the existing Point class. So now all classes that depend on Point, need to be recompiled and redeployed. With this change, you also violated ISP, because all the classes that were happy using 2D points, now have to know about this third parameter, and drag it around, despite never using it. The correct way to extend your code while respecting OCP would have been by creating a Point3D class that inherits from Point, and has the extra z parameter. Of course, if ALL your clases, without exception, have to use 3D points now instead of 2D points, your solution is the way to go.
Reply all
Reply to author
Forward
0 new messages