Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to send data out one of 5 midi ports where if the m_iCurrentStrip+1 is between 1 and 8 the data is to go out midi port 0, if the m_iCurrentStrip+1 is between 9 and 16 the data is to go out to port 1 and so on for up to 5 ports.

Unable to visualize this in a elegant way I simply used a series of if then else's
inside a switch statement.

It works, but bleh. I hate looking at it.

How would you do it?


C++
int port; // usb or midi port (SurfaceAddress[port]
switch(NUM_UNITS)
{
	case 0: // there are no units
	    port = 0;
	    break;
	case 1:    // there is 1 unit
	    port = 0;
	    break;

	case 2:// there are two units
										                   if(m_iCurrentStrip+1  > 8)
		{
	            port = 1;
                }else{
	            port = 0;
		}
                 break;

	case 3:// there are three units
										                    if(m_iCurrentStrip+1 > 16)
		{
	             port = 2;
                 }else{
											          if(m_iCurrentStrip+1 > 8)
	         {
		      port = 1;
	         }else{
												 if(m_iCurrentStrip+1 < 8)
		 {
			port = 0;
		 }
	      }
										}
	case 4:// there are four units
										                   if(m_iCurrentStrip+1 > 24)
		{
	               port = 3;
                }else{
										                   if(m_iCurrentStrip+1 > 16)
		{
	               port = 2;
	        }else{
											         if(m_iCurrentStrip+1 > 8)
	         {
	               port = 1;
	         }else{
												 if(m_iCurrentStrip+1 < 8)
		{
			port = 0;
		}
	      }
            }
          }
	case 5:// there are five units
										                    if(m_iCurrentStrip+1 > 32)
		{
	                port = 4;
                 }else{
										                    if(m_iCurrentStrip+1 > 24)
		 {
	                port = 3;
                   }else{
										                     if(m_iCurrentStrip+1 > 16)
		  {
	                port = 2;
                   }else{
											           if(m_iCurrentStrip+1 > 8)
	           {
	                port = 1;
											            }else{
												    if(m_iCurrentStrip+1 < 8)
		    {
			port = 0;
	            }
											           }
		  }
		 }
		}
												break;
}
/xml>
Posted

This fails if m_iCurrentStrip + 1 equals 8. Apart from that, case 0 and 1 can fall through. If this was C++ I'd use a map, or a Dictionary in C#, to have one line of code that looks up the right value. You could also switch on all the values ( as in case 16: case 15: and so on ), but in C, this could be as good as it gets.


Actually, it looks to me like the only difference in the rules between cases, is how many there are. I'd drop this in to one if statement, and add checks such as:

C#
if(m_iCurrentStrip+1 > 32)
       {
             if (NUM_UNITS > 4 )  
                   port = 4;
                }else{
                                                           if(m_iCurrentStrip+1 > 24mp;& NUM_UNITS > 4)
       {
             if (NUM_UNITS > 3 )  
                  port = 3;
                 }else{
                                                           if(m_iCurrentStrip+1 &gt; 16)
        {
             if (NUM_UNITS > 2 )  
                  port = 2;
                 }else{
                                                     if(m_iCurrentStrip+1 &gt; 8)
             {
             if (NUM_UNITS > 1 )  
                  port = 1;
                                                      }else{
                                                  
          port = 0;
              }
 
Share this answer
 
Probably you can do this using a single line:
C++
port = NUM_UNITS == 0 ? 0 : min((m_iCurrentStrip+1) / 8, NUM_UNITS - 1);
 
Share this answer
 
v2
Comments
Ron Anders 6-Jan-13 19:29pm    
Andrew Cherednik, that is lovely.

Thanks
chaau 6-Jan-13 19:34pm    
Please run unit tests and compare the results with your code for all possible values. I am not sure if it works as expected for the border values (when m_iCurrentStrip = 8, 16, 32, etc). If it's wrong, you may need to use min(m_iCurrentStrip / 8, NUM_UNITS - 1).
Also, please note, that the code does not validate the values of NUM_UNITS > 5 (same as your code). You may wish to consider including this check.
Ron Anders 6-Jan-13 20:19pm    
I certainly shall Monday!

Thanks. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900