Remy Porter

Remy is a veteran developer who writes software for space probes.

He's often on stage, doing improv comedy, but insists that he isn't doing comedy- it's deadly serious. You're laughing at him, not with him. That, by the way, is usually true- you're laughing at him, not with him.

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

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.


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);

Truly Strung Out

by in CodeSOD on

Strings in C remain hard. They're complicated- they're complicated because of null termination, they're complicated because of memory management, they're complicated because of the behaviors of const. There's a lot going on in C strings.

But Alice had a simple case: convert a true or false value into the string "true" or "false". Let's see how her co-worker solved this:


Reopening the Log

by in CodeSOD on

The list of things never to write yourself contains some obvious stars: don't write your own date handling, your own encryption, your own authentication/authorization. It's not that no one should write these things, it's that these domains all are very complicated, require a great deal of specialized knowledge, and while easy to get almost correct, they're nearly impossible to get actually correct. If you embark on these tasks, it's because you want to create a product that solves these problems, hopefully not because you've got a terminal case of NIH syndrome.

While it's not as big a star on the list, another domain you probably shouldn't try and solve yourself is logging. Today's anonymous submission isn't the worst home-grown logging I can imagine, but it's got enough bad choices in it to merit some analysis.


An Operating Query

by in CodeSOD on

Sami inherited some C# LINQ code. The actual behavior and purpose of this code is fairly simple. The way the code was written, however, well…

foreach (var operatingMode in ahu.CalculationData.OperatingModes) { operatingModesModel.OperatingModeNames.Add
(operatingModeNumber, operatingMode.OperatingModeName); var 
innerOperatingModeNumber = operatingModeNumber; foreach (var 
property in from partData in operatingMode.PartDatas.Where(p 
=> p.PartGuid == partGuid) let finalOperatingModeNumber = 
innerOperatingModeNumber from property in (from resultProperty 
in this.GetProperties(partData).Where(p => 
FilterAcceptNonSoundAndNonImageProperties(p, updateResult.For
(partData)) && (propertyFilterFunction?.Invoke(partData, p) ?? 
true)).ToList() let measurementUnit = resultProperty.Type.
GetPresentationMeasurementUnit(measurementUnits) let 
measurementUnitTranslationId = measurementUnit?.TextId select 
new OperatingModesModel.OperatingModePropertyModel
(finalOperatingModeNumber, this.TranslationService.
GetTranslator(this.Language.Code).Translate(resultProperty.
Type.NameId), this.PrintoutUtil.GetValueString(resultProperty, 
measurementUnit, this.Language), string.IsNullOrEmpty
(measurementUnitTranslationId) ? "-" : this.TranslationService.
GetTranslator(this.Language.Code).Translate
(measurementUnitTranslationId), resultProperty.Key)) select 
property) { operatingModesModel.OperatingModeProperties.Add
(property); } operatingModeNumber++; }

Archives