News for hackers. Stuff that matters.
Re:Never use a dependency that you could replace with an aft... by xamuel on Tuesday August 11, 2020 @03:58PM (#24125241)
Well, one special edge-case would be where you only need to parse some extremely tiny subset of JSON (for example: you only need to parse dictionaries whose keys and values are positive integers, like {1:2,3:4}). Then, depending how expensive the full json parser is, it might be worth your while just writing the limited parser yourself.

Of course, you might say, inevitably feature-creep will expand the list of things your parser needs to parse, but that's not a law of physics. Sometimes in certain limited, well-defined projects, it really is true that YAGNI.

Re:Never use a dependency that you could replace with an aft... by Izkata on Tuesday August 11, 2020 @04:05PM (#24125349)
Your example is more apt than intended: That's not valid json, which only allows string keys. If you use a library it'll either barf now or later when they fix it, so if you're forced to work with an API like that and can't change it, a custom parser is really the only way to go.
Re:Never use a dependency that you could replace with an aft... by JackFr on Tuesday August 11, 2020 @04:49PM (#24125809)
Well the good libraries have option flags that will allow you to handle JSON as found in the wild.
Re:Never use a dependency that you could replace with an aft... by majewsky on Tuesday August 11, 2020 @06:13PM (#24126641)
That's not JSON, though. It's absolutely something else. Maybe a JS snippet. Maybe YAML. Definitely not JSON, though.

(Some JSON libraries do have option flags, but usually it's about whether, during deserializing into a known type, unknown fields are an error or silently ignored. Or whether C-style comments are an error or considered as whitespace.)

Re:Never use a dependency that you could replace with an aft... by JackFr on Tuesday August 11, 2020 @07:43PM (#24127233)
It may not be JSON but it’s out there in the wild. A lot.

Let’s say I’m scraping a website. I can:

1) complain to the owner that “it’s not JSON”.

2) write a parser for a syntax that has no spec, (it’s not JSON, but it sure looks an awful lot like JSON with unquoted keys.)

or

3) Set ALLOW_UNQUOTED_FIELD_NAMES to true in the Jackson library.

Re:Never use a dependency that you could replace with an aft... by Izkata on Tuesday August 11, 2020 @11:38PM (#24128388)
> Maybe a JS snippet.

While acceptable, also misleading: JS only does string keys, but unlike JSON it'll convert whatever it's given into strings. Not a problem most of the time, since it'll do the same conversion for both accessing and setting, but good to be aware of if you're doing something like iterating Object.keys()

Re:Never use a dependency that you could replace with an aft... by jimmynest on Wednesday August 12, 2020 @12:14PM (#24133530)
I use https://jsonformatter.org/json-parser sometime to parse json and to validate.
Re:Never use a dependency that you could replace with an aft... by hinkley on Tuesday August 11, 2020 @05:37PM (#24126299)
You can also apply YAGNI to 'do we need our own custom parser'?

You don't know what your requirements are. The customers haven't told you yet.

If you pick a library with a straightforward interface, especially one that isn't too opinionated, you can always drop in a custom implementation later on. Frameworks, not so much (but that cuts both ways; the people who will write libraries often love writing frameworks too)

Re:Never use a dependency that you could replace with an aft... by aliceryhl on Tuesday August 11, 2020 @04:03PM (#24125309)
There are fully correct JSON parsers you aren't going to beat, even if you implement a subset. [1]

[1]: https://branchfree.org/2019/02/25/paper-parsing-gigabytes-of...

Re:Never use a dependency that you could replace with an aft... by Ma8ee on Tuesday August 11, 2020 @04:51PM (#24125825)
No, I won't beat them. But if it a limited subset that I can implement with twenty lines straightforward code, that will often be cheaper.

I've been on projects where they imported xml-parsers many times bigger than the rest of the whole codebase just to send a well formatted order number.

Re:Never use a dependency that you could replace with an aft... by benibela on Wednesday August 12, 2020 @05:12PM (#24137053)
xml and json are different beasts

A json parser can probably be implemented in an afternoon. But a conformant xml parser can take months.

There are some weird things in xml, for example, this is correct xml:

    <?xml version="1.0"?><!DOCTYPE abc[<?abc >]]<abc><abc/>]>>?>]><?x?><a/><?x <(x)>>?>
And for external entities, you need an http client in the xml parser, although it is probably better to not support that part of the standard.
Re:Never use a dependency that you could replace with an aft... by Ma8ee on Friday August 14, 2020 @04:45AM (#24155455)
And we didn’t need a conformant xml parser, which I know is huge and complex.
Re:Never use a dependency that you could replace with an aft... by dogma1138 on Tuesday August 11, 2020 @05:01PM (#24125924)
And what happens when your parsing needs to be expanded?
Re:Never use a dependency that you could replace with an aft... by Ma8ee on Tuesday August 11, 2020 @05:20PM (#24126115)
Then you reevaluate the situation. YAGNI is true here too.
Re:Never use a dependency that you could replace with an aft... by dogma1138 on Tuesday August 11, 2020 @06:49PM (#24126907)
YANGI is nice but when the PM asks you why it would take two months to accept a new JSON format from a client and you’ll answer well because we didn’t want to use an industry standard fully functional and vetted JSON parser so we essentially wrote our own edge case parser we both know how that conversation will end.

And YANGI doesn’t have anything against dependencies.

Re:Never use a dependency that you could replace with an aft... by Ma8ee on Tuesday August 11, 2020 @07:30PM (#24127151)
I said, then you reevaluate the situation.

When there are new requirements you do a quick estimate if you should add four new lines to the existing 20 or if it is worth to switch to an external library. 4 new lines to the 20, just add them to the core. But if this is regularly occurring that you have to add things, or requirements affecting this particular little parser that was supposed to be simple and static isn’t, then you should probably change your decision and use the library.

But you do that only then. Because chances are that with your approach you are going to drag along a large generic library that you only use a tiny fraction of. And that also has costs. In particular if your immediate impulse always is to add another library instead of writing things yourself.

Re:Never use a dependency that you could replace with an aft... by mro_name on Wednesday August 12, 2020 @02:44AM (#24129200)
That's IMHO key – solve problems once you know them, not earlier. Old idea, also core to XP.
Re:Never use a dependency that you could replace with an aft... by fernandotakai on Tuesday August 11, 2020 @05:12PM (#24126043)
taking in consideration how business work, in a few years you are going to have a full parser in your hands.
Re:Never use a dependency that you could replace with an aft... by dogma1138 on Tuesday August 11, 2020 @06:47PM (#24126890)
With all the technical debt associated with it, which is the problem basing your project on a dependency that would allow you to easily scale and add features is a huge benefit.

This is like saying you should roll your own crypto because you only need to do a very limited sub set of crypto operations so why use something like NaCl or Tink.

Re:Never use a dependency that you could replace with an aft... by samus on Wednesday August 12, 2020 @10:21AM (#24132293)
Encryption is a terrible edge case. If you are forced to half-ass encryption, you should seriously question the project requirements. Bad encryption can be worse than none at all. Things won't end well if data security is treated as a detail.
Re:Never use a dependency that you could replace with an aft... by abiogenesis on Tuesday August 11, 2020 @07:29PM (#24127143)
What do you mean by cheaper?

By using a third party library you are writing twenty lines less code, so it's cheaper in that aspect.

There are probably libraries that are faster than your twenty lines of un-optimized code, so it's cheaper as far as computing resources are considered too.

The only time it could matter is when you ship the code to the client through the wire (such as a Javascript bundle).

Re:Never use a dependency that you could replace with an aft... by Ma8ee on Tuesday August 11, 2020 @07:40PM (#24127210)
It’s cheaper in the sense that it is faster to write and maintain those 20 lines of code. Because someone has to evaluate the library, understand it well enough to actually call it and then make sure it stays up to date. And often there are a few lines of code to translate your data into a form that the library requires etc.
Re:Never use a dependency that you could replace with an aft... by BigJono on Wednesday August 12, 2020 @02:33AM (#24129145)
Plus for every developer to come, one call to an external library usually also means 30 pages of documentation to trawl through if they ever want to change anything, 29.9 of which is completely irrelevant to whatever your narrow use case is.

That's the real cost. The size of the code means absolutely nothing.

Re:Never use a dependency that you could replace with an aft... by Retric on Tuesday August 11, 2020 @07:38PM (#24127201)
You can’t use a library with zero lines of code. On top of this library’s always have development overhead outside of the code you write. Ex: What version number should you use? Did the latest version break something? Did the old version break something on the latest compiler? Etc etc.
Re:Never use a dependency that you could replace with an aft... by flukus on Tuesday August 11, 2020 @06:47PM (#24126895)
And it's going to take more than an afternoon to evaluate these parsers. You have to look at the options, evaluate the API, evaluate if they're stable and supported, evaluate if they integrate well with you're project, evaluate any dependencies they might have, etc. Then you need a plan to manage these dependencies long term.

If you're needs can be solved adequately by strtok then that's a far simpler and more maintainable solution that can be knocked out in an afternoon.

Re:Never use a dependency that you could replace with an aft... by tnorthcutt on Tuesday August 11, 2020 @05:04PM (#24125951)
Re:Never use a dependency that you could replace with an aft... by TeMPOraL on Tuesday August 11, 2020 @05:06PM (#24125968)
I agree.

> Of course, you might say, inevitably feature-creep will expand the list of things your parser needs to parse

If you've done your parser correctly, you'll be able to replace its implementation with the new dependency, with little to no need for extra refactoring in the rest of the codebase.