|
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.
|
|
|
|
|
Your not on crack, I also follow the same defensive programming philosophy. Checking pointers for NULL does not harm performance at all and anyone who tells you that is mentally deficient. Instructions executed on modern processors are measured in MIPS[^] which is to say millions of instructions per second. Current processors are capable of executing billions of instructions each second.
Of course checking for NULL pointers is a philosophy however its one that I follow. I always check for NULL pointers before using them and when I am done using my pointer I assign it a NULL value. Its two simple rules that I follow and guarantees that I never have a NULL pointer exception in my code.
In my opinion a program should *never* crash to the desktop. I believe it is the engineers duty to detect the NULL pointer exception or other unhandled exceptions and prompt the user with a dialog stating that a critical error has occured and allow the user to attempt a graceful shutdown of the application. A combination of NULL pointer checks, variable validation and a global structured exception handler[^] gives applications stability and reliability. Not all software is created equal, I want mine to be in the top percentage.
Only arrogant programmers do not follow these types of rules, they seem to think their code will never crash. Eventually they are wrong, usually when some poor guy has been working for 6 hours and forgot to save his work.
Best Wishes,
-David Delaune
|
|
|
|
|
Randor wrote: Of course checking for NULL pointers is a philosophy however its one that I follow. I always check for NULL pointers before using them and when I am done using my pointer I assign it a NULL value. Its two simple rules that I follow and guarantees that I never have a NULL pointer exception in my code.
There is no such thing as a "NULL pointer exception" - there is "access violation" and if you follow your rules you may still very easily run into it, as demonstrated in the code snippet here[^].
|
|
|
|
|
Nemanja Trifunovic wrote: There is no such thing as a "NULL pointer exception" - there is "access violation"
Nemanja,
Come on man I know your not really that pedantic about verbiage. Call it whatever you want, "Access Violation" is simply an error title. Now your just trying to be argumentative. I'll call it segmentation fault[^] from now on just to get under your skin.
Nemanja Trifunovic wrote: if you follow your rules you may still very easily run into it
First off let me say that I have respect for both you and your philosophy. I can only hope to recieve the same. But unfortunately I have a partially opposing view.
There are many techniques to making software more robust. Assigning pointers to NULL and checking for NULL is only one of them and many companies practice it as can be seen in both Microsoft DDK and Platform SDK samples.
Logically from a mathematical statistics standpoint any technique which reduces the chance of accessing an invalid memory address would be beneficial to the software.
Magic Number[^] assignment/checking is used by all major software vendors.. Apple, Microsoft and IBM etcetera.
Recognize any of these?
0xCDCDCDCD
0xBAADF00D
0xCCCCCCCC
Initializing a pointer to NULL to denote an invalid memory address is the same magic number technique. It assists the programmer with validating pointers and most certainly assists with making software more robust.
I am not interested in arguing the point anymore. I fully expect you to reject my assertions and you are within your rights to do so. Let us agree that we disagree and leave it at that.
Best Wishes,
-David Delaune
|
|
|
|
|
Randor wrote: Come on man I know your not really that pedantic about verbiage. Call it whatever you want, "Access Violation" is simply an error title. Now your just trying to be argumentative. I'll call it segmentation fault[^] from now on just to get under your skin.
It is more than just verbiage. By following your rules (which btw really make sense in C90 where you have to declare all variables in the beginning of the function) you will indeed never hit a "null pointer segfault" but segfaults happen on any invalid pointer, not just NULL.
Randor wrote: There are many techniques to making software more robust. Assigning pointers to NULL and checking for NULL is only one of them
At least now we are not claiming that by following these two simple rules we will never segfault
Randor wrote: Initializing a pointer to NULL to denote an invalid memory address is the same magic number technique. It assists the programmer with validating pointers and most certainly assists with making software more robust.
And guess what? I agree. If you initialize a pointer to NULL, and then go to different code paths in which it may or may not be set to point to a valid object, checking it for NULL makes a perfect sense.
However, this thread is about a guy who inserts NULL checks all around the code in the hope it would make it more robust. Well, it won't. If there is a bug in the code, chances are it will not be caught by a NULL check.
|
|
|
|
|
Performance aside, checking for NULL will give you a false sense of security. A bad pointer usually has a non-NULL value anyway. Ie.:
void my_function(MyType* object)
{
delete object;
object = NULL;
}
int main()
{
MyType* object = new MyType;
my_function(object);
if (object)
object->do_something();
}
modified on Saturday, December 20, 2008 6:55 PM
|
|
|
|
|
Protect against what you can anyway.
|
|
|
|
|
PIEBALDconsult wrote: Protect against what you can anyway.
Sorry, not good enough
The only sane way to avoid this kind of problems is to keep the object alive within the scope it is used and have no pointers pointing to it out of that scope. Checking for NULL is helpful only in cases NULL is a valid parameter to a function (meaning - ignore this parameter). As a safety measure, it is completelly worthless - they are billions (on 32-bit systems) possible invalid values for a pointer - why checking for 0 only?
|
|
|
|