- 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
You might think you can replace this gem with one line, but you can't. Because when "ApplicationUserInfo.Current.ApplicationType" is "ApplicationTypeEnum.Stradegy2" then "ApplicationUserInfo.Current.ApplicationType.toString()" returns "MagAdvisor".
Fooled you, huh?
Admin
Admin
Admin
EDIT: now if you can make THAT a one-liner, I'll owe you beer and wings.
Admin
Another wtf is that we don't know whether that function should merely convert the value or whether the intention was to filter it; maybe the writer was only interested in those 12 values.
Admin
Admin
Bonus points get!
Admin
4 Years ago, I had seen functions like this. I replaced them all. There was a function for each enum. There was about 50 enums :(
Admin
How come this story isn't the top hit for "stradegy"?
https://www.google.com/search?q=stradegy
Admin
Don't do it in a tight loop - the innocuous-seeming Enum.ToString and Enum.Parse invoke reflection to query the metadata and figure out the name you gave to that enumerated value.
GetName uses a hash table for quick look-up; that hash table is filled when GetName is first used for the type and holds up to 100 types (though on the 101st it dumps the lot!). It uses a special function in the runtime optimized for this rather than the generic reflection system. It's still way more costly than a cast to int, though.
Admin
OK, correct answer is of course:
...but an even correcter answer would be: ...why convert it to a loosely typed int in the first place? If it needs to be sent in as an argument for a method call then use: ...or better, just change the method to accept an enum of type ApplicationTypeEnum. And also changed the name of the type to just ApplicationType, drop the 'Enum', it's hungarian notation all over again.Admin
<!-- This is a comment solely to annoy Hortical -->
Admin
I didn't look if anyone corrected you yet, but the "ToString" there is going to do something you didn't want; correct is:
int _applicationId = Convert.ToInt32(ApplicationUserInfo.Current.ApplicationType);
Admin
TRWTF is that this site is all about poking fun at people who aren't good at writing code, but the person who runs the site is incapable of writing a single post without a typo, no matter how short that post is.
Admin
Moreso, you can write an implicit conversion so you can skip the explicit cast!
Admin
this is not true.. without the break after each return will not compile...
Admin
Admin
I think that return; break; would actually not compile at all.
Admin
Damn, lol. Beat me to it.
Admin
oohhhh... I'm feeling woozy. Maybe I should just use a pen...
Admin
Admin
Strategy + Tragedy = Stradegy
Admin
WTF7 is the real winner! No need to convert it in the first place. _applicationId should be of type ApplicationTypeEnum to begin with.
And yes, ApplicationTypeEnum.None = 0
ApplicationUserInfo.Current.ApplicationType is set when the user logs into one of the applications listed and is a static method, (sorry, didn't mention that) so there is no reason for this method in the first place.
Thank you all for playing!
TRWTF: I didn't obfuscate the names!
Admin
Your meking hard atempt at troling, but low on humerus content. Sorry brother!
Admin
kidney bean?
Admin
Also, TRWTF is #9: who ever wrote the app(s) didn't use OO principles and simply declare functionality in interfaces or an abstract base class.
Admin
private int SetApplicationId() { switch (ApplicationUserInfo.Current.ApplicationType.ToString()) { case "Stradegy2": return Convert.ToInt32(ApplicationTypeEnum.Stradegy2); case "AdDetector": return Convert.ToInt32(ApplicationTypeEnum.AdDetector); case "MagAdvisor": return Convert.ToInt32(ApplicationTypeEnum.MagAdvisor); case "AdDetectorAlerts": return Convert.ToInt32(ApplicationTypeEnum.AdDetectorAlerts); case "MarketAdvisorAdTel": return Convert.ToInt32(ApplicationTypeEnum.MarketAdvisorAdTel); case "NewspaperAdvisor": return Convert.ToInt32(ApplicationTypeEnum.NewspaperAdvisor); case "MarketAdvisorMarketSpender": return Convert.ToInt32(ApplicationTypeEnum.MarketAdvisorMarketSpender); case "StradegyOnline": return Convert.ToInt32(ApplicationTypeEnum.StradegyOnline); case "AdSpender": return Convert.ToInt32(ApplicationTypeEnum.AdSpender); case "Evaliant": return Convert.ToInt32(ApplicationTypeEnum.Evaliant); case "eBooks": return Convert.ToInt32(ApplicationTypeEnum.eBooks); case "FrenchAdex": return Convert.ToInt32(ApplicationTypeEnum.FrenchAdex);
}
Admin
I don't do C# either, but if it's like most languages wouldn't control just fall through the entire switch statement and move onto the return 0 if it didn't match any case?
I think most would consider it poor style, but not necessarily a wtf on its own. But now I'm just nitpicking I guess.
Admin
Throw, yeah, that's what I just fucking did.
Admin
It is set in a method called
Admin
If Mr T. Experience gets to him first, you might not need to give him the wings, he might just be getting a pair of his own. Or horns.
Admin
Strad + e(d)gy: Nigel Kennedy?
Admin
She's brilliant, obviously.
Admin
No, no, no. That's just German for "The Frits, the."
Admin
I don't know C# but I bet I could replace that gem with one line. Just don't ask me to make it one statement . . .
Zing!
Admin
Yeah, that's what I'd do.
Admin
Yeah, that's what I'd do.
Admin
WTF? The function should be named "GetApplicationId()"
Admin
No, I'm pretty sure that's valid. If you put a break after the return, you would get an "unreachable code" warning.
Admin
Since ApplicationName is an enum, you can just do a simple typecast. In general, typecasting an enum to int (or whatever the underlying value type is) just gives you back the enumeration value.
So if you wrote something like enum names { Tom=1, Jeff=2 }
int num=(int)names.Tom;
num would end up as 1.
Admin
Admin
You're missing the whole point, though.
This thing simply converts an int-based enum to its underlying integer value.
x=(int) y;
that's all that's required yere.
Admin
Admin
Admin
All of your one line solutions are wrong. This will print 2, which should be 0 according to the original method being replace.
public enum Foo { Test = 1 } class Program { static void Main(string[] args) { Foo bar = (Foo)2; Console.WriteLine((int)bar); } }
Admin
hmm... was this supposed to be anonymized? a quick google search brings up http://kantarmediana.com/intelligence/products
Admin
so...
he was right?
Admin
Hitler wasn't all bad, after all he did kill Hitler.
Admin
That is exactly what I was thinking. Why bother with the casting, you have an Enum setup! Now you have strongly type code.
Admin