Recent Articles

Jun 2023

Two Blinded Mice

by in Error'd on

On a hazy evening, bookended by a pair of mouses, we bring you classic examples of the genre.

The first Anon E. Mous with a better trap beat it straight to our door, crowing "I have to admit, 'Add a Catchy Header' is a pretty catchy header."


Base-36 Conversion

by in CodeSOD on

Johannes needed to convert some data from Base-36. A helpful peer offered this solution, which… works.

int getBase36(char c) {
        std::map<char, int> b36;
       
        b36.insert(std::pair<char, int>( '0', 0 );
        b36.insert(std::pair<char, int>( '1', 1 );
        b36.insert(std::pair<char, int>( '2', 2 );
        b36.insert(std::pair<char, int>( '3', 3 );
        b36.insert(std::pair<char, int>( '4', 4 );
        b36.insert(std::pair<char, int>( '5', 5 );
        b36.insert(std::pair<char, int>( '6', 6 );
        b36.insert(std::pair<char, int>( '7', 7 );
        b36.insert(std::pair<char, int>( '8', 8 );
        b36.insert(std::pair<char, int>( '9', 9 );
        b36.insert(std::pair<char, int>( 'a', 10 );
        b36.insert(std::pair<char, int>( 'b', 11 );
        b36.insert(std::pair<char, int>( 'c', 12 );
        b36.insert(std::pair<char, int>( 'd', 13 );
        b36.insert(std::pair<char, int>( 'e', 14 );
        b36.insert(std::pair<char, int>( 'f', 15 );
        b36.insert(std::pair<char, int>( 'g', 16 );
        b36.insert(std::pair<char, int>( 'h', 17 );
        b36.insert(std::pair<char, int>( 'i', 18 );
        b36.insert(std::pair<char, int>( 'j', 19 );
        b36.insert(std::pair<char, int>( 'k', 20 );
        b36.insert(std::pair<char, int>( 'l', 21 );
        b36.insert(std::pair<char, int>( 'm', 22 );
        b36.insert(std::pair<char, int>( 'n', 23 );
        b36.insert(std::pair<char, int>( 'o', 24 );
        b36.insert(std::pair<char, int>( 'p', 25 );
        b36.insert(std::pair<char, int>( 'q', 26 );
        b36.insert(std::pair<char, int>( 'r', 27 );
        b36.insert(std::pair<char, int>( 's', 28 );
        b36.insert(std::pair<char, int>( 't', 29 );
        b36.insert(std::pair<char, int>( 'u', 30 );
        b36.insert(std::pair<char, int>( 'v', 31 );
        b36.insert(std::pair<char, int>( 'w', 32 );
        b36.insert(std::pair<char, int>( 'x', 33 );
        b36.insert(std::pair<char, int>( 'y', 34 );
        b36.insert(std::pair<char, int>( 'z', 35 );
       
        return b36.find(c)->second;
}

Copy Serialization

by in CodeSOD on

Twenty years ago, Stefano Z was a lowly junior developer, working with a set of senior developers, who had rules. A lowly junior developer, for example, couldn't be trusted to do something risky and dangerous, like serialize data to a buffer. Not without a safe API to keep them from foot-gunning themselves.

The API interface went thus:


Enterprise Estimates

by in Feature Articles on

Mary's company makes an enterprise product. Like many enterprise products, it shipped as a large pile of features that could potentially solve every problem any business could ever have, along with a suite of APIs that allowed customers to patch in their own custom functionality for their business needs. Also like many enterprise products, those features were only turned on or off based on how much the customer paid.

The arithmetic of all of these factors summed up to a set of function calls in the form IsFeatureXAvailable, with an added twist: the business side of the company was constantly changing the rules. "What the market will bear," and all that, meant that the IsFeature class of functions were some of the most volatile in the codebase.


Weakly Miles Calculation

by in CodeSOD on

Emma found a function called get_mileage_per_year. The purpose of the function is to apply some business rules around travel expenses, and while I'm sure it does that… it also makes some choices.

def get_mileage_per_year(self):
    # Mileage not set yet
    if not self.mileage_per_day:
        return 0

    weekly_miles = self.mileage_per_day

    extra_miles = 60 - self.mileage_per_day if self.purposes == ["special"] else 25

    # Add in extra miles for 'special' purpose is selected

    weekly_miles += extra_miles if "special" in self.purposes else 0

    #   where average daily miles are given by the weekly miles variable
    annual_miles = (weekly_miles * 365) + 2000
    if "special" in self.purposes:
        annual_miles = max(annual_miles, 24000)

    # Return annual miles bound to range 4,000 < mileage < 50,000
    mileage = int(min(max(annual_miles, 4000), 50000))
    return mileage

Zeroics

by in Error'd on

This week we have submissions from regulars, and one or two who I did not recognise (zedless in honour of the source of the penultimate paragraph).

Nearly all of us use credit cards regularly, I'm sure, but only The Beast In Black has found one with such an unusual rewards scheme. Remarks friend Beast "If this is Wall Street Math(tm), it's no wonder that the US has a banking crisis." It's more likely to be North Dakota math.


The Delete Procedure

by in CodeSOD on

Daniel recently found this pair of stored procedures. While the code within them is simple, they hint at horrors just beyond the edge of the stage, the kinds of things that might drive one mad.

CREATE PROCEDURE [dbo].[sp_SomeProc]
@ID       VARCHAR(6)

AS

IF @ID = '109369'
        BEGIN
                DELETE FROM table1
                WHERE ID = '109369'
        END
IF @ID = '100976'
        BEGIN
                DELETE FROM table1
                WHERE ID = '100976'
        END
GO

CREATE PROCEDURE [dbo].[sp_SomeOtherProc]
@ID       VARCHAR(6)

AS

IF @ID = '109369'
        BEGIN
                DELETE FROM table2
                WHERE ID = '109369'
        END
IF @ID = '100976'
        BEGIN
                DELETE FROM table2
                WHERE ID = '100976'
        END
GO