6ea4797d26c05b3d44e49b989f1a393c

Hi, I am new on c#...
there is a way to simplify this code....
thanks

private string CodiceControllo(string codice_fiscale)
{
	int somma = 0;
	int resto = 0;
		
	for (int i = 0; i < 15; i++){
		if (i == 0 || i == 2 || i == 4 || i == 6 || i == 8 || i == 10 || 
        	    i == 12 || i == 14) {
					
				switch (codice_fiscale[i]) {
					case 'A': 
						somma += 1;
						break;
					case 'B': 
						somma += 0;
						break;
					case 'C': 
						somma += 5;
						break;
					case 'D': 
						somma += 7;
						break;
					case 'E': 
						somma += 9;
						break;
					case 'F': 
						somma += 13;
						break;
					case 'G': 
						somma += 15;
						break;
					case 'H': 
						somma += 17;
						break;
					case 'I': 
							somma += 19;
						break;
					case 'J': 
						somma += 21;
						break;
					case 'K': 
						somma += 2;
						break;
					case 'L': 
						somma += 4;
						break;
					case 'M': 
						somma += 18;
						break;
					case 'N': 
						somma += 20;
						break;
					case 'O': 
						somma += 11;
						break;
					case 'P': 
						somma += 3;
						break;
					case 'Q': 
						somma += 6;
						break;
					case 'R': 
						somma += 8;
						break;
					case 'S': 
						somma += 12;
						break;
					case 'T': 
						somma += 14;
						break;
					case 'U': 
						somma += 16;
						break;
					case 'V': 
						somma += 10;
						break;
					case 'W': 
						somma += 22;
						break;
					case 'X': 
						somma += 25;
						break;
					case 'Y': 
						somma += 24;
						break;
					case 'Z': 
						somma += 23;
						break;
					case '0': 
						somma += 1;
						break;
					case '1': 
						somma += 0;
						break;
					case '2': 
						somma += 5;
						break;
					case '3': 
						somma += 7;
						break;
					case '4': 
						somma += 9;
						break;
					case '5': 
						somma += 13;
						break;
					case '6': 
						somma += 15;
						break;
					case '7': 
						somma += 17;
						break;
					case '8': 
						somma += 19;
						break;
					case '9': 
						somma += 21;
						break;
				}
			} else {
	
				switch (codice_fiscale[i]) {
					case 'A': 
						somma += 0;
						break;
					case 'B': 
						somma += 1;
						break;
					case 'C': 
						somma += 2;
						break;
					case 'D': 
						somma += 3;
						break;
					case 'E': 
						somma += 4;
						break;
					case 'F': 
						somma += 5;
						break;
					case 'G': 
						somma += 6;
						break;
					case 'H': 
						somma += 7;
						break;
					case 'I': 
						somma += 8;
						break;
					case 'J': 
						somma += 9;
						break;
					case 'K': 
						somma += 10;
						break;
					case 'L': 
						somma += 11;
						break;
					case 'M': 
						somma += 12;
						break;
					case 'N': 
						somma += 13;
						break;
					case 'O': 
						somma += 14;
						break;
					case 'P': 
						somma += 15;
						break;
					case 'Q': 
						somma += 16;
						break;
					case 'R': 
						somma += 17;
						break;
					case 'S': 
						somma += 18;
						break;
					case 'T': 
						somma += 19;
						break;
					case 'U': 
						somma += 20;
						break;
					case 'V': 
						somma += 21;
						break;
					case 'W': 
						somma += 22;
						break;
					case 'X': 
						somma += 23;
						break;
					case 'Y': 
						somma += 24;
						break;
					case 'Z': 
						somma += 25;
						break;
					case '0': 
						somma += 0;
						break;
					case '1': 
						somma += 1;
						break;
					case '2': 
						somma += 2;
						break;
					case '3': 
						somma += 3;
						break;
					case '4': 
						somma += 4;
						break;
					case '5': 
						somma += 5;
						break;
					case '6': 
						somma += 6;
						break;
					case '7': 
						somma += 7;
						break;
					case '8': 
						somma += 8;
						break;
					case '9': 
						somma += 9;
						break;
				}
			}
		}
			
		Math.DivRem(somma, 26, out resto); 
			return char.ConvertFromUtf32(resto+65);
	}

Refactorings

No refactoring yet !

880cbab435f00197613c9cc2065b4f5a

danielharan

December 19, 2007, December 19, 2007 18:19, permalink

3 ratings. Login to rate!

That code is an abomination. Two things for you to look at: modulo operator and arrays. It should only be about 12 lines once you are done.

69f4fbbfa3d8fcef2e6d11f04db64c5b

Jonathan Starr

December 20, 2007, December 20, 2007 00:11, permalink

4 ratings. Login to rate!

It looks like you are enciphering some of the characters but not all of them.

I separated out the logic to decide which cipher to use in method isCipher1 base on character position.

I also tried to give variable names a more meaningful name - really "i" is a bad variable name....

I converted the input string to an array of characters as the string codice_fiscale[i] is meaningless - this wiull return nothing - but the character array chars_codice_fiscale[characterPosition] will return one chacter for each characterPosition. Also I like to use the IsNumber function wrapped in the Char class for testing whether a character is a number...

Finally, instead of long switch statements I used an int array, and position in the ascii table to assign values.

The end product is more maintainable, and I included some error checking as well.. (I think you still need to check that the string is an empty string or null to make this better, IMHO.

Hope this helped you,

Jonathan Starr

private bool isCipher1( int characterPosition)
     {
         int[] cipher1Chars = new int[] { 0, 2, 4, 6, 8, 10, 12, 14 };
         foreach ( int cipher1Char in cipher1Chars)
         {
             if (characterPosition == cipher1Char) return true;
         }

         return false;
     }

    private string CodiceControllo(string codice_fiscale)
    {
	    int somma = 0;
	    int resto = 0;

        int [] cipher1 = new int [] {1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,
                                10,22,25,24,23,0,5,7,9,13,15,17,19,21};

        char [] chars_codice_fiscale = codice_fiscale.ToUpper().ToCharArray();

	    for (int characterPosition = 0; characterPosition < 15; characterPosition++)
        {
       
            if (isCipher1(characterPosition))
            {
                if (Char.IsNumber(chars_codice_fiscale[characterPosition]))
                {
                    somma += cipher1 [ Int32.Parse(chars_codice_fiscale[characterPosition]) + 26] ;
                    continue;
                }
                
                if (Char.IsLetter(chars_codice_fiscale[characterPosition]))
                {
                    somma += cipher1 [Convert.ToInt32(chars_codice_fiscale[characterPosition]) - 65];
                    continue;
                }

                throw new ArgumentException("Invalid character in argument " + codice_fiscale);
            }
            else
            {
                if (Char.IsNumber(chars_codice_fiscale[characterPosition]))
                {
                    somma += Int32.Parse(chars_codice_fiscale[characterPosition].ToString()) ;
                    continue;
                }
                
                if (Char.IsLetter(chars_codice_fiscale[characterPosition]))
                {
                    somma += Convert.ToInt32(chars_codice_fiscale[characterPosition]) - 65;
                    continue;
                }

                throw new ArgumentException("Invalid character in argument " + codice_fiscale);
        
            }
	
		    Math.DivRem(somma, 26, out resto); 
			return char.ConvertFromUtf32(resto+65);
	}
326e4b379fafa37c9483d6f8e8eb9826

nibbles&bits

December 20, 2007, December 20, 2007 21:13, permalink

2 ratings. Login to rate!

for the sake of readability, i'm more of a lookup man myself.
it may not be as performant, but it's sure easier to read, and the ultimate addition line is much more understandable/simple.

// just for the switch

Dictionary<char,int> dict = new Dictionary<char,int>(26);
dict.Add('A',1);
dict.Add('B',0);
dict.Add('C',5);
dict.Add('D',7);
dict.Add('E',9);
dict.Add('F',13);
dict.Add('G',15);
dict.Add('H',17);
dict.Add('I',19);
dict.Add('J',21);
dict.Add('K',2);
dict.Add('L',4);
dict.Add('M',18);
dict.Add('N',20);
dict.Add('O',11);
dict.Add('P',3);
dict.Add('Q',6);
dict.Add('R',8);
dict.Add('S',12);
dict.Add('T',14);
dict.Add('U',16);
dict.Add('V',10);
dict.Add('W',22);
dict.Add('X',25);
dict.Add('Y',24);
dict.Add('Z',23);
dict.Add('0',1);
dict.Add('1',0);
dict.Add('2',5);
dict.Add('3',7);
dict.Add('4',9);
dict.Add('5',13);
dict.Add('6',15);
dict.Add('7',17);
dict.Add('8',19);
dict.Add('9',21);
// then instead of the big switch, use this:
somma += dict[codice_fiscale[i]];
9c52ad00ba2f2602661c49f896733229

orip

January 18, 2008, January 18, 2008 18:51, permalink

No rating. Login to rate!

nibbles&bits: I also think a dictionary captures the key->value mapping better than a switch or an array, but the verbose syntax for initializing it takes some readability away.

C# 3.0 syntax is nice, though:

Dictionary<char,int> dict = new Dictionary<char,int> {
  {'A', 1},
  {'B', 0},
  {'C', 5},
  //...
  {'8', 19},
  {'9', 21}
};
9c52ad00ba2f2602661c49f896733229

orip

January 18, 2008, January 18, 2008 18:55, permalink

2 ratings. Login to rate!

I liked Jonathan Starr's approach, here's my variation.

private int TableIndex(char c)
        {
            if (Char.IsLetter(c))
            {
                return (int)Char.ToUpper(c) - (int)'A';
            }
            else if (Char.IsNumber(c))
            {
                return 26 + int.Parse(c.ToString());
            }
            throw new ArgumentException("Invalid character");
        }

        private string CodiceControllo2(string codice_fiscale)
        {
            int[] table_even = new int[] {1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23,1,0,5,7,9,13,15,17,19,21};
            int[] table_odd  = new int[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,1,2,3,4,5,6,7,8,9};

            int somma = 0;

            for (int i = 0; i < 15; i++)
            {
                char c = codice_fiscale[i];

                if (i % 2 == 0)
                {
                    somma += table_even[TableIndex(c)];
                }
                else
                {
                    somma += table_odd[TableIndex(c)];
                }
            }

            int resto = somma % 26;
            return char.ConvertFromUtf32(resto + 65);
        }
6ea4797d26c05b3d44e49b989f1a393c

Squall77it

June 4, 2008, June 04, 2008 12:03, permalink

No rating. Login to rate!

Thanks to all....

Your refactoring





Format Copy from initial code

or Cancel