|
Was this a remnant of moving from the standard 'getElementById' to using a library such as jQuery that provides the '$' shortcut?
Maybe it's not terrible after all. Maybe doing it this way
a) saved many, many characters in the initial form
b) allowed a very quick and safe way of transitioning to the new syntax
cheers,
Chris Maunder
CodeProject.com : C++ MVP
|
|
|
|
|
Could be, only the project is very new (1 month old) and this function is used on 1 page only (so it was not much work to do a Search & Replace on 1 page).
|
|
|
|
|
appreciate ur guts to write these lines of code
Ravie Busie
Coding is my birth-right and bugs are part of feature my code has!
|
|
|
|
|
...so he would have to use the function at least 60 times just to make up for the fixed cost of the function definition...
|
|
|
|
|
Shortly before my co-worker left for vacation today, he wrote something like this:
strNew.Replace("%20", " ");
strNew.Replace("%21", "!");
strNew.Replace("%22", """);
strNew.Replace("%23", "#");
strNew.Replace("%24", "$");
strNew.Replace("%25", "%");
strNew.Replace("%26", "&");
strNew.Replace("%27", "'");
strNew.Replace("%28", "(");
strNew.Replace("%29", ")");
strNew.Replace("%2A", "*");
strNew.Replace("%2B", "+");
strNew.Replace("%2C", ",");
strNew.Replace("%2D", "-");
strNew.Replace("%2E", ".");
strNew.Replace("%2F", "/");
strNew.Replace("%2a", "*");
strNew.Replace("%2b", "+");
strNew.Replace("%2c", ",");
strNew.Replace("%2d", "-");
strNew.Replace("%2e", ".");
strNew.Replace("%2f", "/");
strNew.Replace("%3A", ":");
strNew.Replace("%3B", ";");
strNew.Replace("%3C", "<");
strNew.Replace("%3D", "=");
strNew.Replace("%3E", ">");
strNew.Replace("%3F", "?");
strNew.Replace("%3a", ":");
strNew.Replace("%3b", ";");
strNew.Replace("%3c", "<");
strNew.Replace("%3d", "=");
strNew.Replace("%3e", ">");
strNew.Replace("%3f", "?");
strNew.Replace("%40", "@");
strNew.Replace("%5B", "[");
strNew.Replace("%5C", "\\");
strNew.Replace("%5D", "]");
strNew.Replace("%5E", "^");
strNew.Replace("%5F", "_");
strNew.Replace("%5b", "[");
strNew.Replace("%5c", "\\");
strNew.Replace("%5d", "]");
strNew.Replace("%5e", "^");
strNew.Replace("%5f", "_");
strNew.Replace("%60", "`");
strNew.Replace("%7B", "{");
strNew.Replace("%7C", "|");
strNew.Replace("%7D", "}");
strNew.Replace("%7E", "~");
strNew.Replace("%7b", "{");
strNew.Replace("%7c", "|");
strNew.Replace("%7d", "}");
strNew.Replace("%7e", "~");
|
|
|
|
|
|
At first I thought he was smart to use Excel to generate code using ="strNew.Replace(""%" & DEC2HEX(A1) & """, """ & CHAR(A1) & """);" to save typing of boring code that anybody can type.
But then I noticed case sensitive replacements that as possible to generate using following VB.NET code:
For i As Integer = 32 To 126
Dim ch As Char = Chr(i)
If Not Char.IsLetterOrDigit(ch) Then
Console.WriteLine("strNew.Replace(""%{0:X2}"", ""{1}"");", i, ch)
If i Mod 16 > 9 Then
Console.WriteLine("strNew.Replace(""%{0:x2}"", ""{1}"");", i, ch)
End If
End If
Next i
It's still less typing than traditional way to do stuff. Genius!
|
|
|
|
|
Your coworker is a genius.
strNew.Replace("%7E", "~");
...
strNew.Replace("%7e", "~");
This one adds another wtf to the thing. You should probably send to to the dailywtf.com.
|
|
|
|
|
Apart from the fact that it's a horror code, it will just won't work as he needs to assign value returned by Replace to strnew
strNew = strNew.Replace("%20", " ");
|
|
|
|
|
If the code was written on C++ strNew might be a CString, if it was written on C# it might be StringBuilder.
|
|
|
|
|
|
not if its a StringBuilder
Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer.
-Fred Brooks
|
|
|
|
|
any regex alternatives by your co-worker? that might be really interesting...
|
|
|
|
|
Assuming strNew is of a type that allows replace to be used upon it in that fashion, would its treatment of "%253F" match any known standard?
|
|
|
|
|
"%253F" → "%3F" → "?". I don't know of any standard that encodes a question mark that way.
|
|
|
|
|
This minor mistake is easily fixed by moving the %25 -> % replacement to the bottom of the
magnificent method.
|
|
|
|
|
Your co-worker definitely needs the vacation. Months of rest and medication may help them out.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
|
|
|
|
|
I just received a crazy lecture from one of my superiors when I tried to introduce NULL pointer checks in the code. Apparently, he says, "Pointers should never be NULL" and "Checking pointers for NULL throughout the code eats too many cpu cycles and seriously degrades performance", "It's better just to crash and provide a stack dump."
Am I on crack?
|
|
|
|
|
One word of advice: RUN!
--
"My software never has bugs. It just develops random features."
|
|
|
|
|
I fully agree that pointer should never be NULL, however, crashing is not a good idea. I think contents of a pointer may be NULL not a pointer. In my opinion it indicates that not proper homework (or groundwork)is done before coding. I do check for NULL pointers but only in debug version and to eliminate any such occurrence.
|
|
|
|
|
The only way to guarantee his philosophy of crash and burn would be to set free pointers to null.
|
|
|
|
|
Yeah, OK, maybe on Planet Perfect where they also don't have referential integrity on production databases.
Why lock your car? If it gets stolen, just call the police.
Why carry an umbrella on a clear day?
Why wear a condom?
|
|
|
|
|
I think most agree that once you declare a pointer than the next logical step should be to point it to something. Preferably that something is a value other than null. However, there are perfectly legitimate cases for assigning a null value to a pointer , for example the implementation of a linked list where the last element points to nothing (null). These situations should be by design.
If you follow the practice of assigning values to pointers as soon as you declare them than you should minimize the risk of dereferencing a null pointer. However, like anything else there is no guarantee. Thus I recommend some type of centralized error handling scheme that would gracefully handle this exception as opposed to Null pointer checks prior to using the pointer.
|
|
|
|
|
You have good start with your superior¡ He is right about "Checking pointers for NULL throughout the code eats too many CPU cycles and seriously degrades performance". And, of course, pointer can be NULL.
Most people agree: to check pointers for NULL on every parameter in public or protected member of the class, and private and internal members shall not waste CPU to check for something that by logic will never be null. If you not sure about something you wrote, insert Debug.Assert (C#), assert (Java), or ASSERT (MS C/C++). They will be excluded from production code and it will "... just to crash and provide a stack dump" during debugging/testing.
Every code piece has to be executed during unit tests. And if you have something like if(obj == null) return false; and obj is never null according code coverage, then something wrong: either create test to cover that case (which will be impossible when it’s hidden inside private members), or just remove to not spend one CPU cycle .
Check also http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Contract-Oriented-Programming-and-Spec/[^].
|
|
|
|
|
You could use assert's as defensive programming and leave the production code without the checks.
|
|
|
|
|