Live data from Hacker News

Arguments against JSON-driven development

okigiveup.net

211–220 of 306 posts

Re: Arguments against JSON-driven development

#211
post #209

Earlier quoted context omitted.

This simply doesn't make sense. It violates the indiscernibility of identicals, which is one of the cornerstones of Western logic. I can accept different opinions on several matters (e.g., the extent to which using objects is a good idea), but throwing logic out of the window is just too much.

> It violates the indiscernibility of identicals, which is one of the cornerstones of Western logic. Oh, please. Python isn't violating any principles of Western logic. It's only violating your idiosyncratic insistence that there can only be one in-memory representation of any immutable value. Python does this for some types but not for others. Yet computers manage to run Python just fine, Western logic notwithstandi…

> It's only violating your idiosyncratic insistence that there can only be one in-memory representation of any immutable value.

I never said this! There can be multiple representations of the same value. For example, the same ordered set may be represented as two distinct red-black trees, balanced differently.

Values are different from their memory representations. Different memory representations of the same value are okay. Branching on the difference is not. Of course, to hide representation differences from users, you need abstract data types [0], and, sadly, Python doesn't have these.

[0] https://www.cs.cmu.edu/~rwh/introsml/modules/sigstruct.htm

> If you want to argue that Python ought to change its implementation to guarantee that, for example, any two references to the tuple (1, 2, 3) must refer to the same in-memory representation (so the 'is' operator would always return True), because that would save memory, or make the runtime faster, or whatever, that's fine.

I never said this either. I said that it should be a valid optimization, not that implementations absolutely have to do it. As things stand now, it's not a valid optimization, because it would break existing programs.

In actuality, I don't care much about the optimization itself. However, it's a good benchmark for assessing whether Python has compound values. Values can be deduplicated, because they may be represented more than once. Objects can't be deduplicated, because by definition an object exists exactly once in memory.

> But trying to claim that Python is violating "one of the cornerstones of Western logic" is just too much.

It seems appropriate to me. But, to be clear, what I was claiming violates the indiscernibility of identicals is dragonwriter's explanation, not Python itself. A simple explanation that doesn't violate the indiscernibility of identicals is to admit that Python doesn't have compound values. Which takes us back to square one [1].

[1] https://news.ycombinator.com/item?id=12358968

Re: Arguments against JSON-driven development

#212

It sounds to me like these arguments aren't so much against JSON, per se. They're against using JSON.parse() (or json.loads() in Python, json_decode() in PHP, or whatever) as your entire data-import process. Instead, the argument goes, one should load the JSON, walk the redulting structure, and use it to build your native data structure/objects/whatever. Similarly, when the time comes to save, you crawl through your…

This. "${PRACTICE}-driven development" suggests a practice that someone actively pursues because of perceived merit rather than a shortcut taken because of time/resource constraints.

Re: Arguments against JSON-driven development

#213
post #184

Earlier quoted context omitted.

And what happens when one of those keys changes?

You change your code. Loose coupling a nice goal to aim for, but at the end of the day, somewhere deep down inside the code, you have to tightly couple to actually get anything done. Where that transition occurs is entirely programmer's discretion.

But there's a difference between changing it once when you serialize/deserialize it, and changing it every time you try to access the key.

Re: Arguments against JSON-driven development

#214

Earlier quoted context omitted.

Don't mock me. And yes, I did mean the notion of mathematical value and variable. That's one of the core tenets of both.

> I did mean the notion of mathematical value and variable. That's one of the core tenets of both. Maybe from a big distance this is enough similarity to lump functional and logic programming together. But on closer inspection, functional and logic programs are still very different. > Don't mock me. I wasn't mocking you. You clearly must not understand functional and logic programming very well, if you think they are…

They are somewhat close in paradigm: They both favor declarativism, and have mathematical values. Given, they're pretty far apart in paradigm, but those are some strong similarities.

Re: Arguments against JSON-driven development

#215
post #8

I disagree with the anemic object argument. If an object is just there to store data and no behaviour, then that's fine - don't add behaviour if it doesn't need it. A large portion of back-end services are CRUD and data wrangling operations anyway - as in, convert data format A to data format B (which I guess could be a constructor or factory method if you're comfortable with having the conversion logic in a data cla…

> If an object is just there to store data and no behavior Then why do you have it at all?

I suppose because the language doesn't have typed records. You have to simulate them with objects.

Re: Arguments against JSON-driven development

#216

Earlier quoted context omitted.

> I did mean the notion of mathematical value and variable. That's one of the core tenets of both. Maybe from a big distance this is enough similarity to lump functional and logic programming together. But on closer inspection, functional and logic programs are still very different. > Don't mock me. I wasn't mocking you. You clearly must not understand functional and logic programming very well, if you think they are…

They are somewhat close in paradigm: They both favor declarativism, and have mathematical values. Given, they're pretty far apart in paradigm, but those are some strong similarities.

> declarativism

I'm not familiar with that term. Could you give a rigorous definition?

Anyway, after some googling, I found a very plausible definition that makes functional programming not a declarative paradigm: http://semantic-domain.blogspot.com/2013/07/what-declarative...

Re: Arguments against JSON-driven development

#217
I'm surprised no-one has linked Steve Yegge's Universal Design Pattern – http://steve-yegge.blogspot.co.uk/2008/10/universal-design-p...

It argues that loosely defined objects are an excellent design pattern, but I'm too tired to decide if it is directly relevant to this.

Re: Arguments against JSON-driven development

#218
post #138

Earlier quoted context omitted.

This. I've been programming this way for over a decade. Long before JSON was a thing. I find I rarely need anythng more than a list or a dict for most of the data manipulation I do. Being on the web has only strengthened my tendency for this, since everything ends up being stringly typed anyways. Nearly every function/API I write is: get some data from somewhere (hopefully serialized), manipulate the data, return dat…

Lists are lists, and I have no issues there. It's dicts as objects (often nested) where things to get hairy. I often see folks end up relying on internal implementation details of other libraries, or other data sources, and things can subtly break (or explode in a ball of fire). The more systems I've built, the more I've wanted to have very well-defined seams between "inside" and "outside" -- well-defined interfaces…

> I often see folks end up relying on internal implementation details of other libraries, or other data sources, and things can subtly break

I'm not sure how you can get around this as a consumer of a service? How do you know what is an internal implementation detail? Why are they exposing implementation details?

As a producer of a service there are lots of techniques. E.g.

- Only expose data and provide a spec for that data.

- Provide "helper" classes in target languages for consumers to use

- Publish an API spec + gaurantees

- Always maintain backwards compatibility

Re: Arguments against JSON-driven development

#219
I agree that the supplied code is improvable. Consider this:

  def set_r(adict, keypath, val):
    key = keypath[0]
    if len(keypath) == 1:
      adict[key] = val
      return
    if not key in adict:
      adict[key] = {}
    set_r(adict[key], keypath[1:], val)

  def build_book_inventory(book_ids, shops):
    shop_labels = [shop['label'] for shop in shops]
    books = Persistency_books_table_read(
      shop_labels=shop_labels,
      book_ids=book_ids)
    inventory = {}
    keys = 'shop_label cell_label book_id'.split()
    for book in book_list:
      keypath = [book[k] for k in keys]
      set_r(inventory, keypath, book['count']
    return inventory
First, the author clearly needed "autovivification" as supplied by Perl. We supply a substitute with set_r().

Second, I'd avoid creating local variables like "book_id". It creates mess. We never had the slightest interest in the book_id; it's just part of the wine we are pouring from one bottle into another.

Third, I've preserved (modulo names) the interface of this function but I suspect the surrounding code could also be improved. Also call a list of books "books", not book_list; list is the assumed sequence container in Python. "books=book_ids" is unfortunate; to thrive in a weakly typed language we need variable names that distinguish objects from ids.

Larger point: the author wants to create classes for the various business objects, which is a common enough pattern, but ultimately just makes extra work and redundant lines of code. A relational database can handle a wide variety of objects, with some knowledge of their semantics, without any custom code per-class.

As you know, the difference between dicts and objects in python is mostly semantic sugar. We can easily enough make a class that gives dot-notation access to values in a dict, if one objects to the noisiness of foo['bar'].

If you want to enforce object schema at system boundaries, there are better ways (more compact, expressive and maintainable) than writing elaborate "classes" for each type of object.

Re: Arguments against JSON-driven development

#220
The rule of thumb I've always used for when to use OO is "will there be more than one extant object at once or not?" If yes, and especially if these objects need real behavior, then use OO.

If you're essentially going through one object at a time, then discarding them, you're may just be doing conduit data processing, and so there's little advantage to using objects. I think what's missing in this (well-written) analysis is this distinction; if you're slurping data from one place, making a few changes (or especially if you're not making any), then sticking into a DB or vice versa, OO may be the wrong choice.

Ask yourself while writing the code: "are these active, behavior-driven objects that need encapsulation and relatively sophisticated behaviors, or is this just data I'm doing some relatively simple processing on?"

Post reply on HN