Article YF3Y CodeSOD: The Apple Genius

CodeSOD: The Apple Genius

by
Remy Porter
from The Daily WTF on (#YF3Y)

Apple refers to their in-store technicians as "geniuses". Everyone on Earth knows that it's nothing more than cute marketing and is a meaningless title.

Well, almost everyone. Derick worked for a company where the CIO worked at Apple's HQ at some point. Said CIO was quite proud of this achievement, and made sure everyone knew it. He wasn't happy that his new startup had decided to use C#, but it was okay: he was ready to reinvent core pieces of the .NET framework to avoid having to deal with whatever bombs Microsoft had snuck in.

And he was going to optimize it.

 static public bool RegExp(ref String buffer, ref String compare) { bool bMatch = false; if ((buffer.Length == 0) || (compare.Length == 0)) { return bMatch; } try { // DO NOT USE RegexOptions.Compiled -> Massive Overhead - NO BENEFIT Regex _regex = new Regex(@compare, RegexOptions.IgnoreCase); Match match = _regex.Match(buffer); if (match.Success) { String matchString = match.Groups[0].Value; bMatch = true; } } catch (Exception ex) {#if DEBUG string str = ex.Message; string ttl = "Company (DBG)"; MessageBox.Show(str, ttl);#else // throw new Exception("An error occurred", ex);#endif } return bMatch; }

There's a lot of little things in here. Swallowing exceptions is always a great code smell, but it's even better to see the line that was commented out- even when they rethrew the Exception, they wrapped it in a generic Exception object, breaking any attempts to use structured exception handling to respond to the specific error.

Of course, that happens inside of a conditional compilation section- so in DEBUG mode, it raises a Message Box instead, which is great. I love clicking through piles of those when the application runs into a problem that isn't properly handled.

The use of the @ sign on the @compare is intriguing. The @ means two things in C#. First, and most often, you use it to disable metacharacters on a string literal. Rarely, you use it to let you create a variable named after a keyword: int @while = 5.

"compare" is neither a string literal, nor is it a reserved word in C#.

But finally, the icing on the cake, is his comment.

// DO NOT USE RegexOptions.Compiled -> Massive Overhead - NO BENEFIT

CIO Wile E. Coyote, SUPER GENIUS, discovered that compiling a regex does nothing but add overhead when you're throwing away the instance and never using it again. This was such a shocking discovery that the CIO had to make sure to document it IN CAPITAL LETTERS.

release50.png[Advertisement] Release!is a light card game about software and the people who make it. Play with 2-5 people, or up to 10 with two copies - only $9.95 shipped! TheDailyWtf?d=yIl2AUoC8zAan2aVSnr8K0
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments