Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Hiowf
Jun 28, 2013

We don't do .DOC in my cave.

Vanadium posted:

This "*_ptr is haaaard, man" stuff is why most C++ people need to use Rust instead :v:

I'm sure a place that bans *_ptr will think highly of it. (Nobody here said they were hard)

Adbot
ADBOT LOVES YOU

ErIog
Jul 11, 2001

:nsacloud:

Scaramouche posted:

Not quite the horror of C pointers (which is kind of a horror in itself) but I've been doing some client work that involves importing Purchase Orders from a large online marketplace. Tucked away in the documentation is:


Sure, no big. Why would you even want unique PO numbers anyway?

Is there any assurance that PO numbers are unique even within the same day if they can be duplicated between days?

Polio Vax Scene
Apr 5, 2009



I'm hoping it cycles through a number sequence then goes back to 0 when it hits an upper limit, one that would not normally be reached within a day, or week or year.

But since this is the coding horrors thread, I'll assume that each PO is just given a random number.

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Well the POs are 7 digits, the leading number can be 0, and they go up incrementally, so that's a number space of 9 or 10 million I guess. It's wrapped around once in a 9 month period. Just seems weird; how hard is it to guarantee uniqueness? Amazon somehow does it across several countries with a much larger number of orders, albeit with way harder to remember numbers (3-7-7 or XXX-YYYYYYY-ZZZZZZZ). Just introduce 1-3 character fields to the 7 digits and you're probably gold.

Mr. Crow
May 22, 2008

Snap City mayor for life
This is an IValueConverter called NaNToStringEmptyConverter.

code:
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool convertHeading = (!string.IsNullOrWhiteSpace(parameter as string) && (parameter as string) == "ConvertZeroHeading")
                ? true
                : false;
            string formattedStr = string.Empty;

            return double.IsNaN((double)value) ?
                ""
                : !convertHeading ?
                    value
                    : (formattedStr = string.Format("{0:000}", Math.Round((double)value) % 360)) == "000" ?
                        "360"
                        : formattedStr;
        }
Whoever can guess what it's doing in under 10 minutes get's an upgrade :shepface:

not

shrughes
Oct 11, 2008

(call/cc call/cc)
Returns the heading to render but if parameter is "ConvertZeroHeading" then it returns a string and returns "360" instead of "000" if value is less than 0.5 because that's how headings are rendered.

Corla Plankun
May 8, 2007

improve the lives of everyone
If I worked with someone who wrote nested, ternary ifs I wold be fired almost immediately for doing a completely inappropriate amount of yelling.

shrughes
Oct 11, 2008

(call/cc call/cc)
That sort of yelling is going to be the reason I get fired.

Sedro
Dec 31, 2008

Mr. Crow posted:

code:
bool convertHeading = (!string.IsNullOrWhiteSpace(parameter as string) && (parameter as string) == "ConvertZeroHeading")
                ? true
                : false;
Oh, so that's how booleans work

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Sedro posted:

Oh, so that's how booleans work

I always slowly shake my head when I see that.

raminasi
Jan 25, 2005

a last drink with no ice
That looks like whoever wrote it was high on some bad Scheme at the time.

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

Corla Plankun posted:

If I worked with someone who wrote nested, ternary ifs I wold be fired almost immediately for doing a completely inappropriate amount of yelling.

I really like chaining ternaries if they're formatted properly.

code:
var someVariable = thing1IsTrue     ? "Thing 1"
                 : thing2IsTrue     ? "Thing 2"
                 : thing3IsNotFalse ? "Thing 3"
                                    : "None of these things";

Dr. Stab
Sep 12, 2010
👨🏻‍⚕️🩺🔪🙀😱🙀

Sedro posted:

Oh, so that's how booleans work

Don't get caught writing code like this. Just use my handy function:

code:
public bool IsTrue (bool expression){
    bool ret = true;
    if (expression ? true : false){
        ret = true;
    }
    else{
        ret = false;
    }
    return ret == true;
}

Dr. Stab fucked around with this message at 04:46 on Aug 6, 2014

Sedro
Dec 31, 2008

Dr. Stab posted:

Don't get caught writing code like this. Just use my handy function:

code:
public bool IsTrue (bool expression){
    bool ret = true;
    if (expression ? true : false){
        ret = true;
    }
    else{
        ret = false;
    }
    return ret == true;
}
You'd better write true == ret or there's no way that passes code review

Hiowf
Jun 28, 2013

We don't do .DOC in my cave.
JavaScript is easymode for this thread. But every now and then, you find something "extra" special.

date = new Date(2013,08,12);

This is:

a) Illegal, because the 0 prefix specifies octal, and 8 isn't an octal digit.
b) Really means 2013-09-12, because unlike days, JS months are 0-indexed.
c) Used in so many sites that browsers have to keep supporting it.

Of course, you can argue that JS itself is the horror for the weird indexing, but why oh why would you write dates as "2013-01-01" when *it actually means something different anyway*.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Skuto posted:

JavaScript is easymode for this thread. But every now and then, you find something "extra" special.

date = new Date(2013,08,12);

This is:

a) Illegal, because the 0 prefix specifies octal, and 8 isn't an octal digit.
b) Really means 2013-09-12, because unlike days, JS months are 0-indexed.
c) Used in so many sites that browsers have to keep supporting it.

Of course, you can argue that JS itself is the horror for the weird indexing, but why oh why would you write dates as "2013-01-01" when *it actually means something different anyway*.

The early edict to ape Java's DateTime APIs are going to be painful unto the end of days. Even at 20 I knew better than to accept the project from my kindly manager to fix the Netscape implementation. (The handy rule is "if you would use it as an index into an array of strings to display, it's zero-based"]: month, day-of-week, etc.)

Octal went away in ES5 though, no?

Hiowf
Jun 28, 2013

We don't do .DOC in my cave.

Subjunctive posted:

Octal went away in ES5 though, no?

Only in strict mode. In ES6 it's back with different prefixes, one of which I'm going to mention because it's appropriate here: 0O. (Admittedly I can't think of a case in which that would cause bugs, no matter how silly it looks)

Maybe you're thinking of parseInt enforcing base10 in ES5?

The problem is the ES6 spec claims the code in the previous post is illegal. On the internet, this means the spec is wrong, because for sure nobody is going to fix their website. What makes it so grating is that the uses are mostly the date example given, so they're are not just wrong, they're downright stupid, misguided, and in some cases likely subtle bugs of their own.

As I said, JavaScript is an easy punching bag, but this one is another level of "extra-special".

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
Usually its a good idea to use a date time library in any language, but that's especially the case in JavaScript. new Date() is just an unintuitive, verbose mess.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Yeah, I was thinking of parseInt. The 08 extension is still legal under the extensibility section, but I wonder if we'll see octal actually drop. Breaking changes aren't impossible, and ES6 is a descendent of ES5-strict so there will be some already.

PIGEOTO
Sep 11, 2007

Ithaqua posted:

I always slowly shake my head when I see that.

We have 27 pages of search results on our code indexer for the search term "? true : false;".

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

PIGEOTO posted:

We have 27 pages of search results on our code indexer for the search term "? true : false;".

At least you can fix it easily.

Mr. Crow
May 22, 2008

Snap City mayor for life

GrumpyDoctor posted:

That looks like whoever wrote it was high on some bad Scheme at the time.

The best / worst part is that it was originally a reasonable class (oddly specific but whatever), doing a simple Double.IsNaN(value) ? String.Empty : value; and then the rest of the abomination was added within the last 3 months.







We have mandatory code reviews and no QC. That thing passed two code reviews :negative:

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

PIGEOTO posted:

We have 27 pages of search results on our code indexer for the search term "? true : false;".

It is possible, depending on language, that some boolean-ish value needs to be coerced to an actual boolean. For example, in Objective-C (and this anecdote belongs in this thread for one reason or another), the boolean type used to be a typedef'd char, with the values YES and NO defined as 1 and 0. So you could return something like 17 as a boolean, and since it's just C it works when you do if (myBoolValuedSeventeen), but as soon as some joker tried if (myBoolValuedSeventeen == YES) they hosed themselves. The competent library writer would thus make sure they returned YES or NO.

Obviously there are superior ways to coerce a boolean than abusing the ternary operator, but a blind find/replace might not do wonders for your bug count.

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer

pokeyman posted:

For example, in Objective-C (and this anecdote belongs in this thread for one reason or another), the boolean type used to be a typedef'd char, with the values YES and NO defined as 1 and 0. So you could return something like 17 as a boolean, and since it's just C it works when you do if (myBoolValuedSeventeen), but as soon as some joker tried if (myBoolValuedSeventeen == YES) they hosed themselves.

UGGHHHHHHHHH

hobbesmaster
Jan 28, 2008

C booleans led to such crazy things.

b0lt
Apr 29, 2005

pokeyman posted:

It is possible, depending on language, that some boolean-ish value needs to be coerced to an actual boolean. For example, in Objective-C (and this anecdote belongs in this thread for one reason or another), the boolean type used to be a typedef'd char, with the values YES and NO defined as 1 and 0. So you could return something like 17 as a boolean, and since it's just C it works when you do if (myBoolValuedSeventeen), but as soon as some joker tried if (myBoolValuedSeventeen == YES) they hosed themselves. The competent library writer would thus make sure they returned YES or NO.

Obviously there are superior ways to coerce a boolean than abusing the ternary operator, but a blind find/replace might not do wonders for your bug count.

"used to be"

(also, it's a signed char)

hobbesmaster posted:

C booleans led to such crazy things.

!!x is the best idiom ever :colbert:

Soricidus
Oct 21, 2010
freedom-hating statist shill
Who writes "x == true" anyway, when in every widely used language that means exactly the same as just "x"?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

b0lt posted:

"used to be"
It's finally a typedef for bool instead for arm64.

I don't really get why people even use BOOL in any new objective-c code written in the last decade or so.

hobbesmaster
Jan 28, 2008

Soricidus posted:

Who writes "x == true" anyway, when in every widely used language that means exactly the same as just "x"?

It parallels "false == x"

raminasi
Jan 25, 2005

a last drink with no ice

Soricidus posted:

Who writes "x == true" anyway, when in every widely used language that means exactly the same as just "x"?

Someone hasn't spent enough time around stupid C++ operator overloading.

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

Soricidus posted:

Who writes "x == true" anyway, when in every widely used language that means exactly the same as just "x"?
Except it doesn't in Python, C, Ruby, Javascript, etc.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Bonfire Lit posted:

Except it doesn't in Python, C, Ruby, Javascript, etc.

Hmm? == is type-converting in JS, and IIRC is defined as the same ToBoolean operation used in if.

What's a case where C would let it compile but give a different answer?

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

Subjunctive posted:

Hmm? == is type-converting in JS, and IIRC is defined as the same ToBoolean operation used in if.

What's a case where C would let it compile but give a different answer?
code:
#include <stdbool.h>
int main()
{
    int x = 17;
    if (x == true) printf("x == true\n");
    if (x) printf("x\n");
}

/* prints "x" */

Hiowf
Jun 28, 2013

We don't do .DOC in my cave.
That's an argument *against* writing == true, though.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

b0lt posted:

(also, it's a signed char)

It was once just char, and unless you set a compiler flag it ends up the same thing, so... good point I guess?

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

Skuto posted:

That's an argument *against* writing == true, though.
I can't think of a good reason to write == true, but it's not the same thing v:shobon:v

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
I wrote if var == true { ... } in Swift recently for checking an optional bool, but then concluded that looked dumb and replaced it with if var ?? false { ... }.

Soricidus
Oct 21, 2010
freedom-hating statist shill

Bonfire Lit posted:

Except it doesn't in Python, C, Ruby, Javascript, etc.
Sorry, I shouldn't have made the mistake of being even slightly ambiguous on the internet. I nearly edited my post as soon as I posted it, but I dared hope that for once people would manage to find the obvious sensible interpretation of my words.

Allow me to rephrase it, then, for the benefit of literal-minded pedants:

Who, ignoring newcomers unfamiliar with standard idioms, discounting the possibility that bad coding standards might mandate the use of unidiomatic code, and ignoring a handful of unusual edge cases when one might actually care about the distinction between "true" and other values which evaluate to true in a boolean context, writes "x == true" when in the context of a conditional expression, i.e. the only situation in which the vast majority of programmers will ever check whether a value is "true" or "false", it means exactly the same thing expresses exactly the same intent as just "x"?

Soricidus fucked around with this message at 20:32 on Aug 6, 2014

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Plorkyeran posted:

I wrote if var == true { ... } in Swift recently for checking an optional bool, but then concluded that looked dumb and replaced it with if var ?? false { ... }.

Ooh, this is a good idea. I'm going to use it if I find myself in this situation in the C# code I have to write.

Adbot
ADBOT LOVES YOU

canis minor
May 4, 2011

Subjunctive posted:

Hmm? == is type-converting in JS, and IIRC is defined as the same ToBoolean operation used in if.

What's a case where C would let it compile but give a different answer?

If anybody's interested, in JS, for example:

code:
if ({}) { console.log('hi'); }
outputs hi.

code:
if ({} == true) { console.log('hi'); }
doesn't

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply