3
\$\begingroup\$

I'm currently in the process of refactoring some code and I was wondering if I should use switch or if-else for the is code:

Switch

 for (int i = 0; i < length; i++)
 {
 CameraStream stream = null;
 var id = (Guid)CameraGridView.Rows[i].Cells[0].Value;
 var Camerasettings = Settings.GetCameraSettings(id);
 switch (i)
 {
 case 0:
 stream = new CameraStream(Camerasettings, new List<PictureBox> { Camera1a, Camera1b }, 
 new List<Label> { Camera1aLbl, Camera1bLbl });
 break;
 case 1:
 stream = new CameraStream(Camerasettings, new List<PictureBox> { Camera2a, Camera2b },
 new List<Label> { Camera2aLbl, Camera2bLbl });
 break;
 case 2:
 stream = new CameraStream(Camerasettings, new List<PictureBox> { Camera3a, Camera3b },
 new List<Label> { Camera3aLbl, Camera3bLbl });
 break;
 case 3:
 stream = new CameraStream(Camerasettings, new List<PictureBox> { Camera4a, Camera4b },
 new List<Label> { Camera4aLbl, Camera4bLbl });
 break;
 case 4:
 stream = new CameraStream(Camerasettings, Camera5, Camera5Lbl);
 break;
 case 5:
 stream = new CameraStream(Camerasettings, Camera6, Camera6Lbl);
 break;
 case 6:
 stream = new CameraStream(Camerasettings, Camera7, Camera7Lbl);
 break;
 case 7:
 stream = new CameraStream(Camerasettings, Camera8, Camera8Lbl);
 break;
 case 8:
 stream = new CameraStream(Camerasettings, Camera9, Camera9Lbl);
 break;
 }
 cameraStreams.Add(stream);
 stream.Start();
 }

if

 for (int i = 0; i < length; i++)
 {
 CameraStream stream = null;
 var id = (Guid)CameraGridView.Rows[i].Cells[0].Value;
 var Camerasettings = Settings.GetCameraSettings(id);
 if (i == 0)
 {
 var PicBoxes = new List<PictureBox> { Camera1a, Camera1b };
 var Labels = new List<Label> { Camera1aLbl, Camera1bLbl };
 stream = new CameraStream(Camerasettings, PicBoxes, Labels);
 }
 if (i == 1)
 {
 var PicBoxes = new List<PictureBox> { Camera2a, Camera2b };
 var Labels = new List<Label> { Camera2aLbl, Camera2bLbl };
 stream = new CameraStream(Camerasettings, PicBoxes, Labels);
 }
 if (i == 2)
 {
 var PicBoxes = new List<PictureBox> { Camera3a, Camera3b };
 var Labels = new List<Label> { Camera3aLbl, Camera3bLbl };
 stream = new CameraStream(Camerasettings, PicBoxes, Labels);
 }
 if (i == 3)
 {
 var PicBoxes = new List<PictureBox> { Camera4a, Camera4b };
 var Labels = new List<Label> { Camera4aLbl, Camera4bLbl };
 stream = new CameraStream(Camerasettings, PicBoxes, Labels);
 }
 if (i == 4)
 {
 stream = new CameraStream(Camerasettings, Camera5, Camera5Lbl);
 }
 if (i == 5)
 {
 stream = new CameraStream(Camerasettings, Camera6, Camera6Lbl);
 }
 if (i == 6)
 {
 stream = new CameraStream(Camerasettings, Camera7, Camera7Lbl);
 }
 if (i == 7)
 {
 stream = new CameraStream(Camerasettings, Camera8, Camera8Lbl);
 }
 if (i == 8)
 {
 stream = new CameraStream(Camerasettings, Camera9, Camera9Lbl);
 }
 cameraStreams.Add(stream);
 stream.Start();
 }
200_success
146k22 gold badges190 silver badges478 bronze badges
asked May 4, 2016 at 11:51
\$\endgroup\$
7
  • \$\begingroup\$ remove the forloop and use if statements looks better to me. \$\endgroup\$ Commented May 4, 2016 at 11:52
  • 6
    \$\begingroup\$ I'm afraid neither version looks clean or maintainable. Proper refactoring in this case should go deeper than choosing between a wall of ifs and a wall of switch-cases. \$\endgroup\$ Commented May 4, 2016 at 11:57
  • \$\begingroup\$ @BCdotWEB I'm using WinForms \$\endgroup\$ Commented May 4, 2016 at 12:26
  • \$\begingroup\$ I'd put those picture boxes and labels into arrays (or arrays of arrays), so you don't need any checks and duplicated code inside your for loop. Alternately, write a utility method to simplify the creation, adding and starting of a singleCameraStream and just call it 8 times in a row: no loop or checks needed. \$\endgroup\$ Commented May 4, 2016 at 12:35
  • 1
    \$\begingroup\$ I agree with @KonradMorawski. Take a look at all the answers of (stackoverflow.com/questions/126409/…) and loot it's knowledge. \$\endgroup\$ Commented May 4, 2016 at 16:30

2 Answers 2

1
\$\begingroup\$

Most of your names are meaningless: Camera1a, Camera1aLbl, etc. barely inform us what they are.


Is there a reason why CameraStream has two different constructors? Why not simply have a single one which takes Camerasettings, List<PictureBox> and List<Label>, that way you can concentrate on filling List<PictureBox> and List<Label> and just have a single new CameraStream() at the end.


But I fear your refactoring might need to widen its scope, because even if you apply this you still have an ugly if or switch.

answered May 4, 2016 at 12:23
\$\endgroup\$
-1
\$\begingroup\$

what about this?

 CameraStream stream = null;
 var id = (Guid)CameraGridView.Rows[i].Cells[0].Value;
 var Camerasettings = Settings.GetCameraSettings(id);
 if (length <= 0)
 {
 var PicBoxes = new List<PictureBox> { Camera1a, Camera1b };
 var Labels = new List<Label> { Camera1aLbl, Camera1bLbl };
 var stream = new CameraStream(Camerasettings, PicBoxes, Labels);
 cameraStreams.Add(stream);
 stream.Start();
 }
 if (length <= 1)
 {
 var PicBoxes = new List<PictureBox> { Camera2a, Camera2b };
 var Labels = new List<Label> { Camera2aLbl, Camera2bLbl };
 stream = new CameraStream(Camerasettings, PicBoxes, Labels);
 cameraStreams.Add(stream);
 stream.Start();
 } ...

This way you remove the loop but keep the functionality, am i correct?

you can encapsulate the double code in a function with input of camera and camera label.

answered May 4, 2016 at 11:57
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.