-1

I'm new to programming in Java and am currently working on a program to move a theoretical robot. Would it be more efficient/better practice to have a method like

public void turn(String direction, double power) { /* code */ }

or like

public void turnLeft(double power) { /* code */ }
public void turnRight(double power) { /* code */ }

Is it purely up to preference/readability or is there a standard?

mariuss
  • 1,177
  • 2
  • 14
  • 30
  • 2
    Why not all three? Implement all three methods, thus a user can call `turn(String, double)` if s/he likes, but you can also provide `turnLeft(double)` and `turnRight(double)` for convenience. Some remarks: instead of passing a `String` you could pass a custom `Direction` enum -- instead of `power`, I would suggest a more speaking name, e.g. `rad` (for radians) or `degree` (for - well - degrees). – Turing85 Oct 17 '18 at 15:56
  • It's up to you. One problem I see with `turnLeft` and `turnRight` is that a robot might do something in between those two extremes. And then you might not have an easy time phrasing that behavior with just two methods. – Tim Biegeleisen Oct 17 '18 at 15:56
  • I think it is totally up to you whether to have one turn method or turnLeft, turnRight ...just follow the method which will be easy to debug and understand for you (or anyone else) later – mettleap Oct 17 '18 at 15:58
  • I'll prefer one method only as power without direction seems useless and one method can have all the logic hence better maintainability. Had it been both the methods completely independent, then would have kept separate for loose coupling. – Pushpesh Kumar Rajwanshi Oct 17 '18 at 15:58
  • @PushpeshKumarRajwanshi having only one method with only power takes away flexibility. What if for some reason the user wants to turn right by 270 degree instead of left by 90 degree? – Turing85 Oct 17 '18 at 15:59
  • @Turing85: How did you even assume that. I never said with one method there will only be one parameter, power. It will have to have both the arguments but since power without direction doesn't mean something significant hence dividing them into two method makes code less maintainable. – Pushpesh Kumar Rajwanshi Oct 17 '18 at 16:02
  • 1
    Note that design and style questions are generally better suited for [softwareengineering.se] than here; our focus is quite narrowly on specific problems. – Charles Duffy Oct 17 '18 at 16:33

3 Answers3

2

public void turn(String direction, double power) has a problem where the direction is a String and hence the caller can pass a value that is not valid. It is advisable to use an Enum here.

enum Direction {
  LEFT, RIGHT,...
}

I would say it is subjective to choose one... I would prefer to have one method for each direction though. Maybe if the enum instances are more than a handful, it makes sense to have just one method. If you add a new direction (say ZIGZAG), no new method needs to be added (but still your implementation needs to handle that new enum/direction).

Think about how the callers will have to call and choose the one that makes more sense.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
1

I'll suggest instead of left or right, the method should take degree and whether it is clockwise or anticlockwise (as long as we stay in context of 2d) hence a boolean.

Hence method should have following signatures,

public void turn(boolean clockwise, double degree, double power) { /* code */ }

This way a robot won't be restriced only for 90 degree movement.

And splitting it into two methods doesn't logically seem right.

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • Why go with a(n unreadable) `boolean`, if one could use something readable like `Direction.(LEFT | RIGHT)`? – Turing85 Oct 17 '18 at 16:11
  • @Turing85: What if robot wants to move 30 degree clockwise? – Pushpesh Kumar Rajwanshi Oct 17 '18 at 16:13
  • If we can agree that a clockwise rotation is a right rotation and that the method interprets `power` as degree, then it would be `turn(Direction.RIGHT, 30.0d)` – Turing85 Oct 17 '18 at 16:16
  • You don't need to forcefully use enum when boolean is more than enough. In rotation context, there can only be two directions either clockwise or anticlockwise. Debating on whether to use enum vs boolean is hardly worth it. BTW boolean (native and just takes one bit) is much faster than an enum :) – Pushpesh Kumar Rajwanshi Oct 17 '18 at 16:19
  • 1
    "*You don't need to forcefully use enum when boolean is more than enough.*" - Sure, but you still have the ambiguity of what `true` means (is it clockwise? counter-clockwise? Left? Right?...), which is eliminated by using an `Enum` with a descriptive name -- "*BTW boolean is much faster than an enum*" - I doubt that. You have some marginal overhead for initial object creation (i.e. enum instances). The comparison of an enum is the comparison of a memory address and thus an atomic operation. Do you have some performace data to proof your claim? – Turing85 Oct 17 '18 at 16:22
  • If I am not wrong, enums are nothing but final static object of class variable. And I don't think comparison of one bit against one object would be faster. There is nothing like I am against using enum. I do use enum whenever I feel the need of it. And even here if we use enum instead of boolean, would hardly make a difference. My main point in my answer is, it can't be just enum with right or left. It has to be a degree too so robots are not restricted to 90 degree movement. – Pushpesh Kumar Rajwanshi Oct 17 '18 at 16:27
  • "*And I don't think comparison of one bit against one object would be faster.*" - Since `Enum`s are basically multitons, you can compare them with `==`, which is the comparison of a memory address and thus an atomic operation. This is neither slower nor faster than the comparison of a `boolean`. Keep in mind that, on bytecode-level, every `boolean` is an `int` and thus at least 32 bits long. – Turing85 Oct 17 '18 at 16:32
0

The second one is more readable. Generally an auto-descriptive method or variable is best.

If you want to use the first method, my hint is to use an Enum instead of a String. With the Enum you avoid a typing error (left vs LEFT) and the code is more readable.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64