worsethanfailure.com had a programming contest to build the worst, 4 function calculator possible. The 12 final entries are amazing to behold. I urge everyone who is a developer to read these and remember where you have seen similar designs in code.
01 The pièce de résistance, however, is the DoDivision function. It was not only also inspired by a WTF posted here, but also includes commented-out code (with a TODO to uncomment it before submitting) that would otherwise cause the calculator to fail the “divide by zero” test case. Fortunately, the configuration instructions told us to uncomment this before running…
02 FileSystemHashMapNotepadCalculator calculation engine works by loading Windows Calculator (how else could one guarantee a correct calculation?), copying the answer to the Clipboard, loading notepad, pasting it into notepad, saving a file to the folder with the appropriate Hash Key (C:\1.0000-ADD-2.0000 in this case), and then reading the resulting file to display the answer. It couldn’t be simpler!
03 the estimator client sends a message to one of the estimator server’s sixteen open ports (each on its own IP address) that corresponds with the button clicked. The message is queued on the server until an operation button is pressed (e.g. “=”), at which point a LISP expression is constructed, parsed, and then calculated. The server then sends the client an XML string with the answer by setting an X11 property. Phew!
04 the Addition function is void and does not return a value. It does, however, throw a value all the way back up to the UI tier. Now that’s efficiency!
05 When the WTF Web Calc is launched, it starts up a "web server" and then opens up a FireFox browser pointed to http://localhost:12345. When the user presses a calculator button, an AJAX call (XmlHttpRequest) is sent to the web server, which builds up an expression that gets submitted to Google when the "=" button is pressed. Dynamically generated Javascript is returned to the client and then executed to update the display.
06 That's right, someone just had to go and implement Optical Character Recognition for their calculator.
07 Charles decision to simplify things with fractions meant that he’d have to implement fractional arithmetic algorithms. And the main problem with this is that such algorithms are notoriously slow. Do you remember how much of a pain it was to figure out the sum of eighteen twenty-thirds and four sevenths?
Thankfully, C++ allows for inline assembly code, which everyone knows is monumentally faster. Risking a slow calculator, Charles had no choice but to use it:
08 All plug-ins to the Universal Calculator must introduce a set of Value Types that may be operated with any other Value Type. In other words, “6 + 3 / BLUE * {Picture of an Orange}” is a perfectly legitimate equation that returns a perfectly legitimate answer
09 Calculations are implemented as recursive functions that use XML (enterprisey!) to communicate with each other. Like any good application that has to spend a lot of time doing calculations, Terry’s Calculator uses a background thread to do the calculations. Unlike a good application, it ignores thread safety, leaks memory like a sieve, and requires a good 100MB+ of memory to run.
10 Instead of the FerronCalc’s state machine wrapping state objects, it uses multiple-inheritance to derive from every state it can exhibit.
11 At the core of VICE lies the VirtualTransistor. It is essentially a switch (modeled after an NPN transistor) that sends current to its output (“Emitter”) only if it receives current from both its input (“Collector”) and its switch input (“Base”). Of course, a transistor is useless without current flowing to and from, so the VirtualConnection was built: it is a virtual wire with a single Boolean indicating whether there’s current or no current flowing through.
But even with VirtualTransistors and VirtualConnections, something is still needed to simulate the “flow” of electricity from wire to transistor to wire, and so on. This is where the VirtualComponent abstract class comes in: it has a single method (Update()) that instructs the implementing class to read its inputs and update its outputs.
12 The ExtensibleCalc comes with several Calculation Engines. There’s the Test Case Engine (two actually, built only to work with our test cases), the Calculator Calculator Engine (outsources to calc.exe), the Chain engine (uses multiple Calculation Engines), and even a Skeleton Solution Engine (designed to run any of the other OMGWTF calculators that did not modify the UI). Our personal favorite, however, is the User Calculation Engine
No comments:
Post a Comment