有没有一种方法来重构这些 if 语句?

huangapple 未分类评论71阅读模式
英文:

Is there a way to refactor these if statements?

问题

只是想知道是否有一种方法重构下面的代码?我对Java还不太熟悉,正在尝试编写DRY(不重复原则)的代码 - 下面是我写的代码,但似乎有很多条件需要检查。

  1. void printDirection() {
  2. if (yDirection > 0) {
  3. if (xDirection < 0) {
  4. println("Travelling South-West");
  5. } else {
  6. println("Travelling South-East");
  7. }
  8. } else if (yDirection < 0) {
  9. if (xDirection < 0) {
  10. println("Travelling North-West");
  11. } else {
  12. println("Travelling North-East");
  13. }
  14. }
  15. }

提前感谢任何帮助!

英文:

just wondering if there's a way to refactor the below code? I'm new to Java and trying to have DRY code - the below I've written but seems like a lot of conditionals to check

  1. void printDirection() {
  2. if (yDirection &gt; 0) {
  3. if (xDirection &lt; 0) {
  4. println(&quot;Travelling South-West&quot;);
  5. } else {
  6. println(&quot;Travelling South-East&quot;);
  7. }
  8. } else if (yDirection &lt; 0) {
  9. if (xDirection &lt;0) {
  10. println(&quot;Travelling North-West&quot;);
  11. } else {
  12. println(&quot;Travelling North-East&quot;);
  13. }
  14. }
  15. }

Thanks in advance for any help!

答案1

得分: 8

你可以分别评估北/南和东/西的条件,然后将这些方向组合成你的消息。

  1. System.out.printf("旅行 %s-%s%n", (yDirection < 0 ? "北" : "南"),
  2. (xDirection < 0 ? "西" : "东"));

我从你问题中的代码推断出你只关心这四个互补的方向(而不是正北、正东、静止等)。

英文:

You can evaluate the north/south and the east/west conditions individually, and glue the directions into your message.

  1. System.out.printf(&quot;Travelling %s-%s%n&quot;, (yDirection &lt; 0 ? &quot;North&quot; : &quot;South&quot;),
  2. (xDirection &lt; 0 ? &quot;West&quot; : &quot;East&quot;));

I assume from the code in your question that you're only concerned about those four complementary directions (not due north, due east, stationary etc.).

答案2

得分: 1

如果您真的想使其更加DRY,可以使用运算符?来实现,但这既不容易阅读也不推荐。它在编程竞赛中被用于尽可能快地进行操作。

它遵循以下模式:
(条件?条件为真时发生的情况:条件为假时发生的情况);
您可以在赋值中使用它:

  1. int i = (a>0)?a:0;

在这种情况下,如果a>0,则i=a,否则a=0。

在您的情况下,我会这样做:

  1. void printDirection() {
  2. System.out.println("Travelling " + (yDirection > 0 ? "South" : "North") + "-" + (xDirection > 0 ? "East" : "West"));
  3. }
英文:

If you really want to make it DRY, it can be done using the operator ? but It's neither easy to read nor recommanded. It's used in programming contest where the goal is to go as fast as possible.

It follows the scheme :
(Condition?WhatHappenIfConditionIsTrue:WhatHappenIfConditionIsFalse);
You can use it in assignment :

  1. int i = (a&gt;0)?a:0;

in that case, if a>0 then i=a, else a=0

In your case, I would do it like that

  1. void printDirection()
  2. {
  3. System.out.println(&quot;Travelling &quot; + (yDirection &gt; 0?&quot;South&quot;:&quot;North&quot;) + &quot;-&quot; + (xDirection&gt;0?&quot;East&quot;:&quot;West&quot;));
  4. }

答案3

得分: 0

  1. public class Status {
  2. public enum Direction {
  3. SOUTH_WEST((x, y) -> y > 0 && x < 0, "Travelling South-West"),
  4. SOUTH_EAST((x, y) -> y > 0 && x > 0, "Travelling South-East"),
  5. NORTH_EAST((x, y) -> x > 0 && y < 0, "Travelling North-East"),
  6. NORTH_WEST((x, y) -> x < 0 && y < 0, "Travelling North-West"),
  7. CENTER((x, y) -> x == 0 && y == 0, "");
  8. BiPredicate<Integer, Integer> bp;
  9. String desc;
  10. private Direction(BiPredicate<Integer, Integer> bp, String desc) {
  11. this.bp = bp;
  12. this.desc = desc;
  13. }
  14. public static Direction getDirection(int x, int y) {
  15. for (Direction direction : Direction.values()) {
  16. if (direction.bp.test(x, y)) {
  17. return direction;
  18. }
  19. }
  20. return null;
  21. }
  22. }
  23. public static void main(String[] args) {
  24. Direction d = Direction.getDirection(3, 4);
  25. System.out.println(d.desc);
  26. /*if (d == Direction.SOUTH_WEST) {
  27. System.out.println("do some thing");
  28. } else if (d == Direction.SOUTH_EAST) {
  29. System.out.println("do some thing");
  30. } else if (d == Direction.NORTH_EAST) {
  31. System.out.println("do some thing");
  32. } else if (d == Direction.NORTH_WEST) {
  33. System.out.println("do some thing");
  34. }*/
  35. }
  36. }
英文:

Some suggestiones:

  1. Due to x,y combination; there are five states; you can use enum type to define these status;
  2. If you want to reduce if...else statementes in your code, please refer to Status Machine Design Pattern; but i think, under your case, the status is so simple, do not need to make it too complicated
  1. public class Status {
  2. public enum Direction {
  3. SOUTH_WEST((x, y) -&gt; y &gt; 0 &amp;&amp; x &lt; 0, &quot;Travelling South-West&quot;)
  4. , SOUTH_EAST((x, y) -&gt; y &gt;0 &amp;&amp; x &gt; 0, &quot;Travelling South-East&quot;)
  5. , NORTH_EAST((x, y) -&gt; x &gt; 0 &amp;&amp; y &lt; 0, &quot;Travelling North-East&quot;)
  6. , NORTH_WEST((x,y) -&gt; x &lt; 0 &amp;&amp; y &lt; 0, &quot;Travelling North-West&quot;), CENTER((x,y) -&gt; x == 0 &amp;&amp; y == 0, &quot;&quot;);
  7. BiPredicate&lt;Integer, Integer&gt; bp;
  8. String desc;
  9. public BiPredicate&lt;Integer, Integer&gt; getBp() {
  10. return bp;
  11. }
  12. public void setBp(BiPredicate&lt;Integer, Integer&gt; bp) {
  13. this.bp = bp;
  14. }
  15. public String getDesc() {
  16. return desc;
  17. }
  18. public void setDesc(String desc) {
  19. this.desc = desc;
  20. }
  21. private Direction(BiPredicate&lt;Integer, Integer&gt; bp, String desc) {
  22. this.bp = bp;
  23. this.desc = desc;
  24. }
  25. public static Direction getDirection(int x, int y) {
  26. for (Direction direction : Direction.values()) {
  27. if(direction.getBp().test(x, y)) {
  28. return direction;
  29. }
  30. }
  31. return null;
  32. }
  33. }
  34. public static void main(String[] args) {
  35. Direction d = Direction.getDirection(3, 4);
  36. System.out.println(d.getDesc());
  37. /* if(d == Direction.SOUTH_WEST){
  38. System.out.println(&quot;do some thing&quot;);
  39. } else if(d == Direction.SOUTH_EAST){
  40. System.out.println(&quot;do some thing&quot;);
  41. } else if(d == Direction.NORTH_EAST){
  42. System.out.println(&quot;do some thing&quot;);
  43. } else if(d == Direction.NORTH_WEST){
  44. System.out.println(&quot;do some thing&quot;);
  45. }*/
  46. }
  47. }

huangapple
  • 本文由 发表于 2020年3月15日 20:43:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/60693002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定