A Percise Parser

by in CodeSOD on

Thomas worked for a company based in Germany which was looking to expand internationally. Once they started servicing other locales, things started to break. It didn't take long to track the problem down to a very "percise" numeric parser.

handleInput( value ){
   let value_ = value;
   if( value.substring( 0, 1 ) === '+' ){
      value_ = value.substring( 1 );
   }

   value_ = value_.split( '.' ).join( '' );

   if( this.usePercisionIfPercentage && value_.indexOf( ',' ) >= 0 ) {
      const parsedPreValue = value_.split( ',' )[ 0 ];
      const parsedCommaValue = parseInt( value_.split( ',' )[ 1 ], 10 ) < 10 ?
         parseInt( value_.split( ',' )[ 1 ], 10 ) * 10 : value_.split( ',' )[ 1 ].substring( 0, 2 );

      if( parsedCommaValue === 0 ) {
         value_ = parseInt( parsedPreValue, 10 );
      }
      else {
         const parsedValue = parseInt( parsedPreValue + parsedCommaValue, 10 );
         value_ = parseInt( parsedValue, 10 ) / 100;
      }
   }
   
   // do stuff with value_
}

Wages of Inheritance

by in CodeSOD on

Tim H writes:

Some say that OOP was the greatest mistake of all. I say they weren't trying hard enough.


Spacetime Anomalies

by in Error'd on

Do we need better verb tenses to describe a counterfactual present from the future perspective? Any trained linguists in the audience, please helped out.

Reinier B. will wonder "Does this mean my cloud storage plan never expires? Or does it expire every day at noon? It's an obvious phishing mail though."


Honorable Conjunctions

by in Representative Line on

Doreann has touched this particular function many, many times. In all those times, she never noticed this particular little line, dropped in by a third-party contractor that has long since cashed their check and wandered off to other things.

(user?.betaMode || !user?.betaMode) && (specialRuleCode())

A Field Terned Visible

by in CodeSOD on

Today's anonymous submitter sends us some C# code. This particular block of code controls whether two different columns are visible on the screen. If the field Dist_Por equals one set of constants, we display one column, if it equals a different constant, we display the other. Seems simple enough.

My question to you is this: how many nested ternaries do you need to solve this problem?


We Get Spam

by in Feature Articles on

I recognize that our comments system here leaves much to be desired, especially with regards to the spam filter. Lots of good comments get moderated, an annoying quantity of spam gets through. But today, I want to take a moment to talk about some of the spam we get. Because we get a lot. And since most of you never see it as most of it hits our moderation queue, I don't think you can appreciate how weird some of the spam we get is. Formatting preserved, but links- where they were present- stripped.

We'll start with one of my "favorites", the sycophant:


Threading the Needle in a Haystack of Files

by in CodeSOD on

Today we return to Jessica (previously), who still suffers under Windows Forms. But it's not all Windows Forms. There's also random CLI tools kicking around. CLI tools which should really be designed to run as a service,

Let's start with the Main method of this particular tool.


Some Southern Exposure

by in Error'd on

Never let it be said that we at TDWTF dish it out and can't take it.

Morgan immediately dished "I'm not sure what date my delivery will arrive but I will {PlanToBeAtHomeWhenItDoes}. "


Archives