5 ms·
I’ve never seen this approach. I imagine I’m just not working in a domain that touches on problems “solved” this way. Any popular examples to point to?
by Waterluvian 4y ago
I’ve never seen this approach. I imagine I’m just not working in a domain that touches on problems “solved” this way. Any popular examples to point to?
- cogman10 4y agoNope, just stories from my day job. One time, to solve the problem of "we want floating point numbers that are close to be considered equal" on an object, the programmer decided to take every field of that object (reflectively of course) turn that into a string, and then compare the two strings together. Another time, in order to convert a LocalDate to our own internal date representation, a dev decided to toString the local date, split on the `-`s to extract the day month year. (Yes, localdate has day, month, year methods). One time, to convert an int to a double a dev decided to turn that int into a string and then used "Double.parseDouble" to turn that int into a double (I wish I were joking). All these and more were found through the joys of profiling :D
- Waterluvian 4y agoI’m in pain reading this. But I feel the need to confess my own sin: I once didn’t want to write or add a dependency for a Python deep equality checker so I just sorted, JSONified, and string compared the two. It worked fine for years. It wasn’t performant important code. But it still keeps me up at night.
- cogman10 4y agoIt's stuff like this that makes me mad when I see "Optimization is the root of all evil" quotes thrown at me. The code I'm most typically fixing for performance isn't some complex algorithm that needs inline assembly and AVX register weaving... No, the kind of code I most often fix for performance is code where someone does `Double.parseDouble(foo.toString())` and turn it into `double bar = (double)foo;`. Or code where someone does n^2 when they could have had n by using a dictionary/hashmap. Or code where someone uses a dictionary/hashmap instead of using a simple POJO. It drives me bonkers when someone apparently goes out of their way to write code in the slowest form possible.
- Waterluvian 4y agoThere’s no black and white. I’ve known really good optimizers who do harm to the company and their peers by obsessing over gains that simply don’t matter. And I’ve known people who make programs unnecessarily slow and painful because of sloppiness. That’s not to say you can’t endeavour to “do it right the first time” but very regularly that is not a realistic goal. Good engineering requires understanding the big picture, particularly what matters and doesn’t matter to a business (and how those priorities change over time).
- acdha 4y agoYeah, I generally favor the approach of first making the code easy to understand/validate, and easy to replace later if you find a performance problem. I’ve found it’s usually only a small percentage of the time that your first guess for what’s going to be a bottleneck is accurate.
- girvo 4y agoEngineering has pretty much always been the art and science of "good enough". Anyone can make a bridge with infinite resources, but an engineer is who can make a bridge just good enough to not fall down within budget and spec. The challenge with software is the underlying "physics" of our world continue to get more powerful year over year (unlike real physics), so "good enough" ends up being sloppier and sloppier as time goes on, and for a lot of applications it doesn't matter. Of course, until that day where it turns out it does matter. Good developers I think can anticipate some of those cases, and great developers can pick the few that they can sink the time into getting Right (tm) (c) 2022, to get the most bang for their (time) bucks.
- horsawlarway 4y agoEh - It's good to know when you're taking the slow route, but most times the slow route is fine. It's easy to point to the title piece and assume the right advice to is try to optimize early - but the reality is that the gains mentioned here basically only matter because these libraries are used by millions of developers, and run often. essentially - don't try to optimize until you have a reason to make that effort worth it. For these libraries - it's worth it. For the times you're getting asked to fix performance, it's because performance has become a feature that matters enough to have someone ask you to fix it. It's fine to wait until that time to swing back around and pay attention to it. There are thousands of times where that never happens, because the performance was fine taking the scenic route through a string. Plus - that leaves you plenty of low hanging fruit to pick up some easy wins. --- I think the real story here is not that folks are taking the slow route, I think the story is that the low hanging fruit hasn't been knocked out of these libraries. And really, that goes to show that a lot of software really is supported by a very small number of library authors, who aren't doing it as their day job, for very little in the way of real compensation. Ex: SVGO is used by 9.1 million people (on github alone) but only has 163 contributors, and only 4 people have more than 10 commits. That means only one in 55,000 users has bothered to add anything. And there's only one real dev for every 2.25 million users. And "users" here also includes entire companies.
- deleted 4y ago[deleted]
- lucumo 4y ago> I once didn’t want to write or add a dependency for a Python deep equality checker so I just sorted, JSONified, and string compared the two. It worked fine for years. It wasn’t performant important code. I think this one is actually fine. You thought about the solution, considered your options and made a design decision based on the balance of your priorities. That's what engineering is: a bunch of trade-offs solidified into a product. The situation where someone uses Double.parseDouble(number.toString()) to translate an int into a double is different. There's really no reason to do it like that: it's harder to understand, uses more memory and cpu and is slower. There's no trade-off that makes that line come out as the best decision.
- specialist 4y agoYou did the right thing. Deep clone and equals on deeply nested, probably heterogenous, object graphs can be challenging. And certainly error prone.
- memco 4y agoThat’s a tough one because Python lacks a standard convention for deep comparisons so your only options are some bespoke function you (probably copy from some random website) stick in every library you use or pull in some third party dependency or use a standard module with a fairly straightforward serialization method that does what you need which I would guess is either pickle or json. Is there a better option?
- more-coffee 4y agoDepending on how much that equality check is deciding the application's performance, jsonifying to compare stuff is fairly ok. Just beware of any lists which you'll need to sort manually (on both sides) to prevent false negatives :)
- acdha 4y agoI’m having flashbacks to some contract Java developers who were calling toString() on deserialized JSON objects and then parsing the output (old school index/substring, not even regex) because they insisted it was too hard to use the built-in accessors. Bonus points for then trying more kludges when they got a reminder that JSON does not guarantee object key order so they couldn’t write code assuming values would be adjacent. They also did that exact same thing parsing dates, except that they flubbed the logic and IIRC had an off-by-one on the month for the constructed Date object.
- Waterluvian 4y agoLots of JSON libraries can be told to enforce key order. And you’ve gotta remember that if you’re going to do something so ghoulish!
- acdha 4y agoThese were definitely not the kind of developer who read the documentation looking for options like that. I've mostly avoided Java for years and still found myself going “this is like 20 lines of code [badly] re-implementing a built-in library function” on almost every code review.
- v-erne 4y agoCould this just be incompenence? I have seen this kind of code many years ago when external company was brought to sell us their implementation of CMS on some (even then) outdated Java/js-on-server-before-node-era platform. I was asked to do code review to chcek if their code can be integrated into our codebase. I was quite unexperienced at that point in time but enough so to notice that there was something off with the code. There was a lot js that looked almost like exercise in obfuscation. Things like doing join, append and split on array to do push - and it was not abstracted into function - this three operations were repeated all over the code. And then I met the programmer - it was ovious after few questions that he did not know what he was doing and his direct manager, who was also present, know nothing about programming (he was more od a business person as he himself proclaimed). It was as if this company hired the cheapes guy they could find, that on paper looked competent, give him task of implementing CMS, and only looked at results (the CMS actually worked), but no one ever checked the code that was hiding under (and the guy never worked with anyone else and never read any other code besides his!)
- _aavaa_ 4y agoI have, and it boggles the mind. You'd think Java was a string-oriented language.
- Dachande663 4y agoCommon example I’ve seen is checking if an IP is in a range. Most (JS) libs I’ve seen do something like regex or, more commonly, split on periods and then have a set of nested ifs. Versus if you have a proper IP type, you check if its long value is between ranges. (Not saying you can’t do this, but because so many times an IP is stored as its string representation, that seems to be the default.
- usrusr 4y agoAlso haven't seen it, but I could imagine it happening if someone desperately misses duck-typing due to too many similar classes not under their control (can't make them share interfaces) and they have a lot of routine with e.g. json from/to but none with reflection. (I do enjoy a hearty dose of tojson in my println debugging)
- rr888 4y agoAnother common one is converting booleans to "Y" and "N".
- Zardoz84 4y agoCan be worse. On the old codebase where I work, I found booleans as : S, s, si, Yes, yes, Si, 1 ... I managed to unified it to be 1 and 0, but sometimes I keep seeing and old "Si" on some place
- karmelapple 4y agoWe saw it with contractor-developed code. They took JSON from an API, then converted it to a big blob of text, and did some gnarly searching. It made no sense. But we were also in a rush to get this app built, so we didn’t push back on this tortured logic. But we were saddled with this technical debt for an embarrassingly long time.
- WorldMaker 4y agoThat sounds almost more excusable than the rush code I found the other day. They were storing something (to local storage and then to a DB) via JSON.stringify() but for whatever reason they hadn't thought after retrieving it back from the database to just JSON.parse() it and instead used a weird mash of string replacements, splits, and regexes to parse an ad hoc brittle subset of JSON.
- ErikCorry 4y agoPeople will convert a float to an integer by using parseInt. That function automatically converts its argument to a string, then parses it as an int, ignoring anything after the decimal point. Completely fails for a number big enough to be stringified to 1.34e20. Plot twist, if your number is in a reasonable range this is very fast because there was one a popularJS benchmark that did this.
- mcculley 4y agoI have had to do it where all of the member methods are private (or otherwise inaccessible to me) and toString() is the only useful way to get information out of the object.