Click here to Skip to main content
16,017,231 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, how can I optimize my code? Reduce the number of lines(Statements)?

What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
      {
          string[] seq1 = { "A", "C", "G", "T", "A", "C", "A", "G","T","G" };
          string[] seq2 = { "T", "A", "C", "A", "C", "G","A","A","G","T" };
          string[] res1 = new string[seq1.Length];
          string[] res2 = new string[seq1.Length];
          for (int i = 0; i <= 4; i++)
          {

              res1[i] = seq1[i];
              res2[i] = seq2[i];
              textBox1.Text +=" "+ res1[i];
              textBox2.Text +=" "+ res2[i];
          }
          for(int i = 5; i < seq1.Length; i++)
          {
              if (seq1[i] == "T" )
              {
                  res1[i] = "A";

              }
              else
                  if (seq1[i] == "A" )
              {
                  res1[i] = "T";

              }
              else
                 if (seq1[i] == "C" )
              {
                  res1[i] = "G";

              }
              else
                 if (seq1[i] == "G" )
              {
                  res1[i] = "C";

              }
              textBox1.Text += res1[i]+"   ";

          }
          for (int i = 5; i < seq2.Length; i++)
          {
              if (seq2[i] == "T")
              {
                  res2[i] = "A";

              }
              else
                  if (seq2[i] == "A")
              {
                  res2[i] = "T";

              }
              else
                 if (seq2[i] == "C")
              {
                  res2[i] = "G";

              }
              else
                 if (seq2[i] == "G")
              {
                  res2[i] = "C";

              }

              textBox2.Text += res2[i] + "   ";
          }
      }
Posted
Updated 18-Apr-17 3:48am
v2
Comments
PIEBALDconsult 18-Apr-17 9:47am    
Ummm... you _do_ realize that in .net, strings are immutable, yes?
Look into StringBuilder.
Richard MacCutchan 18-Apr-17 11:28am    
You can get rid of much of the code by creating a simple translate table for your characters, rather than testing for each character.
Lilyanaa 18-Apr-17 11:47am    
How can do this ?
Richard MacCutchan 18-Apr-17 12:35pm    
Just create character arrays with the destination characters in the correct place. You can then use the source character as an index, something like:

// A translates to G, B to B, C to R etc
char[] destTable = { 'G', 'B', 'R' };

char source = // wherever this character comes from
char dest = destTable[source - 'A'];

It seems that your seq strings contain only single characters. Then don't use string arrays but character arrays. This will not reduce the number of lines but reduces the program size and increases execution speed.

Replace also the if else if conditions by switch statements.
 
Share this answer
 
Comments
Lilyanaa 18-Apr-17 11:10am    
Thanks, yes that is right, I will use char array
Lilyanaa 18-Apr-17 11:16am    
How can I use switch statements with 2 array ?
Jochen Arndt 18-Apr-17 11:26am    
You have to use it for each array. When both sequences have identical length you can do it within one loop. When both use the same logic you can put the switch statement into a sub function:
char getMatch(char seq)
{
 switch (seq)
 {
  case 'T' : return 'A';
  case 'A' : return 'T';
  // ...
 }
 // Error condition (e.g. throw an exception)
 return ' ';
}

Call it this way:
res1[i] = getMatch(seq1[i]);
res2[i] = getMatch(seq2[i]);
So what's the Problem?
I see:
* using direct access to controls, and updating the Text properties in a Loop - inefficient, not reusable.
* No Name for the method or the click-handler to give a hint what this all is about (I'd guess some DNA calculations?)
* No comments, or meaningful variable names.
* hardcoded constants - why 4 or 5?
* no reason for the method to be non-static other than directaAccess of your controls.
* Inefficient string concatinations - use StringBuilder..

So a lot of things to worry about for this Code, number of lines? No that's the smalest problem you have there...
 
Share this answer
 
Comments
Lilyanaa 18-Apr-17 11:08am    
Thank, this is two sequence of DNA, I'm trying to replace the char in sequences DNA (:

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