如何改进这段代码? (太多如果)

发布于 2024-07-30 19:23:51 字数 386 浏览 7 评论 0原文

我想打印正方形的边框... 它可能只打印正方形的一侧,或多侧,所以我写了这个方法

printBorder(N, E, S, W) {
  if (N) {
     square.printBorder(0,0,0,10);
  }
  if (E) {
     square.printBorder(0,10,10,10);
  }
  if (S) {
     square.printBorder(10,0,10,10);
  }
  if (W) {
     square.printBorder(0,0,10,0);
  }
}

它可以很好地工作,但我认为它不是那么优雅,它太多了,并且所有语句都或多或少相同。 我认为必须有一种方法可以简化这些代码,有什么建议吗?

I want to print the border of the square...
It may print only one side, or more sides of the square, so I wrote this method

printBorder(N, E, S, W) {
  if (N) {
     square.printBorder(0,0,0,10);
  }
  if (E) {
     square.printBorder(0,10,10,10);
  }
  if (S) {
     square.printBorder(10,0,10,10);
  }
  if (W) {
     square.printBorder(0,0,10,0);
  }
}

It can work fine, but I think it is not so elegant, it is too many if, and all statement is more or less the same. I think there must be have a way to simplify this codes, any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眼眸印温柔 2024-08-06 19:23:51

简化它的一种方法...即使不需要它们也可以进行调用,但要对实现进行条件化:

printBorder(N, E, S, W){
  square.printBorder(n, 0,0,0,10);
  square.printBorder(e, 0,10,10,10);
  square.printBorder(s, 10,0,10,10);
  square.printBorder(w, 0,0,10,0);
}

然后在 Square (或其他)中:

printBorder(condition, top, left, bottom, right) {
  if (!condition) {
    return;
  }
  printBorder(top, left, bottom, right);
}

类似的替代方法是保留条件 printBorder 与原始函数:

printBorder(N, E, S, W){
  printBorder(n, 0,0,0,10);
  printBorder(e, 0,10,10,10);
  printBorder(s, 10,0,10,10);
  printBorder(w, 0,0,10,0);
}

printBorder(condition, top, left, bottom, right) {
  if (!condition) {
    return;
  }
  square.printBorder(top, left, bottom, right);
}

One way of simplifying it... make calls even if you don't need them, but conditionalise the implementation:

printBorder(N, E, S, W){
  square.printBorder(n, 0,0,0,10);
  square.printBorder(e, 0,10,10,10);
  square.printBorder(s, 10,0,10,10);
  square.printBorder(w, 0,0,10,0);
}

Then in Square (or whatever):

printBorder(condition, top, left, bottom, right) {
  if (!condition) {
    return;
  }
  printBorder(top, left, bottom, right);
}

A similar alternative would be to keep the conditional printBorder with the original function:

printBorder(N, E, S, W){
  printBorder(n, 0,0,0,10);
  printBorder(e, 0,10,10,10);
  printBorder(s, 10,0,10,10);
  printBorder(w, 0,0,10,0);
}

printBorder(condition, top, left, bottom, right) {
  if (!condition) {
    return;
  }
  square.printBorder(top, left, bottom, right);
}
二智少女猫性小仙女 2024-08-06 19:23:51

我不会关心如果。 我只是让它更具可读性:

printBorder(N, E, S, W){
  if(N) square.printBorder( 0,  0,  0, 10);
  if(E) square.printBorder( 0, 10, 10, 10);
  if(S) square.printBorder(10,  0, 10, 10);
  if(W) square.printBorder( 0,  0, 10,  0);
}

I wouldn't care about the ifs. I'd just make it more readable:

printBorder(N, E, S, W){
  if(N) square.printBorder( 0,  0,  0, 10);
  if(E) square.printBorder( 0, 10, 10, 10);
  if(S) square.printBorder(10,  0, 10, 10);
  if(W) square.printBorder( 0,  0, 10,  0);
}
装纯掩盖桑 2024-08-06 19:23:51

就我个人而言,我真的很喜欢二进制比较。

const uint NORTH = 1;
const uint SOUTH = 2;
const uint EAST = 4;
const uint WEST = 8;

// ... some code ...
printBorder(NORTH + EAST);
// ... some other code ...

printBorder(uint Sides)
{
   if((NORTH & Sides) > 0) square.printBorder(0, 0, 0, 10);
   if((SOUTH & Sides) > 0) square.printBorder(0, 10, 10, 10);
   if((EAST & Sides) > 0) square.printBorder(10, 0, 10, 10);
   if((WEST & Sides) > 0) square.printBorder(0, 0, 10, 0);
}

有人可能会说这使得函数内部的代码可读性较差。 然而,我的想法是这个函数只出现一次,而你会在各处调用这个函数。 如果您正在运行一些您已经有一段时间没有看过的代码,哪一个更具可读性?

printBorder(true, false, true, true);

或者

printBorder(NORTH + SOUTH + EAST);

只是我的意见。 :)

Personally, I really like binary comparisons.

const uint NORTH = 1;
const uint SOUTH = 2;
const uint EAST = 4;
const uint WEST = 8;

// ... some code ...
printBorder(NORTH + EAST);
// ... some other code ...

printBorder(uint Sides)
{
   if((NORTH & Sides) > 0) square.printBorder(0, 0, 0, 10);
   if((SOUTH & Sides) > 0) square.printBorder(0, 10, 10, 10);
   if((EAST & Sides) > 0) square.printBorder(10, 0, 10, 10);
   if((WEST & Sides) > 0) square.printBorder(0, 0, 10, 0);
}

Some might say that this makes the code inside the function less readable. However, my thinking is there is only a single occurrence of this function whereas you will be calling this function all over the place. If you're running through some code you haven't looked at in awhile which is more readable?

printBorder(true, false, true, true);

or

printBorder(NORTH + SOUTH + EAST);

Just my opinion. :)

×纯※雪 2024-08-06 19:23:51

首先你做得很好,这正是它所表达的,不用担心你正在使用的空间,这里的大多数解决方案只是把水搅浑了。

如果您确实想“做”某事,请查看是否无法将“边框”参数移动到正方形中。 您可以移动边框填充(示例中的 10 到正方形中),也可能移动应显示边框的状态,然后只需调用 square.printBorders() 。 这在很大程度上取决于您使用它的上下文。

First you are doing ok, this does exactly what it expresses, don't worry about the space that you are using, most of the solutions here just muddy the water.

If you really want to 'do' something look if you can't move the Border parameter into the square. you could move the border padding (10 in your example into the square), possibly also the State which border should be shown, and then just call square.printBorders(). That depends a lot on the context where you are using this.

雨后彩虹 2024-08-06 19:23:51

怎么样:

square.printBorder(N|E|W?0:10, N|S|W?0:10, N?0:10, N|E|S?10:0);

How about:

square.printBorder(N|E|W?0:10, N|S|W?0:10, N?0:10, N|E|S?10:0);
活雷疯 2024-08-06 19:23:51

您没有指定哪种编程语言。

如果是java,枚举可以提供良好的可读语法、类型安全性,并利用EnumSet实现的高效位处理能力。

或者,您也可以提供 varargs 方法签名,但是您无法确定您的方法将使用 printBorder(N,N) 调用,这实际上没有意义。 使用 EnumSet 接口你就有了这样的保证。

  public class PrintBorder {

    //this is your method without the if's
    public static void printBorder(EnumSet<Sides> sides) {
        for (Sides side : sides) {
            side.print(square);
        }
    }

    //use it like this
    public static void main(String[] args) {
        printBorder(EnumSet.of(N, E)); //static import here
    }

    //declare an enum for the sides.
    public enum Sides {
        N(0, 0, 0, 10),
        E(0, 10, 10, 10),
        S(10, 0, 10, 10),
        W(0, 0, 10, 0);

        private final int x1;
        private final int y1;
        private final int x2;
        private final int y2;

        Sides(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

        //this method could as well be in the Square class, would be cleaner
        public void print(Square s) {
            s.printBorder(x1, y1, x2, y2);
        }

    }

    //boilerplate here
    private static final Square square = new Square();

    private static class Square {
        public void printBorder(int x1, int y1, int x2, int y2) {
            //do something..
        }
    }
}

you did not specify which programming language.

if it were java, enums can provide good readable syntax,type safety, as well as take advantage of the efficient bit-fiddling capabilities of the EnumSet implementation.

alternatively you could also provide a varargs method signature, but then you cannot be sure that your method will be called with printBorder(N,N), which does not really make sense. using the EnumSet interface you have this guarantee.

  public class PrintBorder {

    //this is your method without the if's
    public static void printBorder(EnumSet<Sides> sides) {
        for (Sides side : sides) {
            side.print(square);
        }
    }

    //use it like this
    public static void main(String[] args) {
        printBorder(EnumSet.of(N, E)); //static import here
    }

    //declare an enum for the sides.
    public enum Sides {
        N(0, 0, 0, 10),
        E(0, 10, 10, 10),
        S(10, 0, 10, 10),
        W(0, 0, 10, 0);

        private final int x1;
        private final int y1;
        private final int x2;
        private final int y2;

        Sides(int x1, int y1, int x2, int y2) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
        }

        //this method could as well be in the Square class, would be cleaner
        public void print(Square s) {
            s.printBorder(x1, y1, x2, y2);
        }

    }

    //boilerplate here
    private static final Square square = new Square();

    private static class Square {
        public void printBorder(int x1, int y1, int x2, int y2) {
            //do something..
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文