- 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
Wow.
Admin
There is nothing shinier than long, complicated regular expressions without any comments.
Admin
I think I'm supposed to sit here in my pajamas and think about what this code is doing before I can find it funny. I just can't...
Admin
While the second two are WTF's, the first one is NOT.
The "T y = (T) x;" for suffers greatly from discoverability. The implementation of a "T Cast<T>(...)" makes casting much more discoverable.
This is (partially) the reason why C++ implemented dynamc_cast<T>, static_cast<T>, reinterpret_cast<T>, const_cast<T>.
Admin
The seperate cast methods in C++ may be the most horrifically difficult to use things I've ever encountered, especially in code that thrives on crazy pointer manipulation and Templates.
Admin
I was about to say the same thing. Anyone who thinks the first one is a serious WTF has never had to search for a bloody C style cast.
Admin
Admin
Of course they're difficult to use in crazy pointer manipulation: crazy pointer manipulation is bad. Stop doing it. Declarative casts are designed to keep type manipulation sane and compile time checked.
There are only four separate casts, and two of them are only for extreme situations. The other two are extremely simple.
Admin
That depends on the functionality you are trying to achieve: you try writing a monte-carlo neural network simulation framework without doing some very crazy pointer based nonsense. Every methodology and technique has its place (except Waterfall, that's just b*llocks).
Admin
Reminds me of a senior developer I knew. He got to a point where he was completely unwilling to learn any new programming techniques, did everything as though he had no memory and the code would never be maintained. But he was amazing, I saw him accomplish some huge feats but in the must screwed up ways.
Admin
looks like those regexes are supposed to strip superfluous whitespace, but there's a syntax error at position 6 on the first one, and the second one is useless garbage.
captcha: damnum - using the wrong magic number, and the whole system falls apart.
Admin
This is not spam, Akismet!
Admin
Throwing in a bunch of unneccessary regexes is a sure fire way to speed up execution. After all, it's not like the regex classes are slow, is it? It's not like they have the highest cyclomatic complexity of any classes in the entire .NET framework, is it? What's that? They do? Oh, crap.
Admin
You idiots. In the caching example, he is doing the right thing.
When an app gets heavily used it is often SLOWER to get from the cache than to compute the data on the spot. This man obviously has the experience that all of you lack to foresee this.
The cache insert is there for REDUNDANCY, because this senior developer knows very damn well that one of the junior devs will screw up the system, and that's why you need to make sure you have backups.
Truly this man is top notch.
Admin
Admin
this will crash if your object is null, and this new notation is a big fat and ugly bull######it.
If you think the cast function is useful, you're the kind of person who create these functions :
int Multiply(int i,int y) int Add (int i, int y) int Substract(int i,int y)
Why do these stupid programmers create operator when we can create dozens of useless functions ?
Admin
C-Pound (lol) has the same style of static casts as C. It's pretty likely they chose this style because it was mimicking C. Dynamic ones can be done with different syntax, thankfully.
As the issue with C style casts is how good they are at hiding, all the languages that do them in the same style have the same issue, whether it's C-Hash, Java, etc...
Admin
Write code that has better type safety, then casting becomes less of a concern. :P
Admin
Admin
I hope you're being sarcastic.
That function may be much more discoverable, but it creates penalties from boxing/unboxing (if using value types) and an unneccessary function call. It also negates compiler checks for casts. The following code will compile using function in the article:
BTW- Who needs to search for all instances of casts anyway?
Admin
Admin
2)Being able to quickly and reliably locate casts is very often an important task, especially when considering re-factoring. In many high-reliability/high-perormance applications documenting all of the casts [except for implicit casts to a base/interface] is regularly required.
Admin
Cast-by-example is one of the most wretchedly evil hacks ever discovered. Any code I see using this is going straight to the trash heap.
Admin
I miss dBase...
Admin
Not a WTF. Doing caching like this makes sense on embedded systems where you don't have a filesystem to retreive previously cached data from.
Admin
Admin
Better still would be to put the cast after the variable name so you can continue down a long chain without all the brackets: datasource<DataTable>.rows[0][0]<string>.StartsWith("a")<int>;
Admin
Casts are evil!
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.11
Admin
Of course they are.
http://yosefk.com/c++fqa/picture.html#fqa-6.15
Admin
I live for the days when bug reports from our ColdFusion legacy apps land on my desk.
Sarcasm? You decide :o)
Admin
I prefer C-Hash
Admin
D'oh! Sorry I wrecked your URL tag.
CAPTCHA: ullamcorper. In common with everyone who lists their CAPTCHA I can't think of anything funny, I just think it's a magnificent word
Admin
Casting to one side (er) ...
The first two examples are very nearly perfect implementations of the venerable HP engineering adage, "Keep It Simple, Stupid!"
The third example is, unfortunately, an anti-KISS.
The problem with the first two examples is that the Senior Developer has replaced the comma in KISS with an ampersand.
Admin
Admin
Well, you know, in embedded systems without a file system...
Admin
As someone who's been writing to (and maintaining!) a fairly large perl codebase, I'm getting a kick out of your reply...
Admin
Admin
Admin
The C++ implementation is a huge step forward, particularly if you code in emacs. (Doesn't everybody?)
I can faintly see a justification in Java pre 1.5 (or whichever it was), where everything transmogrifies, in a sort of sub-Heisenberg way, into an Object and back again.
I can't see any justification whatsoever in C#, which not only goes to great lengths to avoid the need, but is also invariably coded within a language-aware IDE. Disclaimer: I am not a C# programmer (or a Java programmer). I just think that the first code snippet has a peculiar smell; and that's without even considering exceptions (as above).
In these circumstances, I like to ask myself whether I know better than the designer(s) of the language. To five nines accuracy, my answer is that I do not.
Why would I be providing syntactic sugar when the language designer has decided that it is either unnecessary or a very bad thing?
Admin
I wish somebody would proofread the articles before posting :/
Admin
The Cast<T> function works well as an extension method for a Fluent API. I used something similar in a hand-rolled Dependency Injection framework. I wrote my implementation as "return value as T;" instead. In this way, the responsibility of testing for null is on the caller, and the exception is only thrown if they fail to do it.
Admin
What is with all these so-called "Senior Developers" and crap code (and usually a crappier atittude)? Nearly every "Senior Developer" I've worked with hasn't been senior at anything and seems to be senior in terms of tenure and not skill.
Admin
Proofreading is left as an exercise for the commenter.
Admin
Well almost never.
If you really need to be sure it is a number (but why would you?) you can simply:
Or if you want a string:Admin
TRWTF is using C++ as a good example.
Admin
Maybe I'm just lucky, but where I work, our Project Architect (the senior developer on the project I'm on) is exactly what a senior dev should be. He's literally been with the company longer than anyone but the CEO, he seems to know what anything in the program does and where the code responsible for it is, and he's friendly and easy to talk to.
Oh, and he writes pretty good code too.
Admin
No, he's not being sarcastic.
I'm not an expert on C-NumberSign, but those casts were designed for C++ (which I do know a thing or two about). Here it is straight from the horse's mouth:
http://www2.research.att.com/~bs/bs_faq2.html#static-cast
To summarize, the primary reason for the new syntax is to make casting intent more clear. The secondary reason is to make casts stand out more visually: "An ugly operation should have an ugly syntactic form." Lastly, Stroustrup suggests that programmers will simply avoid casts because of this syntax. (Unfortunately, most programmers seem to fall back to the C-style cast syntax, from my experience.)
So, maybe TRWTF is that C# didn't import the C++ casting syntax ... and this programmer decided to put it back in? (again, I'm no expert on C#, so please correct me if I'm off base here).
Admin
In version 1, you cache the data.
In version 2, you retrieve the data from cache and crow about how much faster it is.
Admin
Admin
In C++ templates, the compiler generates the code at compile time, so you still get compiler type checking and code optimization. C# generics do none of this. Note my example:
This not only compiles, but signal my intent to cast "I Like Puppies!" to a long. I'm not sure where this could ever be useful.