- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It may not break anything but it makes maintenence difficult. Especially if its a huge 500+ line function. You see the variable called szOwnerName = SzGetOwnerName(); at the top of the function.
You think great, I got the OwnerName, I need it down here at the bottom of the function. Good I'm done...
Little do you know the variables been used and abused and whored to death, and is no longer the OwnerName... Granted the maintainer should probably have done a quick search to make sure it was stillt he OwnerName, but still...
Writing good code isn't always about writing code that works. It is about writing code that works AND can be easily updated in the future. By someone else.
Admin
Great thanks C Pound for doing something I didn't ask you to do.
I told you bas is equal to bar. Therefore when bar changes, baz should reflect that change. I didn't say "let baz get a copy or bar" I said "let baz equal to bar"
Admin
Does that mean that this should work?
int a = 5; int b = 6; int c = b; c += 2;
// b now equals 8??
= copies the data, in pretty much every language (even C, where it copies the address of the pointer).
Admin
I'll try that again.
Does that mean that this should work?
int a = 5;
int b = 6;
int c = b;
c += 2;
// b now equals 8??
---------------------------------------
The '=' operator copies the data, in pretty much every language (even C, where it copies the address of the pointer).
Admin
But it's already been pointed out that strings in .NET (and Java) are not mutable. It wouldn't make sense that the assignment of a new object to bar (as in changing the string, the changed string actually being a new object) would affect that which is now pointed to by baz.
Admin
It actually does "let baz equal to bar". The fact that bar==baz, even in Java proves that. The one they mess with is the concatenation.
baz += "more";
is acually implemented more like:
baz = baz.clone().append("more");
That's why bar!=baz after the concatenation. This behavior is intentional. One of the biggest problems in C/C++ is that strings are treated as char arrays. If a mistake is made on a bound check, it could lead to a very difficult to find bug or a buffer overrun vulnerability. Modern high-level languages fix this by always reallocating strings and never editing them in place. I've yet to meet a language where strings aren't at least a little weird. For example, they are usually one of the only classes with a literal expression form.
Admin
I think it's quite obvious that whilst he was hired as the contractor chargin the second highest rate, they only had 2 contractors apply for the position. It would explain a lot :P
Admin
Yeah its c# (DateTime.Now.ToString(), 'string' vs 'String' etc).
Admin
Or if you don't have anybody on hand to do that you could ask for a code sample. Have the consultant write a "Hello World!" app for you. Anyone who mis-spells "World", tries to be too fancy or generally handset in more than 10 lines of code, should be slapped across the face with a Cheque book, and sent on there merry way. Anybody else... might just be worth the risk.
Admin
Of course it is needed. Every **expert consultant** knows that triple (at least) redundancy is essential in "mission critical" applications.
Admin
It doesn't do something you didn't ask him to do, it does what it's supposed to do and you didn't understand what you asked for.
Here, bar and baz are two names bound to the same object, two references to the same object, think of two pointers to the same location. Good.
Now the big thing is that you can't modify the object referenced, since it's a string and both Java and C# strings are immutable. This is the first part.
The second part is that when you later do
baz = "chocolate"
, you're not modifying the object baz holds a reference to (the object thebaz
name is bound to), you're rebindingbaz
to a new object... which isn't the objectbar
is bound to anymore.If you want mutable strings, use mutable strings.
Admin
Nope, that is consistent, on user-defined types the only way to automatically define the equality of two objects is to use identity.
C# can't have the knowledge of the semantics of your object, it therefore uses the lowest common denominator: an object is equal to itself. That's perfectly sensible and logical.
Admin
Assuming this is C# (ToString() instead of toString(). I'm in no way familiar with Java, though.), string.Clone() does not return an actual clone, but rather a reference. This is an exception for strings, reason being that strings are immutable. You can't alter a string, so there's no point in copying it.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemStringMethodsTopic.asp
Remarks
The return value is not an independent copy of this instance; it is simply another view of the same data. Use the Copy or CopyTo method to create a separate String object with the same value as this instance.
Admin
So? Why let this fact get in your way?
Admin
What he's calling inconsistent is not that part, it's the behaviour after the (allowed) overloading of the operator. I smell another debate on operator overloading coming...
Perhaps we can avoid it by focusing on the other part: what are MS's "policies" (coding standards?) on overloading == and what's retarded about them?
Admin
where it also was useful to avoid the hassle of scrolling up to the beginning of your function to declare another variable...
I'm sorry but anyone who alters there code style based on the tediousness of scrolling to create a variable belongs in their own daily entry, not commenting on why todays could be justified.
Admin
The post I'm refering to in my post
Admin
Heh, just thought about it today... How many of us (well, not including myself, obviously) code things that others go 'WTF?!?!?' when they see them ;)
Anyway, I think this was a decent WTF!
Admin
>Initialize all variables at the point of declaration; null is prefered for reference
> types and strings while min- and max- values are preferred for value types,
> e.g. DateTime.MinValue.
Besides the obvious issues with the code, may I mention that this is really lame in practice- any time you step through a constructor, you have to wade through a bunch of fields being initialized before you actually get to the meat of the constructor's code.
Plus, nullable types make it all irrelevent if you're using 2.0...
Admin
.... but enough of your sexual fantasies[^o)] , what about the standard of his work????
Admin
Well, you can get such behavior in C++ by declaring c as a reference to b, i.e.:
int b = 6;
int& c = b;
c += 2; // now b = 8!
Admin
This sort of confusion with reference variables is precisely why we should all go back to using plain C, where variables are explicitly holding pointers or values. Buffer overrun, buffer schmoverun. We should allocate memory manually, like real men, preferably by reaching into RAM with a fine pair of tweezers.
--Rank
Admin
WTF? are you serious?
ToString() is simply a function that returns a string of some kind. The default implementation (at least in C#) for value types is typically to return the object's value formatted as a string. For reference types, it usually returns the object's class name, but should be overridden to return a string that has more semantic relevance.
String.Format() gives you culture-specific formatting, but it calls ToString() on the object internally.
I see no reason to relegate ToString() to debugging.
Admin
Just a personal note here, but if someone reused a variable name as THREE DIFFERENT VARIABLE TYPES (Date, Boolean, Filename), even if they are all string representations, and the variable is named anything except "StupidStorageString" they are completely incompetent. That kind of crap makes code unmaintainable, and is the worst kind of sloppiness.
Admin
For a start, the above is C#, secondly, Huh? Why?
Admin
True, but if you are recursing deeply you can run out of stack space. If you can reuse one int (4 bytes on a typical system) inside a function, you save the space of that one int each time your function is called. If your function needs to call itself 1 million times (presumably this is a bottle neck anyway and you are looking for every little trick to get more speed out of it), you can save 4 megabytes of memory. Carefully reuse it in 5 different ways, and you have saved 20 megabytes. If you are using other variables that cannot be reused (perhaps some strings), and you might be hitting swap, so not having these extra variables around can result in a large speed increase.
If the dataset will only results in a couple levels of recursion, this is a waste of time. Most recursive functions never get more than 100 levels (if that much) in my experience, so it isn't worth bothering with memory savings.
Of course it is a WTF to not use a union (at least in C/C++) for variables that you reuse, as the union will indicate that you really meant to reuse it.
Admin
Folks to put this to rest... the plain and simple nonpreferential WTF isn't what the programmer assigned the default variables to but rather that this consultant failed to understand the [simple] standards.
RIP
Admin
All that ToString().ToString() redundancy is just silly. Why not just be a typical clever consultant and get it over with recursion?
public string ToString(string s) {
return ToString(s);
}
-J-
Admin
You're kidding, right?
I don't do Java, but if I saw a string set to "null", I'd be pretty sure that there was no enlightenment to be had in the rest of the code.
Neeeeeexxxxxxxt. . .
Admin
Hey, I think I some some data files the null-man created. Interspersed in client address fields was the word "null" (not quoted).
Sigh. . .
Admin
C#, Java programmers can drive cars.
C programmers can fix cars.
Admin
This has "code generator" + lazy written all over it.
Admin
Enough with the syntax corrections.
How about some hints on how to spot these useless people.
Admin
Did you even read what I wrote?
Admin
Look in the mirror?
<font size="2">sorry, couldn't resist <g></font>
Admin
I'm gonna back you up on this one as well...
At least in Java, toString() comes from java.lang.Object:
toString
toString
method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.The
toString
method for classObject
returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@
', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:so obviously a "String representation" of a String would be the same String, but the reason toString is present in String is because of OO.
Admin
As opposed to which language?
Admin
I certainly hope he's using recursion to copy the characters one at a time.
(Holding prayer-beads, hitting Post)
Admin
No, only ONE copy is made. You're confusing CBstr with CBstr* (by the way, why are you using such a class? MSVC++ already gives you _bstr_t).
CBstr can override this:
CBstr foo, bar;
foo = bar;
But it can't override this:
CBstr *foo, *bar;
foo = bar;
Admin
The fact it's impossible to have a value of a UDT, only a pointer, is the whole problem here and why this issue doesn't exist in C++.
Admin
Actually...YOU left out the "Ho Ha Ha", then comes the guard, dodge, parry, thrust, then ANOTHER "Ha", Then spin...
Admin
Yes. Speakable things wouldn't hurt too much.
Admin
You picky bitch. Get off your high horse, Narcissus.
Admin
Not even mentioning that there's likely to be something very wrong if you have functions long enough to require much scrolling to reach the top of them.
Admin
I think you just answered your own question
Admin
However, since there doesn't appear to be a String.Copy method, I have the sinking suspicion that he created a string class, which extends String,
Admin
I think Sun would object litigatiously to any other language having a class called java.lang.Object.
But I susped he was aiming at languages without a single-root inheritance tree, such as C++, where there are no methods you can assume to be present in every class.
Admin
That's ridiculous! Of course you should write the test like this:
if (someString == null || someString == "null") {
// someString is really null. Both by normal convention and by consultant conventions
}
Always make your code prepared for attacks by consultants which might step into it and fsck it.
:P
Admin
Hm, seems like the quote for my post disappeared so it doesn't make much sense. New attempt:
*** Quote ***
Sorry, but that is just WRONG!
String someString = "null";
is not the same as
String someString = null;
So, later in the code, when others are checking for null, they're doing...
if (someString == null) {
...
}
and you just broke their software!
*** End quote ***
That's ridiculous! Of course you should write the test like this:
if (someString == null || someString == "null") {
// someString is really null. Both by normal convention and by consultant conventions
}
Always make your code prepared for attacks by consultants which might step into it and fsck it.
:P
(hope this ends up correct, otherwise I resign :))
Admin
Haha,
Once, I work with that king of consultant. Ahaah, all variable was “Alpha”, “Beta”, “Gamma”, ....
Men! What a head hash! <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>