Hogyan lehetne javítani ezt a kódot? (Túl sok, ha)

szavazat
1

Azt szeretné kinyomtatni a határ a tér ... Ez lehet kinyomtatni, csak az egyik oldalon, vagy több oldalán a tér, ezért írtam ezt a módszert

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);
  }
}

Ez jól működik, de azt hiszem, nem annyira elegáns, hogy túl sok, ha minden állítás többé-kevésbé ugyanaz. Azt hiszem, ott kell lennie egy módja, hogy egyszerűsítse ezt kódok, akármi javaslatok?

A kérdést 08/08/2009 08:52
a forrás felhasználó
Más nyelveken...                            


6 válasz

szavazat
5

Ennek egyik módja egyszerűsítse ... kezdeményezhet hívásokat akkor is, ha nincs rájuk szükség, de conditionalise végrehajtására:

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);
}

Ezután Square(vagy bármi más):

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

Egy hasonló megoldás lenne megtartani a feltételes printBorderaz eredeti funkció:

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);
}
Válaszolt 08/08/2009 09:00
a forrás felhasználó

szavazat
5

Én nem törődnek az IFS. Én csak az olvashatóság érdekében:

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);
}
Válaszolt 08/08/2009 09:03
a forrás felhasználó

szavazat
3

Én személy szerint nagyon szeretem a bináris összehasonlítást.

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);
}

Egyesek azt mondják, hogy ez teszi a kód a függvény belsejében kevésbé olvasható. Azonban a gondolkodás csak egyetlen előfordulása ennek a funkciónak mivel akkor a függvény meghívásával az egész hely. Ha fut át ​​egy kódot még nem nézett egy darabig, ami több olvasható?

printBorder(true, false, true, true);

vagy

printBorder(NORTH + SOUTH + EAST);

Csak az én véleményem. :)

Válaszolt 08/08/2009 09:26
a forrás felhasználó

szavazat
3

Először csinálsz rendben van, ez pontosan mit fejez ki, ne aggódj a helyet, amit használ, a legtöbb megoldás itt csak sáros víz.

Ha szeretné, hogy „nem” valamit nézni, ha nem tudja mozgatni a határ paraméter a térre. tudna mozgatni a határ padding (10 példád a négyzet), esetleg az állam határos kell bemutatni, majd csak hívja square.printBorders (). Ez nagymértékben függ a környezetet, amelyben Ön a jelen.

Válaszolt 08/08/2009 14:14
a forrás felhasználó

szavazat
1

nem jelölt meg, amely programozási nyelv.

mintha java, enums nyújthat jó olvasható szintaktikai típusú biztonsági, valamint kihasználják a hatékony bit-hegedülő képességeit a EnumSet végrehajtását.

Vagy pedig olyan varargs módszer aláírásával, de akkor nem biztos, hogy a módszer lehet nevezni printBorder (N, N), ami nem igazán van értelme. a EnumSet felület van ez a garancia.

  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..
        }
    }
}
Válaszolt 08/08/2009 14:49
a forrás felhasználó

szavazat
3

Mit szólnál:

square.printBorder(N|E|W?0:10, N|S|W?0:10, N?0:10, N|E|S?10:0);
Válaszolt 08/08/2009 14:53
a forrás felhasználó

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more