If statement 如何改进此代码?(如果有太多)
我想打印正方形的边框。。。 它可能只打印正方形的一面或多面,所以我写了这个方法If statement 如何改进此代码?(如果有太多),if-statement,optimization,pseudocode,If Statement,Optimization,Pseudocode,我想打印正方形的边框。。。 它可能只打印正方形的一面或多面,所以我写了这个方法 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.printBo
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);
}
}
它可以很好地工作,但我认为它不是那么优雅,它太多了,如果,所有的声明或多或少是相同的。我想一定有办法简化这些代码,有什么建议吗?一种简化的方法。。。即使您不需要,也要拨打电话,但要使实施有条件:
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);
}
然后在方块中
(或任何形式):
类似的替代方法是将条件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);
}
简化它的一种方法。。。即使您不需要,也要拨打电话,但要使实施有条件:
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);
}
然后在方块中
(或任何形式):
类似的替代方法是将条件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);
}
我才不在乎那些假设呢。我只是想让它更具可读性:
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);
}
我才不在乎那些假设呢。我只是想让它更具可读性:
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);
}
就我个人而言,我非常喜欢二进制比较
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);
或
只是我的意见。:) 就个人而言,我非常喜欢二进制比较
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);
或
只是我的意见。:) 首先你做得很好,这正是它所表达的,不要担心你正在使用的空间,这里的大多数解决方案只是把水弄脏了
如果您真的想“做”一些事情,请查看是否无法将Border参数移动到正方形中。您可以将边框填充(在您的示例中为10)移动到正方形中,也可以移动显示边框的状态,然后只调用square.printBorders()。这在很大程度上取决于您使用它的环境。首先,您做得很好,这正是它所表达的,不要担心您正在使用的空间,这里的大多数解决方案只是搅浑了水
如果您真的想“做”一些事情,请查看是否无法将Border参数移动到正方形中。您可以将边框填充(在您的示例中为10)移动到正方形中,也可以移动显示边框的状态,然后只调用square.printBorders()。这在很大程度上取决于您使用它的上下文。您没有指定哪种编程语言 如果是java,枚举可以提供良好的可读语法、类型安全性,以及利用枚举集实现的高效位篡改功能 或者,您也可以提供一个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..
}
}
}
公共类PrintBorder{
//这是你没有if的方法
公共静态void打印边框(枚举集边){
用于(侧面:侧面){
侧面印刷(方形);
}
}
//像这样使用它
公共静态void main(字符串[]args){
printBorder(EnumSet.of(N,E));//此处静态导入
}
//为边声明枚举。
公共枚举方{
N(0,0,0,10),
E(0,10,10,10),
S(10,0,10,10),
W(0,0,10,0);
私人最终int x1;
私人最终int y1;
私人最终int x2;
私人终审法院y2;
侧面(内部x1、内部y1、内部x2、内部y2){
这是1.x1=x1;
这是1.y1=y1;
这是0.x2=x2;
这1.y2=y2;
}
//这种方法也可以在Square类中使用,这样会更干净
公众作废印刷品(方形s){
s、 打印边框(x1,y1,x2,y2);
}
}
//这里的样板
专用静态最终方形=新方形();
私人静态类广场{
公共空白打印边框(int x1、int y1、int x2、int y2){
//做点什么。。
}
}
}
您没有指定哪种编程语言
如果是java,枚举可以提供良好的可读语法、类型安全性,以及利用枚举集实现的高效位篡改功能
或者,您也可以提供一个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..
}
}
}
公共类PrintBorder{
//这是你没有if的方法
公共静态void打印边框(枚举集边){
用于(侧面:侧面){
侧面印刷(方形);
}
}
//像这样使用它
公共静态void main(字符串[]args){
printBorder(EnumSet.of(N,E));//此处静态导入
}
//为边声明枚举。
公共枚举方{
N(0,0,0,10),
E(0,10,10,10),
S(10,0,10,10),
W(0,0,10,0);
私人最终int x1;
私人最终int y1;
私人最终int x2;
私人终审法院y2;
侧面(内部x1、内部y1、内部x2、内部y2){
这是1.x1=x1;
这是1.y1=y1;
这是0.x2=x2;
这1.y2=y2;
}
//这种方法也可以在Square类中使用,这样会更干净
公众作废印刷品(方形s){
s、 打印边框(x1,y1,x2,y2);
}
}
//这里的样板
P