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
Eggnogium
Jun 1, 2010

Never give an inch! Hnnnghhhhhh!
Just lol at the idea that treating < 0 size lists like 0 size lists is defensive programming.

Adbot
ADBOT LOVES YOU

Doom Mathematic
Sep 2, 2008

Opferwurst posted:

it's what happens when you use some ReSharper auto refactoring functionality. It inverts a ">0" statement, but isn't smart enough to know that arrays can't have a negative size.

I feel like there are quite a few seeming coding horrors born out of refactoring operations like this.

Linear Zoetrope
Nov 28, 2011

A hero must cook

AstuteCat posted:

Depending on language/implementation, In some cases calling a count() will traverse the list to get the count - if you're trying to see if an enum/list is non-empty, doing an equivalent of 'list != []' might be quicker.

Out of wonder, which languages? The only one I know of is C strings, I wasn't aware that some (modern) languages didn't just keep an associated count variable.

Soricidus
Oct 21, 2010
freedom-hating statist shill

Linear Zoetrope posted:

Out of wonder, which languages? The only one I know of is C strings, I wasn't aware that some (modern) languages didn't just keep an associated count variable.

Lists in languages like Haskell and ML have this property. It’s common in functional cases where there isn’t really any concept of a list as a distinct object that you could store a length field in.

If you allow collections in general rather than just lists then there are even some examples in the Java standard library - ConcurrentSkipListSet, for example.

GABA ghoul
Oct 29, 2011

Linear Zoetrope posted:

Out of wonder, which languages? The only one I know of is C strings, I wasn't aware that some (modern) languages didn't just keep an associated count variable.

LINQ extends everything enumerable with a Count() method. Mixing up list.Count and list.Count() can have huge performance implications in C#.

SirViver
Oct 22, 2008

Opferwurst posted:

LINQ extends everything enumerable with a Count() method. Mixing up list.Count and list.Count() can have huge performance implications in C#.
It's not actually that dramatic - or rather, only if you make a custom enumerable type that has a Count property but doesn't implement ICollection. LINQ's Count() method special-case checks for the source type being an ICollection and if so uses the Count property instead of actually enumerating the sequence to count. Sure, in a super tight loop the method call overhead might be significant, but the average didn't-know-better use of Count() isn't going to tank your performance.

What's actually bad is having an IEnumerable-only type and using if (sequence.Count() > 0) instead of if (sequence.Any()).

Linear Zoetrope
Nov 28, 2011

A hero must cook
Ah yeah, I was stuck on plain array-backed collections and didn't think about functional languages, recursive types, or odd cases like skip lists which are all kind of obvious cases. (Though in the case of things like Haskell can't the compiler sometimes compile-away the count in optimizations?)

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Linear Zoetrope posted:

Ah yeah, I was stuck on plain array-backed collections and didn't think about functional languages, recursive types, or odd cases like skip lists which are all kind of obvious cases. (Though in the case of things like Haskell can't the compiler sometimes compile-away the count in optimizations?)

In Haskell you wouldn't use length to check if a list was empty

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


If you compute the length of a list in Haskell you're probably doing something wrong.

Ranzear
Jul 25, 2013

DaTroof posted:

fencepost error

Thank you for this term. Every random integer implementation is a crapshoot on this.

redleader
Aug 18, 2005

Engage according to operational parameters

ultrafilter posted:

If you compute the length of a list in Haskell you're probably doing something wrong.

Gosh, imagine needing to know how many things you have in a collection.

Ola
Jul 19, 2004

redleader posted:

Gosh, imagine needing to know how many things you have in a collection.

That would be knowledge of state and such knowledge IS FORBIDDEN AND AN AFFRONT TO THE CURRY. The Curry will only accept an O(n), recursive, list traversing element counter.

VikingofRock
Aug 24, 2008




redleader posted:

Gosh, imagine needing to know how many things you have in a collection.

Haskell "lists" should be thought of and used as stacks, or else you probably want a different data structure. And for the most part you don't need to know how many elements are in a stack; you just need to know if there are any elements left. In the cases where you do care it would be pretty trivial to write a wrapper datatype that kept track of the size.

Also, I ran into this horror today:
code:

// set this field to true to disable showing in UI
bool is_shown_in_ui = false;

One Eye Open
Sep 19, 2006
Am I awake?

VikingofRock posted:

Also, I ran into this horror today:
code:
// set this field to true to disable showing in UI
bool is_shown_in_ui = false;

It took me a while to see this, as my brain refused to believe that someone would do this.

Falcorum
Oct 21, 2010
Does the entire situation with constexpr in C++ count as a coding horror? Tried to make a compile time string hasher, using it to store the hash of whatever string in a class, however unless it was called through constexpr variables, it would not get evaluated as constexpr in any useful scenario (these are constant, compile time strings, so it should work).

End result, I have to manually store the value into a constexpr variable before assigning it to the field in the class. Yes, I know C++20 adds consteval but it's still dumb.

Jazerus
May 24, 2011


Falcorum posted:

Does [...] C++ count as a coding horror?

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

code:
public class Butt {

  private _pocoData1;
  private _pocoData2;
  (etc...)

  public void Become(Butt newButt) {
    _pocoData1 = newButt._pocoData1
    _pocoData2 = newButt._pocoData2
    (etc...) 

Absurd Alhazred
Mar 27, 2010

by Athanatos

NihilCredo posted:

code:
public class Butt {

  private _pocoData1;
  private _pocoData2;
  (etc...)

  public void Become(Butt newButt) {
    _pocoData1 = newButt._pocoData1
    _pocoData2 = newButt._pocoData2
    (etc...) 


To debug the butt, you must become the butt.

Hammerite
Mar 9, 2007

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

NihilCredo posted:

code:
public class Butt {

  private _pocoData1;
  private _pocoData2;
  (etc...)

  public void Become(Butt newButt) {
    _pocoData1 = newButt._pocoData1
    _pocoData2 = newButt._pocoData2
    (etc...) 


What are we supposed to be looking for here

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

Hammerite posted:

What are we supposed to be looking for here

Context: it was used by a coder who didn't understand the concept of references, so instead of doing:

code:
thing.Butt = newbutt
buttList[index] = newButt
he wrote the above method and then would do:

code:
thing.Butt.Become(newButt)
buttList[index].Become(newButt)

Hammerite
Mar 9, 2007

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

NihilCredo posted:

Context: it was used by a coder who didn't understand the concept of references, so instead of doing:

code:
thing.Butt = newbutt
buttList[index] = newButt
he wrote the above method and then would do:

code:
thing.Butt.Become(newButt)
buttList[index].Become(newButt)

Oh I see. Yeah that's not ok, you need to at least have a basic understanding of the object reference model for the language you're working in, or you're going to write all kinds of bugs and un-idiomatic code.

e: "you" is used generically.

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.

Hammerite posted:

e: "you" is used generically.
Are you saying it doesn't apply to golang?

Jazerus
May 24, 2011


Sagacity posted:

Are you saying it doesn't apply to golang?

trying to write idiomatic go was your first mistake

iospace
Jan 19, 2038


Jazerus posted:

trying to write idiomatic go was your first mistake

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
:thejoke:

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
idiotmatic

putin is a cunt
Apr 5, 2007

BOY DO I SURE ENJOY TRASH. THERE'S NOTHING MORE I LOVE THAN TO SIT DOWN IN FRONT OF THE BIG SCREEN AND EAT A BIIIIG STEAMY BOWL OF SHIT. WARNER BROS CAN COME OVER TO MY HOUSE AND ASSFUCK MY MOM WHILE I WATCH AND I WOULD CERTIFY IT FRESH, NO QUESTION

Eggnogium posted:

Just lol at the idea that treating < 0 size lists like 0 size lists is defensive programming.

To be fair I don't think people were specifically referring to <=0 sized list, just the general notion that when checking for a non-positive integer <= 0 is a common defensive practice even when there's no expectation that N will be less than 0. People weren't trying to excuse this usage but just offering explanations as to the thinking that led to the mistake. That was my interpretation anyway.

Falcorum
Oct 21, 2010
Just use unsigned ints like the machine spirit intended.

SupSuper
Apr 8, 2009

At the Heart of the city is an Alien horror, so vile and so powerful that not even death can claim it.

Falcorum posted:

Just use unsigned ints like the machine spirit intended.
I too love hidden underflows.

iospace
Jan 19, 2038


Falcorum posted:

Just use unsigned ints like the machine spirit intended.

:hmmyes:

Absurd Alhazred
Mar 27, 2010

by Athanatos

Falcorum posted:

Just use unassigned ints like the machine spirit intended.

:kheldragar:

Soricidus
Oct 21, 2010
freedom-hating statist shill
Just use floating point array sizes and indices, embrace the webdev future

Plus it’s useful if you only want to add half an object to the array

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

Falcorum posted:

Just use unsigned ints like the machine spirit intended.

the only true path is to have your only numeric type be a double-precision 64-bit binary format IEEE 754 value. dividing by zero shall set this value to a type "Infinite" instead of throwing an exception, and obviously doing a for loop from 0 to infinite shall cause a hang, and performing any arithmetic operation with an undefined value shall silently convert said value to NaN, as god intended. Drawing something to canvas? Behold as you iterate through your data structure that is filled with NaN and the draw calls all work, as obviously the correct response to being told to draw with the coordinates being NaN is to just do nothing, rather than daring to throw an exception as that might make the V8 engine perform poorly.

Absurd Alhazred
Mar 27, 2010

by Athanatos

Bruegels Fuckbooks posted:

the only true path is to have your only numeric type be a double-precision 64-bit binary format IEEE 754 value. dividing by zero shall set this value to a type "Infinite" instead of throwing an exception, and obviously doing a for loop from 0 to infinite shall cause a hang, and performing any arithmetic operation with an undefined value shall silently convert said value to NaN, as god intended. Drawing something to canvas? Behold as you iterate through your data structure that is filled with NaN and the draw calls all work, as obviously the correct response to being told to draw with the coordinates being NaN is to just do nothing, rather than daring to throw an exception as that might make the V8 engine perform poorly.

Ah, the OpenGL school of error handling.

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

Soricidus posted:

Just use floating point array sizes and indices, embrace the webdev future

Plus it’s useful if you only want to add half an object to the array

Also if you want an array with a quintillion elements (though, not if you want to address all of them)

Hammerite
Mar 9, 2007

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

Soricidus posted:

Just use floating point array sizes and indices, embrace the webdev future

Plus it’s useful if you only want to add half an object to the array

the webdev future is webassembly (I say, hoping and praying)

Dirt Road Junglist
Oct 8, 2010

We will be cruel
And through our cruelty
They will know who we are

Absurd Alhazred posted:

Ah, the OpenGL school of error handling.

I was gonna say Pascal with range checking disabled :v:

Absurd Alhazred
Mar 27, 2010

by Athanatos
https://twitter.com/QuinnyPig/status/1128056939470917632

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

Falcorum posted:

Just use unsigned ints like the machine spirit intended.

But the compiler generates better code with signed ints. Because it is allowed to assume that signed overflows don't happen. I still use unsigned for sizes of things, though.

Adbot
ADBOT LOVES YOU

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

Zopotantor posted:

But the compiler generates better code with signed ints. Because it is allowed to assume that signed overflows don't happen. I still use unsigned for sizes of things, though.

I live in the blessed land of GC and exceptions. I thought that unsigned overflow was simply the codification of what would happen by default anyway if unsigned overflow were left as UB. Are there any architectures that need to specifically handle them because they would do weird things otherwise and not guarantee 255 + 2 = 1?

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