Expressing a 2x logical grid in code efficiently

In the event handler, I respond to a change in value. I have access to the old value and the new value and I want to do certain things depending on what the change is.

Each different result will perform some combination of actions / functions X, Y or Z. Z takes a parameter from -1 to 1. The order of these operations does not matter.

Look at the following logical grid. The old value is the leftmost column of labels, and the new value is the top row of labels:

          New:
          0          !=0
          --------   -------
Old:  0 | nothing    Y, Z(1)
    !=0 | X, Z(-1)   X, Y    -- Z(0) is okay but not required for this quadrant

What would be a good way to introduce this?

I work in C #, but I will accept answers in any language, since this is not a language issue - I can translate something.

Example:

if (oldvalue == 0 && newvalue == 0) return;
if (oldvalue != 0) X();
if (newvalue != 0) Y();
Z(oldvalue != 0 ? -1 : 0 + newvalue != 0 ? 1 : 0);

I suppose this looks good, but there are other ways to do it.

int which = (oldvalue == 0 ? 0 : 1) + (newvalue == 0 ? 0 : 2)

switch (which) {
   case 1:
      X(); Z(-1);
      break;
   case 2:
      Y(); Z(1);
      break;
   case 3:
      X(); Y();
      break;
}

, , . , oldvalue newvalue , newvalue, 0.

. , , . , , .

, , , 2x2, . , "", , , X , oldvalue!= 0, , , , , , . , , , .

" ":

Conditions:
oldvalue == 0 ? 0 : 1
newvalue == 0 ? 0 : 2

Actions:
X = {false, true, false, true}
Y = {false, false, true, true}
Z(-1) = true where condition = 1
Z(1) = true where condition = 2

? .

+5
3

. :

  • .
  • .
  • .
  • . ... .

, , . - . ...

, . , , , , , . - spec ( ).

- , , ( !) , , . (: ) , , .

- , - , . , , . , , .

- - , . , , .

, , , , .. , . :

void YourMethod( int oldValue, int newValue )
{
    bool oldValueNonZero = oldValue != 0;
    bool newValueNonZero = newValue != 0;

    if( oldValueNonZero ) { X(); }
    if( newValueNonZero ) { Y(); }
    if( oldValueNonZero && newValueNonZero ) { Z(); }
}

, . .

, , , / , . , , (. ).

, oldValueNonZero newValueNonZero, , . , .

, , if() { } - , - , , . ifs - .

, . , . , (1).

, ? .

. , , "".

? ... , .

, - . , , , , .

, " ", , . :

  • , , MxN.
  • - , : MxNxOxP...xZ.

( ), . , - , , .

- ... . , , /else . , , - .

1 > - - . , , / () , .

+10

. , - , (lambdas).

:

private void Y() { }
private void X() {}
private void Z(int n) {}

private void example(int oldvalue, int newvalue)
{
    var array = new Action[2, 2];
    array[0, 0] = () => { };
    array[0, 1] = () => { Y(); Z(1); };
    array[1, 0] = () => { X(); Z(-1); };
    array[1, 1] = () => { X(); Y(); };

    // invoke
    array[oldvalue == 0 ? 0 : 1, newvalue == 0 ? 0 : 1]();
}

, , , .

, , , .

  • oldvalue == 0
  • newvalue == 0

(), , if.

+2

. , , . , , , .

, . , , . , .

          New == 0 || New == Old    New != 0 && New != Old
          ----------------------   ------------------------
Old == 0 | nothing                  Y, Z(1)
Old != 0 | X, Z(-1)                 X, Y    -- Z(0) is okay but not required for this quadrant

, dthorpe newValue == 0 newValue == 0 || newValue == oldValue:

private void example(int oldvalue, int newvalue)
{
    var array = new Action[2, 2];
    array[0, 0] = () => { };
    array[0, 1] = () => { Y(); Z(1); };
    array[1, 0] = () => { X(); Z(-1); };
    array[1, 1] = () => { X(); Y(); };

    array[oldvalue == 0 ? 0 : 1, newValue == 0 || newValue == oldValue ? 0 : 1]();
}

LBushkin newValue != 0 newValue != 0 && newValue != oldValue . :

void YourMethod( int oldValue, int newValue )
{
    bool oldValueNonZero = oldValue != 0;
    bool newValueDifferentAndNonZero = newValue != 0 && newValue != oldValue;
    int zCondition = 0;

    if( oldValueNonZero ) { X(); zCondition--;}
    if( newValueDifferentAndNonZero ) { Y(); zCondition++;}
    if( zCondition != 0 ) { Z(zCondition); }
}

!

+1
source

All Articles