- 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
2003 gives intellisense either way
Drak
Admin
Doesn't c# throw a compiler error if you don't initialise a local variable?
e.g. the following results in the error "Use of unassigned local variable 's'"
<FONT size=1><FONT face=Arial><FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> Main(<FONT color=#0000ff>string</FONT>[] args)</FONT></FONT>
<FONT face=Arial size=1>{</FONT>
<FONT size=1><FONT face=Arial><FONT color=#0000ff>string</FONT> s;</FONT></FONT>
<FONT face=Arial size=1>Console.WriteLine(s);</FONT>
<FONT face=Arial size=1>}</FONT>
Admin
AFAIK, switch statements are not compiled this way in C... they are build by creating an array of jump distances corresponding to the cases and then jumping directly.
e.g.
So, basically switch cases against byte values are compiled without comparisons... :-D
Admin
Probably wrong. My guess would be that some languages (notbaly C, and in some cases Java) have no guarantee of what declared variables have until they are assigned to. If you try to read from them, you may get just about any behaviour from your program.
On the other hand, we now have compilers that warn of such usage (and in Java it's even an error to read a variable that the compiler cannot prove is definitly assigned to), so these days it's actually better not to initialize them manually unless you have to - it lets the compiler catch potential bugs in your code for you.
Admin
What you say here may be true but I thought the reason is that if you don't assign a value, there's no way to know what the value will be. The values of the variables don't default to anything. So if the variable is a pointer, and you try to dereference it, there's no knowing what you might get. It might even be accessible creating a bizarre result.
In languages like Java and I assume C#, the compiler ensures that each variable will be set in one way or another before it is used.
Admin
Java builds jump tables for switch statements. That's why you can only switch on integral types in Java.
Admin
I agree. I don't think using switch amounts to a hill of beans in terms of peformance. But it's still on of the reason often sighted for using switch. Also, if this wasn't the reason for it's inclusion in the language, why not let developers switch on Strings and use non-literal cases?
Admin
a. This is not C++
b. Even in C++, assigning one constant and then another one in the next line makes no sense at all
Admin
(This was in response to "V." - the forum is totally broken in Firefox...)
Admin
In Visual Studio 2003, for C++ at least, for that same if statement, if x==1 evaluates to true, I don't think it will evaluate for y==2 or z==3. That could be a result of compiler optimizations. In Visual Studio 6.0, I think it would try to evaluate the whole if statement.
Admin
Nope. "Short-Curcuit" if() evaluation is required by the Language Standard going all the way back to the original K&R "C Programming Language".
It's really necessary in a language where you can write:
Admin
I don't think any C or C++ compiler generates code without shortcut optimization. A lot of C programs would otherwise crash, like the following example:
if (s != NULL && !strcmp(s, "")) {
...
}
would cause a null pointer exception (memory fault, access violation, whatever ;-)
Admin
The time appears correct to me. Note that it it trying to display the timestamp in your local time zone, so you have to go to your account page and set your time zone correctly there.
Admin
you were 10 seconds faster, grrrrr ;)
Admin
umm... Ya'know that's kind-of a mini-WTF itself, as it's equivalent to the much simplier/faster/readable:
Admin
Ah. A user error problem.
Admin
No error as far as I am concerned. GMT is fine with me.
Admin
Nothing is really bad about "select case true", although it's too verbose.
Ruby has the same construct and it's actually recommended:
case when x > 3 puts "x > 3" when x < -5 puts "x < -5" else puts "42" end
It's allowed to short "case true" to just "case" in Ruby to make it more (cond)y.
In BASIC, however, ELSE IF-constructs look more sane. But that might be just me.
Admin
This is at most a nano-WTF, but honestly, in most cases I prefer the version with strcmp; your version is of course faster, but it does so by expressing "how it is done" instead of "what is done". You notice the difference when you imagine an automatic translation of the above code to other programming languages.
Admin
dim x as string
x = "" 'creates memory space for variable
x= "something" 'destroys orginal memory space and creates new space
Calling that code once makes no measurable difference, calling it a million times will
Admin
I'd agree --- IF you had written it as "strcmp(s, "") ==0" rather than "!strcmp(s, "")". My version implies equality by using the equality operator, while yours implies equality by using the NOT operator. Hence it would be very easy for someone to read your original line ("if (s != NULL && !strcmp(s, ""))") as "If s is not NULL, and is not empty" (which is wrong), while mine would be read "If s is not NULL, and is empty"
Admin
See that's the thing... I was writing a couple programs in VC++ 6.0, and I'd write something like this:
if(i<array.length() && array[i]==somevalue)
{
//do stuff
}
and the bugger would crash on me when i==array.length because it was out of bounds!
so I had to do something lame like this:
if(i<array.length())
if(array[i]==somevalue)
{
//do stuff
}
Now of course, the same code compiled in VS 2003 works fine.
It would be sad if 6.0 was really that bad...
Admin
Hmmm... I don't think I've ever written strmp(...)==0, always !strcmp(...). I aggree it's easy to misread in a proportional font (like in this forum), but I don't see the problem in an editor using a monospaced font. In my world, !strcmp is an idiom for "equality". But maybe it's just me.
Admin
Crash in the debugger? Or really crash when compiled and executed outside VS? Can't believe...
</array.length></array.length>
Admin
VC++ 6.0 DLL being called by a Delphi EXE. Memory exception in release mode, array out of bounds error in debug mode. Working on completely different projects with VC++ 2003 often dealing with arrays I never get that problem.
Admin
I invented it to evaluate which radio button is set:
<FONT size=1>(BTW this posting interface is horrible. It's taken me 20 minutes to compose this reply, it doesn't offer PRE, the size setting comes out wrong during preview, and the HTML view disappears.)</FONT>
Admin
I'm not good with the goofball VB syntax. Haven't used it since 'intro to programming.' What does:
INI.AnInteger("PreferredRemedy", aTransName) = cmmDelete
do? Are you assigning a variable to a method here?
Admin
This is why I'm counting my blessings that I'll never have to touch Visual Basic code ever at my job here... *coworker asking to have Excel macros edited* Oops, I spoke too soon.
Seriously, as a favor though, for the sake of sanity, can you guys please please please please please please stop using switch(true)? It makes baby Jesus cry and doesn't make your code run any faster.
Admin
Aaaaaaahhh, you were using a competing product together with a Microsoft product. Did you try
#undef __DETECT_AND_CRASH_DELPHI
Admin
Heh, or how abount: #pragma never USE_DELPHI
Well, since you asked, this is a freeware game called Civ Evolution written in the free version of Delphi. It's written in such a way that you can write an AI Module in any language, so long as it is a DLL. Unsurprisingly, most of the AIs are writting in C++. I'm always surprised that people will actually develop in Pascal. From my understanding, I thought it was only intended as a demonstration language.
Admin
From my understanding, C++ was meant as a practical joke, but people use it anyways.
Admin
Admin
Is it really wise to sacrifice readability to spare a couple CPU cycles shorting conditions in a Windows environment that renders stuff with subpixel anti-aliased scalable fonts?
I don't know what the COM object does, but it cannot be that expensive (or someone should really take a look inside it)
Anyway, Select Case x.error should have been used instead.
Admin
Why wouldn't the jump table also be faster for the interpreted language? The Java bytecode has an instruction for a jump table. I timed a simple example with the -Xint switch to force the JVM to use interpreted bytecode. There were five packed cases, 1-5, and the version with the switch was 34% faster than the if/else version. Of course, with sparse cases it might revert to doing the equivalent of a series of if/elses, but so might C.
When I let the JVM use the JIT, the times were much closer together. The switch still won, but only by a few percent.
Admin
I wish you'd post your real name, so I could be sure never to hire you.
Admin
It's an example Supersitious Code, perhaps as a result of a Voodoo Chicking Coding experience in the recent past.
Oooh! Maybe if you're using non-ECC ram, one of your variables will magically change! You better try-catch every line of code just in case!
Admin
That was supposed to be Voodoo Chicken. WTF is a chicking anyway... there must be some psycholinguistic explanation to my WTF.
Admin
The syntax is irrelevant.
The point is that I had to do different assignments depending on which radio button is checked, and that the Select True clause provides a nice uniform way to express this.
Admin
In the event of "strange things", you have not understood constructors, nor have you read Myers.
Please stop coding in C++ until you have caught up.