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

Exceptional Descriptions

by in CodeSOD on

"The Colonial" was trawling through some code they inherited, and found this approach to doing exceptions in C#:

public enum ReturnCode : int
{
      Success                                         = 0,

        Enum1                     = 100,
  Enum2          = 110,

   // *snip* - LOTS of enums
     // .
    // .
   
    UnknownError               = 998,
      Exception                  = 999
};

Trimming Up Your Language

by in CodeSOD on

As a native English speaker, I've inherited a very chaotic perspective on language: "correct" language is defined by usage, loan words are less "loaned" and more like the mandolin someone lent me 20 years ago- mine now. New words can be ginned up on the fly, and parts of speech are just a suggestion.

Many other languages don't take this approach. French, for example, is defined by the Académie Française. There is a standard, and officially correct way to use French. In programming terms, we could say that French is C, while English is Perl.


Classic WTF: Wordy Invoice

by in Feature Articles on
It's a holiday weekend in the US, which means we dip back into the archives for a classic story. This one remembers the good old days, of greenbar paper and programs that can't handle large numbers because they don't have the memory for it. Original -- Remy

The daisy wheel stabbing at green-lined sheets could have been Satan’s fanfare, but Andy was long accustomed to tuning out ambient printer noise. It was 1982, and he spent most of his time before his Commodore PET 4032, churning out useful things in 6502 Assembly. Most of the code was for printing invoices, much like customer invoice currently printing and making all of that racket.

A sudden cloud formed over his desk. Once Andy clued in to the shadow overhead, he glanced up to find the new regional sales manager, Rick, accordion-folded printout in hand.


Not Really an Error'd Error'd

by in Error'd on

Scraping the bottom of the barrel this week, we accepted a couple of submissions that aren't, by any means, Errors. But they're undoubtedly amusing to the likes of those who haunt these pages, and as such, bon appetit.

Diner Dave A. wondered "I wasn't sure if you would really accept this as an Error'd, because it only *looks* like an error, which is why it caught my eye, but it isn't! So, on with the snark: You'd think there'd be Null chance that someone would name a restaurant like this, right? But NO! (Or as YAML would say, Norway!) This really exists -- I haven't been there yet but at the very least its website isn't Null. Maybe two Nulls are like a double negative, making it positively exist?" It doesn't look very filling.


A Big Ol' Log

by in CodeSOD on

We've already picked on bad logging code this week, but we haven't picked on PHP in awhile, so let's look at some bad PHP logging code, from Kris.

        $path = self::getPath();

        if (file_exists($path)) {
            $content = file_get_contents($path);
        } else {
            $content = "";
        }
        $content .= "\n" . date('Y-m-d H:i') . " | " . $message;
        file_put_contents($path, $content);

Archives