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
netcat
Apr 29, 2008

yippee cahier posted:

Tell your boss you'll be in when it's light out and work 8 hours from whenever that is.

Not having to work during winter :pray:

Adbot
ADBOT LOVES YOU

xtal
Jan 9, 2011

by Fluffdaddy
vvv ok I wasn't sure if coding horror drama would be appreciated

xtal fucked around with this message at 00:07 on Apr 18, 2018

Ranzear
Jul 25, 2013

Looked it up a bit. Don't think that belongs here.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

UraniumAnchor posted:

I have uncovered hell, and it is timezones.

Is this you? Lol, talk about hell, jesus

https://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result

iospace
Jan 19, 2038


Timezones are the loving devil.

UraniumAnchor
May 21, 2006

Not a walrus.

No, thankfully the oldest information I need to deal with 99% of the time is maybe 4-5 years old at the most. Anything older than that is audit (in the general sense, not a tax audit) territory only, and even with that the oldest info in the database is from 2006.

That's a pretty wonderful 'bug', though.

CPColin
Sep 9, 2003

Big ol' smile.
Groovy being Groovy:

Java code:
public class NumberAwareComparator<T> implements Comparator<T> {
    public int compare(T o1, T o2) {
        try {
            return DefaultTypeTransformation.compareTo(o1, o2);
        } catch (ClassCastException cce) {
            /* ignore */
        } catch (GroovyRuntimeException gre) {
            /* ignore */
        }
        // since the object does not have a valid compareTo method
        // we compare using the hashcodes. null cases are handled by
        // DefaultTypeTransformation.compareTo
        int x1 = o1.hashCode();
        int x2 = o2.hashCode();
        if (x1 == x2) return 0;
        if (x1 < x2) return -1;
        return 1;
    }
}
This is the comparator that the Collection.sort() extension method uses. As you can see, they wanted to handle the case where the two objects don't even implement Comparable, so they resort to comparing hash codes. (Note that this fallback behavior is not called out in the documentation for Collection.sort().) No idea why they don't just do return o1.hashCode() - o2.hashCode(); like normal people, though!

Needless to say, this subtlety was not noticed by whoever wrote the code in my project that's loading some stuff from a database, calling sort(), and continuing on its merry way, leaving me to wonder why the order of the displayed data randomly flips on refresh!

Edit: Here's the documentation for sort(), by the way:

quote:

Sorts the Collection. Assumes that the collection items are comparable and uses their natural ordering to determine the resulting order. If the Collection is a List, it is sorted in place and returned. Otherwise, the elements are first placed into a new list which is then sorted and returned - leaving the original Collection unchanged.

It'll mutate the input! Maybe!

No Safe Word
Feb 26, 2005

iospace posted:

Timezones are the loving devil.

Worse than Daylight Savings? Time Zones at least somewhat make sense logically even though political divisions make them awkward. Daylight Savings is just a stupid archaic concept that needs to die and causes more problems than it solves (which is none).

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

I’m at a conference and a guy was just showing how to push some code up using git and just did the old ‘mv foo.bak foo’

JawnV6
Jul 4, 2004

So hot ...
code:
#define HACK_TO_REMOVE_DUPLICATE_ERROR

iospace
Jan 19, 2038


No Safe Word posted:

Worse than Daylight Savings? Time Zones at least somewhat make sense logically even though political divisions make them awkward. Daylight Savings is just a stupid archaic concept that needs to die and causes more problems than it solves (which is none).

When you're trying to make libraries from scratch from one, the proper advice, as demonstrated in this thread is simply this:

don't.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe
Put another way, "I'm going to roll my own date library" is only better advice than "I'm going to roll my own crypto library" because the only person you're really likely to hurt is yourself.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

CPColin posted:

Groovy being Groovy:

Java code:
public class NumberAwareComparator<T> implements Comparator<T> {
    public int compare(T o1, T o2) {
        try {
            return DefaultTypeTransformation.compareTo(o1, o2);
        } catch (ClassCastException cce) {
            /* ignore */
        } catch (GroovyRuntimeException gre) {
            /* ignore */
        }
        // since the object does not have a valid compareTo method
        // we compare using the hashcodes. null cases are handled by
        // DefaultTypeTransformation.compareTo
        int x1 = o1.hashCode();
        int x2 = o2.hashCode();
        if (x1 == x2) return 0;
        if (x1 < x2) return -1;
        return 1;
    }
}
This is the comparator that the Collection.sort() extension method uses. As you can see, they wanted to handle the case where the two objects don't even implement Comparable, so they resort to comparing hash codes. (Note that this fallback behavior is not called out in the documentation for Collection.sort().) No idea why they don't just do return o1.hashCode() - o2.hashCode(); like normal people, though!

Needless to say, this subtlety was not noticed by whoever wrote the code in my project that's loading some stuff from a database, calling sort(), and continuing on its merry way, leaving me to wonder why the order of the displayed data randomly flips on refresh!

Edit: Here's the documentation for sort(), by the way:


It'll mutate the input! Maybe!

that is wonderful

Soricidus
Oct 21, 2010
freedom-hating statist shill
I have finally found the worse thing than parsing xml with regex

It’s parsing c code with regex

Jeb Bush 2012
Apr 4, 2007

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.

Soricidus posted:

I have finally found the worse thing than parsing xml with regex

It’s parsing c code with regex

what on earth

I'm curious about what scenario could make that even kind of work

dis astranagant
Dec 14, 2006

Jeb Bush 2012 posted:

what on earth

I'm curious about what scenario could make that even kind of work

Regex engines haven't been limited to just regexes in decades and some of the more popular ones are pretty close to describing full on context-free languages. The rest comes town to tool, hammer, problem, nail, etc.

Jeb Bush 2012
Apr 4, 2007

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.

dis astranagant posted:

Regex engines haven't been limited to just regexes in decades and some of the more popular ones are pretty close to describing full on context-free languages. The rest comes town to tool, hammer, problem, nail, etc.

I know that but trying to build a C parser in one still seems insane

dis astranagant
Dec 14, 2006

It's illadvised but it's just C, not something terribly complicated. Babby's first compiler project back in school was to write the lexer and parser for subset of C. I'm sure with the right application of brute force and ignorance you could at least get the basics bludgeoned together with just PCRE or something.

dis astranagant fucked around with this message at 09:08 on Apr 25, 2018

Jeb Bush 2012
Apr 4, 2007

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.

dis astranagant posted:

It's illadvised but it's just C, not something terribly complicated.

even with regex extensions any CFL* is terribly complicated to parse

*C isn't even a CFL, but I guess we can assume they weren't handling preprocessor directives properly here

dis astranagant
Dec 14, 2006

Jeb Bush 2012 posted:

even with regex extensions any CFL* is terribly complicated to parse

*C isn't even a CFL, but I guess we can assume they weren't handling preprocessor directives properly here

I'm assuming they knocked together a barely Turing Complete subset before reality started to sink in. Either that or they're one of those crazy people who do things like implement Turing Machines in Powerpoint.

dis astranagant fucked around with this message at 09:14 on Apr 25, 2018

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Never look at the autocomplete engine for any editor ever, then. Or ctags.

Nth Doctor
Sep 7, 2010

Darkrai used Dream Eater!
It's super effective!


I'm a big fan of regexes (it's me, I'm the horror) and every once in a while someone asks me to do some parsing of arbitrary source code with them to strip out comments and such. It feels great to say they it can't be done that way, and that I wrote a book about why.

My undergraduate thesis was on the subject and I ended up bumbling into making a lexical analyzer before I ever took a compilers course.

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

Suspicious Dish posted:

Never look at the autocomplete engine for any editor ever, then. Or ctags.

Facts.

I maintain a suite of language tools that's mostly implemented with a proper parser, but still has a few legacy components that depend on regex and ad hoc crawls over raw strings. The legacy stuff is a train wreck.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



CPColin posted:

Groovy being Groovy:

Java code:
public class NumberAwareComparator<T> implements Comparator<T> {
    public int compare(T o1, T o2) {
        try {
            return DefaultTypeTransformation.compareTo(o1, o2);
        } catch (ClassCastException cce) {
            /* ignore */
        } catch (GroovyRuntimeException gre) {
            /* ignore */
        }
        // since the object does not have a valid compareTo method
        // we compare using the hashcodes. null cases are handled by
        // DefaultTypeTransformation.compareTo
        int x1 = o1.hashCode();
        int x2 = o2.hashCode();
        if (x1 == x2) return 0;
        if (x1 < x2) return -1;
        return 1;
    }
}
This is the comparator that the Collection.sort() extension method uses. As you can see, they wanted to handle the case where the two objects don't even implement Comparable, so they resort to comparing hash codes. (Note that this fallback behavior is not called out in the documentation for Collection.sort().) No idea why they don't just do return o1.hashCode() - o2.hashCode(); like normal people, though!

Needless to say, this subtlety was not noticed by whoever wrote the code in my project that's loading some stuff from a database, calling sort(), and continuing on its merry way, leaving me to wonder why the order of the displayed data randomly flips on refresh!

What about that makes it flip the order on refresh? Are the 'hash codes' just pointer addresses or something?

CPColin
Sep 9, 2003

Big ol' smile.

Munkeymon posted:

What about that makes it flip the order on refresh? Are the 'hash codes' just pointer addresses or something?

Yep. In Java, Object.hashCode() returns something similar to the address of the object if you don't override it and Groovy inherited that. In Java, though, if you try to sort objects that don't implement Comparable, you sensibly just get an exception.

Volguus
Mar 3, 2009

CPColin posted:

Yep. In Java, Object.hashCode() returns something similar to the address of the object if you don't override it and Groovy inherited that. In Java, though, if you try to sort objects that don't implement Comparable, you sensibly just get an exception.

It is the address of the object usually:

quote:

As much as is reasonably practical, the hashCode method defined by class {@code Object} does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)

CPColin
Sep 9, 2003

Big ol' smile.
Not any more, apparently:

https://stackoverflow.com/a/26975908

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Cool post, I was still under the impression that they were memory addresses :monocle:

Volguus
Mar 3, 2009

Janitor Prime posted:

Cool post, I was still under the impression that they were memory addresses :monocle:

Yeah, me as well, based on the quoted javadoc though. os::random() .... wouldn't have imagined.

CPColin
Sep 9, 2003

Big ol' smile.
It's hilarious that HotSpot has a command-line argument for which hash code algorithm you want. It's sad that somebody somewhere has probably had to use it.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

CPColin posted:

It's hilarious that HotSpot has a command-line argument for which hash code algorithm you want. It's sad that somebody somewhere has probably had to use it.

Yeah holy poo poo, what?

Also I love that "always const 1" is an option lol.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
HotSpot has used a generational (copying) collector for, like, 20 years. You cannot use the object address as a hash code with a copying collector.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

rjmccall posted:

HotSpot has used a generational (copying) collector for, like, 20 years. You cannot use the object address as a hash code with a copying collector.

Aha, that makes sense.

Could you avoid copying the objects if you instead maintained a list pointers to which objects were in which generation? Although it would add memory overhead and the only advantage would be constant memory addresses so who cares I guess.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
You can use whatever you like as long as you memoize it somewhere. It'd just end up being whatever the address was the first time someone asked for the hash code.

e: one of the big wins of generational garbage collection is that you can allocate memory by just bumping the pointer in your young generation. Then after a collection you end up with a pristine young generation so you can keep doing that. Going back to having to rummage around lists and deal with memory fragmentation would kind of defeat the point a little.

Jabor fucked around with this message at 01:22 on Apr 26, 2018

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Jabor posted:

You can use whatever you like as long as you memoize it somewhere. It'd just end up being whatever the address was the first time someone asked for the hash code.

That could potentially create collisions between live objects right?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Zaphod42 posted:

That could potentially create collisions between live objects right?

Technically yes, but it's not a problem if two objects have the same hash code.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

Jabor posted:

Technically yes, but it's not a problem if two objects have the same hash code.

...unless your stupid code assumes it does. But that's not the JVM's problem.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Also raw object addresses are terrible hash codes; you should always at least shift/rotate the alignment away.

rjmccall fucked around with this message at 02:10 on Apr 26, 2018

FrantzX
Jan 28, 2007
Shouldn't the default implementation of GetHashCode() or equivalent just return 0? You should either explicitly define a hashing method or not get one at all.

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Why? The default implementation is totally fine for any type that uses the default equality semantics, and being able to stick everything into a hash-based collection is really useful.

Really the only thing you need is a way to enforce also implementing hashCode when you write a type with custom equality.

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