2010年10月28日星期四

Guys, is this called C# code?

This is the code that written by the senior guy who wanna taught me something about C# and design!

 public int RCEncrypt(string srcString, ref char[] dstBufer, int nLen, int nIndex)
        {

            char []srcArray = new char[srcString.Length];
            int i = 0, j = 0;
            for (i = 0; i < srcArray.Length; i++)
            {
                 srcArray[i] = srcString[i];
            }

            i = 0;
            int key_len = szKey.Length;
            char c0, h1, l1, k1, c1, c2, h2, l2;
            j = nIndex;
            for (i = 0; i < nLen; i++, j++)
            {
                if (j >= key_len)
                    j = 0;
                k1 = (char)szKey[j];
                c0 = (char)srcArray[i];
                h1 = (char)(c0 / 0x10);
                l1 = (char)(c0 % 0x10);
                c1 = (char)(k1 ^ l1);
                h2 = (char)(c1 / 0x10);
                l2 = (char)(c1 % 0x10);
                c2 = (char)(h1 * 0x10 + l2);
                dstBufer[i] = c2;
            }
            return j;
        }

Seriously,  I've totally no idea about what language he is using! 
C#, in syntax, it might be, but I can promise no REAL C# GUY writes code in this way! I promise it code can be compiled successfully by C++ compiler with some slightly changes.
The guy who wrote this code only knows C++, and nothing about C#.

Tons of type conversion, I've no idea why he just try to convert a char to char?
Tons of memory copy, I've no idea why he just copy the string content to a char array, since C# string can be used as a char array in syntax. And if he indeed wanna copy the C# string is immutable object, so there is absolutely no necessary to copy the string to prevent the data changes during processing data in multi-thread context. And if he absolutely need to convert the string to a char array, he just need to call ToCharArray() method of string, instead of writing such a bunch of code to do that in such a ugly way!

The horrible news is that there are bunches of such code that he just gave. And it is said that I have to include these code into my project!
No objective concept, just procedures; tons of meaningless type conversion, tons of unnecessary memory copy with for loop, tons of meaningless variable names, totaly C++ style variable name format and function name format (yes, yes, I said FUNCTION not METHOD! since there is only one file with one class contains tens of functions there!). And most ridiculous, he just put his unit test code ( According to the function name, it is called unit test, although I can hardly recognized it as a unit test ) into his functional code, in the SAME class and the SAME file!

So this is the code written by a senior software engineer, who intend to taught me something about C# and OBJECT ORIENTED DESIGN!

TimNew
------------
Release your passion
To Realize your potential

Posted via email from 米良的实验室

没有评论:

发表评论