Skip to main content
added 8 characters in body
Source Link
Doc Brown
  • 217.6k
  • 34
  • 402
  • 612

IMHO the first one is in most cases preferable. First, in C# you can rewrite the initialization much shorter as

    Action[] array = new[]{one, two, threthree, four, five, six, seven,eight,nine};

But the real advantage comes when you have to change your actions later, for example, by introducing an additional argument. You then have to change only this line

    array[buttons.Count - 1](myArgument);

The switch variant instead needs changes in 9 places instead:

switch(buttons.count){
case 1: one(myArgument); break;
case 2: two(myArgument); break;
...

That is because the switch variant does not follow the "Don't Repeat Yourself" (DRY) principle. Imagine what happens when you have not just 9 actions, but 50, or you have to introduce more arguments, or to process a return code from the action / function.

And don't worry too much about performance - in most real-world cases the performance difference is neglectablenegligible. And whenWhen you suspect it is not, you will have to try and measure if a replacement by a switch statement does really bring the necessary performance gain.

IMHO the first one is in most cases preferable. First, in C# you can rewrite the initialization much shorter as

    Action[] array = new[]{one, two, thre, four, five, six, seven,eight,nine};

But the real advantage comes when you have to change your actions later, for example, by introducing an additional argument. You then have to change only this line

    array[buttons.Count - 1](myArgument);

The switch variant instead needs changes in 9 places instead:

switch(buttons.count){
case 1: one(myArgument); break;
case 2: two(myArgument); break;
...

That is because the switch variant does not follow the "Don't Repeat Yourself" (DRY) principle. Imagine what happens when you have not just 9 actions, but 50, or you have to introduce more arguments, or to process a return code from the action / function.

And don't worry too much about performance - in most real-world cases the performance difference is neglectable. And when it is not, you will have to try and measure if a replacement by a switch statement does really bring the necessary performance gain.

IMHO the first one is in most cases preferable. First, in C# you can rewrite the initialization much shorter as

    Action[] array = new[]{one, two, three, four, five, six, seven,eight,nine};

But the real advantage comes when you have to change your actions later, for example, by introducing an additional argument. You then have to change only this line

    array[buttons.Count - 1](myArgument);

The switch variant instead needs changes in 9 places instead:

switch(buttons.count){
case 1: one(myArgument); break;
case 2: two(myArgument); break;
...

That is because the switch variant does not follow the "Don't Repeat Yourself" (DRY) principle. Imagine what happens when you have not just 9 actions, but 50, or you have to introduce more arguments, or to process a return code from the action / function.

And don't worry too much about performance - in most real-world cases the performance difference is negligible. When you suspect it is not, you will have to try and measure if a replacement by a switch statement does really bring the necessary performance gain.

added 97 characters in body
Source Link
Doc Brown
  • 217.6k
  • 34
  • 402
  • 612

IMHO the first one is in most cases preferable. First, in C# you can rewrite the initialization much shorter as

    Action[] array = new[]{one, two, thre, four, five, six, seven,eight,nine};

But the real advantage comes when you have to change your actions later, for example, by introducing an additional argument. You then have to change only this line

    array[buttons.Count - 1](myArgument);

The switch variant instead needs changes in 9 places instead:

switch(buttons.count){
case 1: one(myArgument); break;
case 2: two(myArgument); break;
...

That is because the switch variant does not follow the "Don't Repeat Yourself" (DRY) principle. Imagine what happens when you have not just 9 actions, but 50, or you have to introduce more arguments, or to process a return code from the action / function.

And don't worry too much about performance - in most real-world cases the performance difference is neglectable. And when it is not, you will have to try and measure if a replacement by a switch statement does really bring the necessary performance gain.

IMHO the first one is in most cases preferable. First, in C# you can rewrite the initialization much shorter as

    Action[] array = new[]{one, two, thre, four, five, six, seven,eight,nine};

But the real advantage comes when you have to change your actions later, for example, by introducing an additional argument. You then have to change only this line

    array[buttons.Count - 1](myArgument);

The switch variant instead needs changes in 9 places instead:

switch(buttons.count){
case 1: one(myArgument); break;
case 2: two(myArgument); break;
...

That is because the switch variant does not follow the "Don't Repeat Yourself" (DRY) principle. Imagine what happens when you have not just 9 actions, but 50.

And don't worry too much about performance - in most real-world cases the performance difference is neglectable. And when it is not, you will have to try and measure if a replacement by a switch statement does really bring the necessary performance gain.

IMHO the first one is in most cases preferable. First, in C# you can rewrite the initialization much shorter as

    Action[] array = new[]{one, two, thre, four, five, six, seven,eight,nine};

But the real advantage comes when you have to change your actions later, for example, by introducing an additional argument. You then have to change only this line

    array[buttons.Count - 1](myArgument);

The switch variant instead needs changes in 9 places instead:

switch(buttons.count){
case 1: one(myArgument); break;
case 2: two(myArgument); break;
...

That is because the switch variant does not follow the "Don't Repeat Yourself" (DRY) principle. Imagine what happens when you have not just 9 actions, but 50, or you have to introduce more arguments, or to process a return code from the action / function.

And don't worry too much about performance - in most real-world cases the performance difference is neglectable. And when it is not, you will have to try and measure if a replacement by a switch statement does really bring the necessary performance gain.

added 116 characters in body
Source Link
Doc Brown
  • 217.6k
  • 34
  • 402
  • 612

IMHO the first one is in most cases preferable. First, in C# you can rewrite the initialization much shorter as

    Action[] array = new[]{one, two, thre, four, five, six, seven,eight,nine};

But the real advantage comes when you have to change your actions later, for example, by introducing an additional argument. You then have to change only this line

    array[buttons.Count - 1](myArgument);

The switch variant instead needs changes in 9 places instead:

switch(buttons.count){
case 1: one(myArgument); break;
case 2: two(myArgument); break;
...

That is because the switch variant does not follow the "Don't Repeat Yourself" (DRY) principle. Imagine what happens when you have not just 9 actions, but 50.

And don't worry too much about performance - in most real-world cases the performance difference is neglectable. And when it is not, you will have to try and measure if a replacement by a switch statement does really bring the necessary performance gain.

IMHO the first one is in most cases preferable. First, in C# you can rewrite the initialization much shorter as

    Action[] array = new[]{one, two, thre, four, five, six, seven,eight,nine};

But the real advantage comes when you have to change your actions later, for example, by introducing an additional argument. You then have to change only this line

    array[buttons.Count - 1](myArgument);

The switch variant instead needs changes in 9 places instead:

switch(buttons.count){
case 1: one(myArgument); break;
case 2: two(myArgument); break;
...

That is because the switch variant does not follow the "Don't Repeat Yourself" (DRY) principle.

IMHO the first one is in most cases preferable. First, in C# you can rewrite the initialization much shorter as

    Action[] array = new[]{one, two, thre, four, five, six, seven,eight,nine};

But the real advantage comes when you have to change your actions later, for example, by introducing an additional argument. You then have to change only this line

    array[buttons.Count - 1](myArgument);

The switch variant instead needs changes in 9 places instead:

switch(buttons.count){
case 1: one(myArgument); break;
case 2: two(myArgument); break;
...

That is because the switch variant does not follow the "Don't Repeat Yourself" (DRY) principle. Imagine what happens when you have not just 9 actions, but 50.

And don't worry too much about performance - in most real-world cases the performance difference is neglectable. And when it is not, you will have to try and measure if a replacement by a switch statement does really bring the necessary performance gain.

Source Link
Doc Brown
  • 217.6k
  • 34
  • 402
  • 612
Loading