- 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
And Python 3 fixed so many other things about the type system too; they came so close. Like
5 < "string"
now produces aTypeError
like it should, instead of ordering things in some basically-random way.Admin
Yes, this is obviously decompiled code.
I suspect the original code looked something like this:
Still :hankey: , but not quite as bad as the decompiled version.
As for how the decompiled version ended up in the code base...
Admin
That's the first thing I noticed. Reusing a variable is not a good practice, but there has been worse.
This double-negative logic though, that is priceless... and reused constantly throughout the whole procedure. Plus reassigning and testing the same variable inside the block is a double-hit combo.
Admin
csc is nothing close to a decent optimising compiler. In fact it does not even claim to be an optimising compiler. That it a job for the JITer, which may in some cases even do the opposite for allowing the processor to more easily reorder instructions.
Admin
Something finally broke free today...and I finally had to acknowledge it and now y'all just have to live with it:
One busy, two busy, three busy Booleans, Four busy, five busy, six busy Booleans, Seven busy, eight busy, nine busy Booleans, Ten busy Boolean flags.
Ten busy, nine busy, eight busy Booleans, Seven busy, six busy, five busy Booleans, Four busy, three busy, two busy Booleans, One busy Boolean flag.
Sorry.
Admin
Sorry, @Aron_Tsang, I really didn't mean to do it in reply to you, but there's DiscoReply for you. Always a zillion reply buttons visible, but never the one (you want to/should be) clicking
Admin
Thank you. It saves me from having to do it.
Admin
IDisposable
is to have ausing(IEnumerator<JobWorkItem> enumerator = recepients.GetEnumerator()){ code goes here}
statement, and never manually callDispose()
. When you go out of scope for theusing
statement, the compiler calls that for you. So that's its own little WTF for anyone who has used C# for anything remotely non-trivial.Admin
Admin
I recall having to do that because GCC 3.4 (IIRC) was a twat about things and either complained or generated code that crashed (can't remember which) on
delete NULL;
...Admin
There are cases where I might do something like that to, say, make sure something got disposed in a catch. C#, being a managed language, you don't explicitly "delete" variables. The garbage collector handles that for you. But some types might use resources that need to/should be release immediately to prevent chewing on the system's resources. In those cases, the classes should inherit from the IDisposable interface to provide a "Dispose()" method that will clean up stuff like fill handles or references to unmanaged resources. In those scenarios where you want to manually force a dispose, you have to check to make sure that you variable isn't NULL before you do that because, objectInstance.Dispose() will throw a NullReferenceException at runtime, if objectInstance is NULL.
Like Mouajin said, 95% of the time, the proper way to handle this is with a "using" statement to define the scope in which the object will live, but there are a few exceptions.
Admin
Admin
I thought you were saying (when talking about “managed language”) that the large majority of the time, you don't say anything at all about deleting the object. As long as the object isn't bridging into the unmanaged space — the massive majority of objects are like this — that's the right approach. :smile: It's only with the remaining minority that you're talking about explicit disposal, and then that's definitely best done with
using
where possible; scope binding is a great way to do that sort of thing provided you've got a suitable scope.The problems only come when someone mistakes “it's a very good idea” for “it's the LAW!!!” and loses sight of the fact that some cases persistently don't fit nicely with such models.
Admin
Are you sure about that? No mention of duck typing in MSDN
Edit: just checked in VS, on a .Net 4.5 project. Having a Dispose() method isn't enough, you need IDisposable
There are occasional cases where a `using` block doesn't make sense. In most of those, refactoring so it does makes more sense, but you don't always have the time to do that.Admin
Admin
I doubt it. That would be a breaking change for any code that relied on it, and they try pretty hard to avoid that. Maybe there were proposals to add it but they never went through
Admin
(Curiously, without using the option that enables trigraphs, all versions of g++ are C-- compilers, because of the notorious "slash-slash comment that ends with 3n+2 question marks and a slash" problem. With trigraphs enabled, the comment becomes 3n question marks and a backslash, which then splices the following line to the end of the comment... With trigraphs disabled, the ??/ remains ??/, and the line that follows remains a separate line.)
One (or maybe more...) version of the Sun C++ compiler didn't have the right destructor scope rules for isolated brace blocks:
I'd normally expect
thingy
to be destructed at ((1)), but this Sun compiler destructed it at ((2)).Admin
If you're writing code that requires trigraphs, you are bad and should feel bad. Or you are stuck on one of the Bad Old Platforms and should watch out for Codethulhu.
Admin
Admin
:wtf:
Admin
Admin
More to the point, ASCII won out over EBCDIC and the C++ spec should acknowledge this by removing trigraphs. Minimally from the required feature set, but preferably from the current timeline by going back in time and killing the grandfather of their inventor. It's the only way to be sure…
Admin
Admin
I was under the impression IDisposable was a requirement for the using.
There have been rare cases where I have had to refactor existing code, without "breaking" the current implementation. For example, adding a method overload to include a Linq2Sql DataContext as a parameter because we now need to know the original context for something, but I don't want to have to track down every line of code that was calling the original method signature. All the code gets moved to the new method, with a try/finally; original method now just calls the new signature with a "null" for the context parameter. First thing we do under the new method signature is to check and see if the passed DataContext is null and instantiating a new one if it is. If we created a local context, set a bool value to "true" so that, inside of a "finally" block, I can dispose the context only if it was created within the method itself.
They're rare "dirty hack" scenarios that are sometimes unavoidable when you're dealing with a system that has a half-million lines of code, a tight deadline, and a bad PM who keeps agreeing to all kinds of changes without adjusting the deadline.
Admin
Admin
It's also possible that there is some scenario under which you are entirely correct and I just don't know about it :smile:
It's not like it would be difficult to check for a "Dispose()" method on a class using reflection.
Admin
You wouldn't even need that; all the compiler does is convert the
using {}
to aOK, the true transformation is a little more robust, but that's the gist of it. And if there's no
Dispose()
, compiler error :smile:Admin
True, but you're actually overcomplicating it a bit:
^^^ Compiler error: 'Object' does not contain a 'Dispose' method.
It's the same result as what you're proposing. With the IDisposable interface inheritance, I can just do this:
... and that will compile and run just fine. I'd rather use the interface method ;)
Of course, to avoid the cluebat to the face, I will point out that the proper way to do this would be:
Which is much cleaner than any of the other options :)
Admin
I… :headdesk:
I've had my head in NodeJS too long :smile:
Admin
lol. I'm trying (time permitting) to learn NodeJS right now. It's cool, but definitely a very different animal from C#.
Admin
You got it backwards, it looks like:
http://blogs.msdn.com/b/ericlippert/archive/2011/06/30/following-the-pattern.aspx
Admin
You're a bad person.
Admin
As opposed to deliberate "accident"?
<SCNR
Admin
If you mistake your code window with your FB Messenger window, you're in deeper shit than the trigraphs will ever put you in anyway.
Admin
well it's a bit more complicated than that because it does it direct into IL instead of C# and then reinterpreting, but yes that is the moral equivalent of the syntactic sugar.
tiny pedantry i know, but it had to be said.
Admin
sounds about the right amount of time actually...
:fa_trophy:
Admin
And, of course, nobody picked up on my error, the one about 3n+2 question marks. ??? isn't a valid trigraph, so only the ??/ at the end matters, and therefore I should have said "at least two question marks"...
Admin
Admin
Name one valid reason other than "being an idiot".