TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Arguments against JSON-driven development

257 pointsby afroisalreadyinover 8 years ago

58 comments

wtbobover 8 years ago
&gt; The fundamental advice on Unicode is decode and encode on system boundaries. That is, you should never be working on non-unicode strings within your business logic. The same should apply to JSON. Decode it into business logic objects on entry into system, rejecting invalid data. Instead of relying on key errors and membership lookups, leave the orthogonal business of type validity to object instantiation.<p>This right here is the correct approach. Serialisation formats should be serialisation formats, whether they be JSON, S-expressions, protobufs, XML, Thrift or what-have-you; application data should be application data. There are cases where it makes sense to operate on the serialised data directly, for performance or because it makes sense in context, but in the general case operate on typed application values.
评论 #12359212 未加载
评论 #12358862 未加载
评论 #12359135 未加载
评论 #12363222 未加载
评论 #12364821 未加载
sidllsover 8 years ago
I&#x27;m not entirely in agreement.<p>Use of object-oriented programming paradigms here would merely distribute the logic that is necessary to achieve the desired mapping over multiple points in the code.<p>The example function presented is only marginally too complicated. I&#x27;d split it in two: one to obtain the book list given the same arguments as the example function, and one taking the result as its only argument to build the mapping.<p>I find myself shying away from rigorous adherence to encapsulation more and more these days. I prefer small functions that operate on data explicitly.<p>Edit: and I&#x27;m a bit confused how the example has anything to do with &quot;JSON-driven development&quot;, other than the coincidence that a hash&#x2F;dictionary is the core data structure being manipulated here. This example function could exist and be (mostly) reasonable had JSON never existed. I&#x27;d expect to see an argument that the JSON serialization schemes that abound are problematic, given the title.
评论 #12359793 未加载
评论 #12359346 未加载
评论 #12365000 未加载
评论 #12363785 未加载
Cthulhu_over 8 years ago
I disagree with the anemic object argument. If an object is just there to store data and no behaviour, then that&#x27;s fine - don&#x27;t add behaviour if it doesn&#x27;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&#x27;re comfortable with having the conversion logic in a data class).
评论 #12359014 未加载
评论 #12374222 未加载
评论 #12365052 未加载
评论 #12359259 未加载
lmmover 8 years ago
The main reason this happens in Python is that creating actual datatypes is incredibly clunky (by Python standards) because of the tedious &quot;def __init__(self, x): self.x = x&quot;. The solution here is to have a very lightweight syntax for more specific types, e.g. Scala&#x27;s &quot;case class&quot;.<p>I&#x27;d also argue for using thrift, protobuf or even WS-* to put a little more strong typing into what goes over the network. Such schemata won&#x27;t catch everything (they have to have a lowest-common-denominator notion of type) but distributed bugs are the hardest bugs to track down; anything that helps you spot a bad network request earlier is well worth having.
评论 #12359016 未加载
评论 #12358954 未加载
评论 #12358998 未加载
评论 #12358976 未加载
评论 #12365101 未加载
评论 #12359047 未加载
mhdover 8 years ago
This basically repeats the ORM arguments&#x2F;counter-arguments, but now it&#x27;s a slightly more complex data structure instead of the DB-row-as-hash&#x2F;array you get there. &quot;row-driven&quot; in this context often leads to barely wrapped DAO Objects.<p>On the other hand, sometimes (surprisingly often) a hash is good enough and the effort spent in modeling the database (...) doesn&#x27;t need to be replicated.<p>And as with ORMs&#x2F;SQL generators&#x2F;DAOs&#x2F;etc., there&#x27;s a whole spectrum of solutions and you really have to look at the task to see what&#x27;s appropriate...
mythzover 8 years ago
This isn&#x27;t JSON-driven development, it&#x27;s just choosing to apply logic over loose-typed data structures instead of named constructs. It&#x27;s more awkward in Python because it doesn&#x27;t have sugar syntax to index an object like JavaScript has.<p>But using clean built-in data structures instead of named types has its benefits especially if you need to serialize for persistence of communication as it doesn&#x27;t require any additional knowledge of Types in order to access serialized data, so you can happily consume data structures in separate processes without the additional dependency of an external type system that&#x27;s coupled and needs to be carried along with your data.<p>This is why Redux uses vanilla data structures in its store or why JSON has become popular for data interchange, any valid JSON can be converted into a JavaScript object with just `JSON.parse()` which saves a tonne of ceremony and manual effort then the old school way of having to extract data from data formats with poor programatic fit like an XML document into concrete types.<p>If your data objects don&#x27;t need to be serialized or accessed outside of the process boundary than there&#x27;s little benefit to using loose-typed data structures, in which case my preference would be using classes in a static type system to benefit from the static analysis feedback of using Types.
评论 #12359901 未加载
评论 #12360697 未加载
mcmsover 8 years ago
Anemic objects and whether they are harmful or harmless has been debated in software engineering for long.<p>I find over-relying on encapsulation more harmful than useful nowadays specially if you are going to write scalable software that are inherently distributed. For example, hiding accessing a database behind a simple getter function makes another programmer ignore performance implication and other issues that may arise.
评论 #12359342 未加载
Millenniumover 8 years ago
It sounds to me like these arguments aren&#x27;t so much against JSON, per se. They&#x27;re against using JSON.parse() (or json.loads() in Python, json_decode() in PHP, or whatever) as your entire data-import process.<p>Instead, the argument goes, one should load the JSON, walk the redulting structure, and use it to build your native data structure&#x2F;objects&#x2F;whatever. Similarly, when the time comes to save, you crawl through your native structure to build a dict&#x2F;array&#x2F;primitive structure, then call JSON.stringify() (or the analogous function) to serialize that.<p>Uncoupling your data structure from the serialization format, though, is really just basic good software design anyway, is it not? Does anyone argue in favor what this article calls &quot;JSON-driven development&quot; as a design principle? Or is it just a shortcut that developers -and I am no less guilty of this than anyone else- sometimes take in the interest of getting a quick-and-dirty solution out the door?<p>Yes, working directly on the output of JSON.parse() is a code smell. But I&#x27;m not sure that claiming there&#x27;s a rising trend of &quot;JSON-driven development&quot; is entirely founded. It&#x27;s just people taking shortcuts.
评论 #12362213 未加载
micimizeover 8 years ago
While I see the point the Ulaş is getting at, I wouldn&#x27;t call this JSON-driven development. I think JSON-driven development would use abstraction layers that are based on JSON, like JSON schema, and perhaps an OOP library that leverages it.<p>What I&#x27;d actually call this problem is a lack of abstraction. In functional programming, simple data structures are often preferred, and composable functions are used to manage complexity. A functional programmer might declare a function `to_structured_dict(enumerable, path)` and call it with `to_structured_dict(book_list, path=(&#x27;shop_label&#x27;, &#x27;cell_label, &#x27;book_id&#x27;, count&#x27;))`
评论 #12359320 未加载
评论 #12359281 未加载
falcolasover 8 years ago
If you&#x27;re in Python, and are afraid of &quot;anemic&quot; objects, I would recommend checking out collections.namedtuple. It&#x27;s a fantastic lightweight and performant object-like data structure.<p>You also get a few additional features, such as in-order iteration, the parameters are fixed at run time, and there&#x27;s a method for turning it into an ordered dictionary (which is serializable in, wait for it, JSON).
评论 #12358919 未加载
Singletonedover 8 years ago
&gt; Once you go dict, you won&#x27;t go back. This style of development is too easy, since dictionaries are baked into Python, and there are many facilities for working effectively with them.<p>How is this an argument against using dictionaries?<p>After 10 years of Python development, I do find myself using dictionaries rather than objects, in just the way that the author proscribes, but I&#x27;m finding it to be a genuine pleasure.
评论 #12360326 未加载
评论 #12361046 未加载
beatover 8 years ago
I think this article is somewhat off-base. The problem isn&#x27;t JSON, it&#x27;s lack of respect for separation of duties. JSON is just a data exchange format.<p>Want to program in an OO way using JSON? Easy. Just build a factory to generate objects from JSON input. Put your validation and error handling right there. Now you can get a known valid object from the JSON, a class instance with all the encapsulation and business logic your heart desires. Need to share it with the outside world? Provide a JSON output method.<p>Translating data formats is at the heart of day-to-day programming. It ain&#x27;t rocket surgery. Fix the problem, not the blame.<p>(And if you think JSON sucks, believe me, you never dealt with data file formats from the pre-XML days!)
评论 #12359777 未加载
nbevansover 8 years ago
There is so much wrong with this blog post that I don&#x27;t even know where to begin. He appears to have included Python-specific details in his list of why he hates lists and dictionaries. Apparently Python throws exceptions if a key doesn&#x27;t exist and seemingly has no Maybe&#x2F;Option alternative? I don&#x27;t know if that is true or not.<p>He claims using lists and dictionaries means you lose encapsulation - does it? A smarter programmer would realise that actually it entirely depends on the _types_ you are storing in those data structures.
digisthover 8 years ago
The rule of thumb I&#x27;ve always used for when to use OO is &quot;will there be more than one extant object at once or not?&quot; If yes, and especially if these objects need real behavior, then use OO.<p>If you&#x27;re essentially going through one object at a time, then discarding them, you&#x27;re may just be doing conduit data processing, and so there&#x27;s little advantage to using objects. I think what&#x27;s missing in this (well-written) analysis is this distinction; if you&#x27;re slurping data from one place, making a few changes (or especially if you&#x27;re not making any), then sticking into a DB or vice versa, OO may be the wrong choice.<p>Ask yourself while writing the code: &quot;are these active, behavior-driven objects that need encapsulation and relatively sophisticated behaviors, or is this just data I&#x27;m doing some relatively simple processing on?&quot;
corysamaover 8 years ago
The author has lots of good points. Because I write most of my Python in the style he is advising against, I recognize that style has issues. The main issue for me is that a dict of dicts of dicts is not an interface. It doesn&#x27;t have any constraints. It doesn&#x27;t communicate expectations for use for the actual intent of the code. The best you can do is a comment explaining what to expect and a lot of error checking.<p>That said, almost all of the python I write these days is in the form of functional transforms on built-in data structures. And I love it!<p>There was a great Pycon2012 talk titled &quot;Stop Writing Classes&quot;. You can find it linked and discussed here <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=3717715" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=3717715</a>
评论 #12360049 未加载
agentgtover 8 years ago
On the one hand I agree with OP on directly interacting with JSON is not really a good idea but on the other hand I completely disagree with that behavior should be shoved into data objects. Also I think part of the problem is Python doesn&#x27;t have much typing (I know they recently added optional typing in python but I don&#x27;t think many use it).<p>As more of an FP guy I&#x27;m firm believer of the separation behavior and data. Clojure&#x27;s Hickey sort of has a valid point... its freaking data... stop making it complicated to access it.
Robin_Messageover 8 years ago
I&#x27;m surprised no-one has linked Steve Yegge&#x27;s Universal Design Pattern – <a href="http:&#x2F;&#x2F;steve-yegge.blogspot.co.uk&#x2F;2008&#x2F;10&#x2F;universal-design-pattern.html" rel="nofollow">http:&#x2F;&#x2F;steve-yegge.blogspot.co.uk&#x2F;2008&#x2F;10&#x2F;universal-design-p...</a><p>It argues that loosely defined objects are an excellent design pattern, but I&#x27;m too tired to decide if it is directly relevant to this.
thesmallestcatover 8 years ago
It&#x27;s called a hash. Or a dict. Or a map. Not JavaScript Object Notation, FFS.
spullaraover 8 years ago
At this point everyone should be using an evolvable (thrift, protocol buffers, avro, etc) schema format when they are storing or transmitting their data if they want to run an always on service - there is no downtime for migrations in the real world. Trying to do this ad-hoc with JSON is a lost cause and will eventually lead you to failure at runtime or worse, data loss situations.
评论 #12363672 未加载
评论 #12360603 未加载
jjzieveover 8 years ago
At least lists, dictionaries map relatively well to a tabular (SQL) format. Objects don&#x27;t map well at all! Anyone who&#x27;s spent enough time with &quot;mature&quot; ORMs knows this. Especially when there&#x27;s a deadline and you have to write &quot;native&quot; SQL just to get whatever the hell you needed in the first place. &quot;Well maybe you should have read everything and understood the ORM to its most minute detail...&quot; NO! That&#x27;s the whole point of abstraction! If I understood everything about that code, I&#x27;d be better off re-writing it to better suit MY specific problem. Look, I don&#x27;t want to be another OO basher. OO definitely has a place in complex systems like game development, where the lives of the objects are longer than a page refresh. But in web dev, its becoming increasingly obvious to me that the OO paradigm is a huge time suck. &#x2F;rant
评论 #12360723 未加载
lgunschover 8 years ago
I find that code that uses dictionaries a lot ends up with mysterious unnamed types used in various places throughout the system disguised as dictionaries. They have required fields, and must interacted with using business logic that is not obvious. This becomes a real problem when the original authors of the system are gone, and new maintainers have taken over and have to implement new features.<p>By using objects, or lightweight objects like namedtuple, which have already been mentioned in other comments a bunch, you formally document the data-structure. You give it a name, expected fields, and required behaviours when interacting with it. The code becomes much easier to follow and understand clearly. Bugs don&#x27;t creep in when a new maintainer forgets about the mysterious undocumented required business logic.
keithnzover 8 years ago
Lots of religion in this thread!<p>I think the point is, if json is your data exchange format it <i>could</i> be bad if you let that structure propagate into your application.<p>so in general you should prefer :-<p>json =&gt; &lt;chosen languages best form for dealing with data&gt;<p>over json =&gt; &lt;chosen languages tools for dealing with json&gt;<p>Different languages are going to have different mechanisms. Some languages you may abstract from json completely, some languages may natively deal with json, and your persistence layer may deal with json also.<p>So what you need is a well considered design that takes advantage of your chosen languages philosophy &#x2F; mechanics, whatever that may be. There is no one way to design anything. The thing to avoid is - not working out how to structure your code to make things easy &#x2F; appropriate to the task at hand. That path leads to messy code.
qwertyuiop924over 8 years ago
For crying out loud, you don&#x27;t have to build an object hierarchy around the thing (although it would make sense to in this case), but at least have the common sense, or the sense of shame, to abstract away data lookups into separate functions. That&#x27;s data structure abstraction 101.
lanestpover 8 years ago
I take a couple of issues with the author here. I don&#x27;t personally worry much about breaking away from strict OOP. When a pattern like this develops it is usually because the data is too dynamic for a static property list. An obvious example is for creating reports. No one is going to sit down and hand design every single multi column report in a large project (I tell a lie, people do, it just makes the code base a horror show). By letting the data be more dynamic (Usually with JSON) it is trivial to create generic report structures and populate them.<p>Additionally, if you are using NoSQL as a backing store then solutions like class serialization don&#x27;t make any sense since you will need to communicate in JSON anyway.
grandalfover 8 years ago
The code in the example has poor encapsulation, but I do not think it&#x27;s the &quot;JSON&quot; style that causes that.<p>Much OOP code includes a hodgepodge of exposed internal state and methods that offer a combination of derived state and behavior to mutate that state.<p>Often, using data literals (like JSON) can make code clearer by making it explicit what is going on with state (when&#x2F;if it is being mutated), and making the system easier to snapshot, test, etc.<p>While much code that uses JSON-like constructs is overly verbose and error prone, adding a bit of structural typing (with Flow) or creating schemas to ensure system invariants (jsonschema) can lead to a system that is easy to reason about and maintain.
weatherlightover 8 years ago
Use GraphQL or Really stick with RESTful routes. The more predictable the schema of these Dictionaries&#x2F;hashes&#x2F;JSON are the less likely you are to see that mess above. This is true whether you are using a FP approach or an OO approach. Using an Imperative coding style when doing ETL will always be hairy.<p>That function also violates the Single responsibility principle. I wouldn&#x27;t even know where to begin to write a unit test for that other than breaking it down into smaller parts. There are design patterns that could be followed in dynamically typed languages that would avoid that mess altogether other than just OO.
smizellover 8 years ago
Coupling your code to the JSON you receive over the web can lead to some interesting problems. If the system on the other end decides to make some change you are not expecting, it can lead to errors.<p>In JavaScript, a simple thing that helps is to use lodash.get and provide a path to the property you are wanting.<p><pre><code> lodash.get(someObject, &#x27;path.to.a.property&#x27;) </code></pre> If the path isn&#x27;t there, the lodash.get returns undefined. This is much nicer than getting the error &quot;Cannot read property &#x27;to&#x27; of undefined&quot; when &quot;path&quot; isn&#x27;t there.
评论 #12359553 未加载
评论 #12360358 未加载
bluetwoover 8 years ago
Yes, any useful technology will get over-applied, at the detriment of better ways to do things.<p>Not the fault of the technology, but of the developer who failed to consider alternate ways of accomplishing the same task.
tscs37over 8 years ago
JSON is best when it&#x27;s solely used for serialization (or config files).<p>Using it deep into the project makes no sense, the first step in handling JSON should always be to code it into native data structures.
评论 #12359828 未加载
评论 #12359186 未加载
评论 #12359381 未加载
ajmurmannover 8 years ago
I think the &quot;anemic objects[domain model]&quot; is a red herring in this case. It would be much cleaner to create separate serialized and deserializers that convert your actual domain models to JSON and back. By the time you are doing something as shown in his example like building a book inventory it should be all proper objects and no primitives dictated by what should be the serialization layer.<p>Edit: Fixing phone auto correct typo - &quot;property objects&quot; -&gt; &quot;proper objects&quot;
mpweiherover 8 years ago
I actually saw this coding style <i>way</i> before JSON, for example at Apple. I even jokingly created DUKE: Developers United against Keyed Everything.<p>Another place I see this is in the eternal dynamic&#x2F;typing debate. A lot of the criticism of dynamic typing will be with examples from JavaScript, Ruby, Python and maybe even PHP. Hardly ever from Smalltalk (or Objective-C), because Smalltalk code tends to not have the types of problems cited. This puzzled me for a while, because I also find these languages somewhat less &quot;solid&quot;, yet couldn&#x27;t quite put my finger on why.h<p>That is, until I realised that all of these languages use hashes as their basic object representation. Coincidence? I think not. So I coined the term &quot;hash language&quot; for these languages, both because they are hash-based and it appears to be easy to make a hash of things in them, possibly for precisely that reason.<p>That said, I think it&#x27;s also a mistake to disregard the power this sort of very generic programming brings, especially once you consider objects composed of multiple facets that are interpreted in different contexts.<p>IMNSHO, the way to combat hash-programming is to provide powerful and convenient metaprogramming facilities for object representation, so dealing with objects generically is just as easy and obvious as dealing with dictionaries.<p>Not entirely surprisingly, my own language ( <a href="http:&#x2F;&#x2F;objective.st" rel="nofollow">http:&#x2F;&#x2F;objective.st</a> ) has some facilities for this, mostly by making identifiers into first class entities. More research needed ;-)
iamleppertover 8 years ago
Is the OP familiar with Object.keys()?<p>You don&#x27;t have to hard-code explicit dot notation into your code when you&#x27;re processing JSON or any other hierarchical object serialization format, which is what JSON is.<p>If you want to make your code more robust, you should process the structure of the JSON document and infer meaning from its keys based upon your position in the tree and of the values of the key names that are meaningful to your application.<p>This makes it possible to accept any kind of JSON, even if the original format changes, and you won&#x27;t get uncaught exceptions and your application can decide what to do in a more graceful manner.<p>You should also centralize the code that is responsible for serializing and deserializing your JSON wire format and creating objects. There&#x27;s no reason to have ad-hoc code in each object constructor like his example. A good example of such a thing is dnode <a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;dnode" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;dnode</a>. It handles all the JSON abstraction (in this case for RPC) and you don&#x27;t even need to worry about the JSON ever again.<p>This has nothing to do with JSON and more to do with poor design and tight coupling of interfaces.
spdustinover 8 years ago
I may be missing something, so I&#x27;d appreciate a correction, but why all that effort when you can use collections.namedtuple and a custom object_hook for json.loads?<p><pre><code> import json from collections import namedtuple data = &#x27;{JSON string goes here}&#x27; fancy_data = json.loads(data, object_hook=lambda d: namedtuple(&#x27;X&#x27;, d.keys())(*d.values()))</code></pre>
K0nservover 8 years ago
I agree with this and I&#x27;ve raised it several times in Objective-C codebases that can some times end up littered with NSDictionary:s everywhere taking no advantage of the Objective-C type mechanics. I don&#x27;t think it&#x27;s necessarily as bad in python or javascript. This is because these languages are dynamically typed and that diminishes the benefits of deserializing json to a native model. It&#x27;s still valuable because you can guard against bad data by rejecting at the boundary of your program.<p>In statically typed languages however the added bonus is a lot more significant because the type system increases the benefits of deserializing JSON to native models. Take this Swift example<p><pre><code> import Foundation enum SerializationError: ErrorType { case InvalidData } struct Thing { let a: Int let b: String static func deserialize(fromDictionary data: [String:AnyObject]) throws -&gt; Thing { guard let a = data[&quot;a&quot;] as? Int, let b = data[&quot;b&quot;] as? String else { throw SerializationError.InvalidData } return Thing(a: a, b: b) } static func deserialize(fromArray data: [[String: AnyObject]]) -&gt; [Thing] { return data.flatMap { try? deserialize(fromDictionary: $0) } } } let data: [[String: AnyObject]] = [ [ &quot;a&quot;: 10 as NSNumber, &quot;b&quot;: &quot;Hello&quot; as NSString ], [ &quot;a&quot;: &quot;10&quot; as NSString, &quot;b&quot;: 10 as NSNumber ] ] let models = Thing.deserialize(fromArray: data) </code></pre> Not only do you end up with a native array of models you can also be certain that any type information is correct because invalid results have been thrown away during parsing.
评论 #12359348 未加载
mooseyover 8 years ago
This is what keeps dragging me back to moose &amp; perl5. You describe the attributes of a class, the constructor for that class is created for you, and you can pass in hashes and it will automatically instantiate (and fail if the rules you have set for attributes are not met).<p>I&#x27;ve found that you can kinda sorta do the same with other languages (python&#x2F;ruby&#x2F;javascript) by writing static factory builders inside the class that do this checking for you and raise an exception or return an object for you, but it still doesn&#x27;t compare to me to Moose&#x2F;Moose::Util::TypeConstraints::coerce&#x2F;subtype and attributes with the coerce option set. It makes it so easy to coerce a deep json object into a deep class structure.<p>I always try to hunt down things that are similar in other languages (python allows named arguments from a dict, IIRC, and that allows similar things, but you still have to write the constructor yourself), but I&#x27;ve yet to find something that makes it as simple.
sleekover 8 years ago
If he used tuples as keys to the dict, he wouldn&#x27;t have this absurd code and the article wouldn&#x27;t have been written
zdwover 8 years ago
And this is where we end up without easy to use and well supported schemas...<p>If this was XML, you&#x27;d write a very simple RELAX NG grammar (use the compact syntax: <a href="http:&#x2F;&#x2F;relaxng.org&#x2F;compact-tutorial-20030326.html" rel="nofollow">http:&#x2F;&#x2F;relaxng.org&#x2F;compact-tutorial-20030326.html</a> ) that describes the structure of the incoming data, then use it to validate the input data before processing it.<p>After that, you know data is valid and in the right structure, so you can throw away most of the &quot;is this in the right place?&quot; checks.<p>JSON and YAML&#x27;s various schema implementations can&#x27;t hold a candle to this, and it&#x27;s been around for over a decade.<p>The XML ecosystem does have some very bad parts, but it&#x27;s not all bad, so it&#x27;s worth learning from places where it actually works well.
评论 #12358993 未加载
michaelfeathersover 8 years ago
This is space that the Clojure community has already visited.
评论 #12361678 未加载
评论 #12359105 未加载
cruciniover 8 years ago
I agree that the supplied code is improvable. Consider this:<p><pre><code> 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[&#x27;label&#x27;] for shop in shops] books = Persistency_books_table_read( shop_labels=shop_labels, book_ids=book_ids) inventory = {} keys = &#x27;shop_label cell_label book_id&#x27;.split() for book in book_list: keypath = [book[k] for k in keys] set_r(inventory, keypath, book[&#x27;count&#x27;] return inventory </code></pre> First, the author clearly needed &quot;autovivification&quot; as supplied by Perl. We supply a substitute with set_r().<p>Second, I&#x27;d avoid creating local variables like &quot;book_id&quot;. It creates mess. We never had the slightest interest in the book_id; it&#x27;s just part of the wine we are pouring from one bottle into another.<p>Third, I&#x27;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 &quot;books&quot;, not book_list; list is the assumed sequence container in Python. &quot;books=book_ids&quot; is unfortunate; to thrive in a weakly typed language we need variable names that distinguish objects from ids.<p>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.<p>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[&#x27;bar&#x27;].<p>If you want to enforce object schema at system boundaries, there are better ways (more compact, expressive and maintainable) than writing elaborate &quot;classes&quot; for each type of object.
rm999over 8 years ago
More generally, using data structures well-suited to your problem is really important but often underappreciated in software engineering. Elegant code and algorithms naturally follow.<p>In the example in the article, OO seems like a good way to go.
dgb23over 8 years ago
From an OO perspective I can completely agree with this and the encode&#x2F;decode pattern the author is suggesting seems to be the right way to deal with this problem as it will also hopefully translate in having an esperanto data type which can be used for all sorts of APIs and formats.<p>But from a functional perspective I would disagree. The code example wouldn&#x27;t even make sense in that world. You would query it as is and compare it with other data or create new structures as with any other data you handle in your language.
tony-allanover 8 years ago
I agree 100% with the article.<p>I&#x27;ve just written a script which makes every one of the mistakes listed in the article. I am consuming a JSON based API in a long ugly mash of code, exactly as described. It doesn&#x27;t look pretty.<p>In my defence, I wrote the code as I was trying to understand the API. I had not read ahead and didn&#x27;t really know which API&#x27;s I would need or how fiddly it would be to bring it all together.<p>I am now exposed to pain if the API changes or if anything breaks. Time to go back and tidy up the code!
评论 #12371001 未加载
astazangastaover 8 years ago
JSON is the equivalent of a UNIX pipe; it is for passing data between applications on the Internet, just like a pipe is a way for passing data between applications on a machine.
codedokodeover 8 years ago
In PHP we call it &quot;Array Oriented Programming&quot; ( <a href="http:&#x2F;&#x2F;www.epixa.com&#x2F;2012&#x2F;04&#x2F;array-oriented-programming.html" rel="nofollow">http:&#x2F;&#x2F;www.epixa.com&#x2F;2012&#x2F;04&#x2F;array-oriented-programming.html</a> ). So looks like Python developers finally discovered this paradigm too. Let&#x27;s wait for JS developers now.<p>By the way, PHP has type hints for functions that help to understand what are the types of arguments and the return type.
joelcorreaover 8 years ago
Pretty much statically typed vs dinamically-typed discussion. It depends on your particular case, if there is a sufficiently defined schema or not
评论 #12359587 未加载
jroseattleover 8 years ago
Just an aside, I&#x27;m not sure I agree with the title of &quot;JSON-driven&quot; development.<p>The problem being described is about using parts of a language in an inefficient or ineffective way of solving a problem.<p>The solution to this particular problem can be summed up in concepts like encapsulation or DRY. More to the point, let&#x27;s not blame components like JSON for basically poor implementation.
malodyetsover 8 years ago
I prefer XML + schema (for which I use RelaxNG + jing) because this makes validating the input very straightforward and normalizes that process. But I understand the appeal of JSON and use it for some APIs myself. What experiences do people have with JSON.net Schema? Does anyone know of a json schema + validator system that is cross-language?
emodendroketover 8 years ago
There&#x27;s no particular reason objects can&#x27;t be serialized as JSON and deserialized back into strongly typed objects.
partycoderover 8 years ago
I like to wrap relevant objects around a type, that is responsible for creating, validating, serializing, deserializing and mutating that object.<p>But taking a JSON and passing it around, with no ownership, no predictability... it&#x27;s the mindset of the tech debt programmer.
qwertyuiop924over 8 years ago
I think the general rule is &quot;validate anything coming in from the internet, ever.&quot;
collywover 8 years ago
Thankfully there isn&#x27;t really JSON <i>driven</i> development as a methodology. Its just a bit of a crappy pattern (for many tasks) that far too many people use. Hopefully no one is advocating it as <i>the</i> way to develop software.
risover 8 years ago
I couldn&#x27;t agree more. My life these days consists 90% of marshalling json around. Hooray for microservices.
seomisover 8 years ago
So we just need references? Let&#x27;s all switch to XML (just kidding) or YAML (maybe not kidding)!
Goladusover 8 years ago
Although I coincidentally mostly agree with the conclusion &quot;encode on entry and decode on exit&quot; I disagree substantially with the rest of the article.<p>&gt; I know that it&#x27;s now en vogue to sneer at OO<p>I don&#x27;t <i>sneer</i> at it. OO has been the dominant coding religion for most of my career and I rage against the educators and propagandists who spend a decade smothering everyone with it. I curse them for all the time I wasted trying to build classes for my data only to realize that if I had used a simple dictionary or list my code would be shorter, simpler, more robust, and more flexible.<p>The logic in the article above is all premised on object-oriented religion. OO for OO&#x27;s sake because OO. Using the power of dictionaries is bad, using the power of objects is good.<p>&gt; It completely defeats object orientation.<p>You could just as easily say using objects defeats the point of having lists and dicts.<p>&gt; It offers nothing of the abstraction powers of object orientation.<p>Most of the abstraction power of object orientation happens when you create the methods. If you aren&#x27;t defining new classes, you write functions instead. You still have abstractions, what you don&#x27;t have is the encapsulation of code with data.<p>&gt; It doesn&#x27;t say what it&#x27;s doing.<p>Sure it does, and better yet since you are using the standard data types it will be said in a language that any other Python developer is likely understand immediately.<p>&gt; The above code is filled with auxiliary logic that has nothing to do with what it actually tries to achieve.<p>The above code is filled with auxiliary logic because the author of it apparently didn&#x27;t write any useful functions for operating on dictionaries.<p>I&#x27;m not sure that any of the auxiliary logic in that code has anything to do with the choice to use dictionaries. It&#x27;s a standard data munging problem that comes from having data from different sources. You have the exact same problem with objects if you didn&#x27;t write any useful methods for operating on them.<p>&gt; but done right, it can be very powerful, especially in big and complex codebases.<p>And in my opinion, the right way to do Object-Orientation in Python is to <i>not do it</i> until you really know you need it: your code is heading towards big and complex and you need to lock it down and organize the data and methods into well-encapsulated classes. (Although maybe at that point you realize it&#x27;s not a big deal and don&#x27;t bother)<p>Designing around lists and dicts from the start is a much more flexible strategy than trying to get all the encapsulation exactly right on the first try. If you don&#x27;t have lots of time to spend up front UML&#x27;ing an object heirarchy for your big, complex application, you&#x27;re probably better off sketching and iterating with JSON in mind (and yaml if humans need to edit it). As your application takes shape, it will become apparent where it makes sense to lock down functions and data into objects.<p>This is especially true given that all of Python&#x27;s standard types are inheritable classes.
drawkboxover 8 years ago
Arguments for dict&#x2F;list driven development:<p>NOTE: This isn&#x27;t JSON driven-development, JSON just mimics base types like dict, list, string, numeric etc of all languages and a big reason it is so common especially in Python&#x2F;Javascript.<p>- Large unknown lists&#x2F;dictionaries data structures can be deserialized into dicts&#x2F;lists for any language without issue.<p>Sometimes keys&#x2F;data are unknown such as large attribute data sets that may always have new keys. In that case strong typing to an OO object will always be broken. Example: a facebook attribute set, keys&#x2F;data that aren&#x27;t set will not appear, new ones are added all the time, which would create a cat&#x2F;mouse serialization&#x2F;deserialization game. Same problem a binary structure has (offsets) when you really need a flexible keyed structure.<p>One missing key doesn&#x27;t break your whole serialization&#x2F;deserialization system built with strongly typed OO. Validation can be done on accept and <i>if necessary</i> convert into an OO system.<p>- If needed, classes that are backed&#x2F;extended&#x2F;inherited by a dict&#x2F;list or set (or composition) that can load in the JSON&#x2F;dict&#x2F;lists and only expose the needed values after validation are useful.<p>i.e. a class that inherits from or composes to Dictionary&lt;string,object&gt; for instance in C# would only fill keys that are necessary for the view data, not a bunch of extra null fields because it might not have that key&#x2F;property. It also has the ability to deserialize objects that may have new keys. Not everything is a perfect world where data structures are known before-hand.<p>- It reduces complexity many times, no need for an OO serialize&#x2F;deserialize layer when you are passing back as basic dict&#x2F;list or JSON.<p>Why add complexity to something simple?<p>- Unless you control the server and the client, real-world data structures aren&#x27;t a perfect map of keys&#x2F;values to OO properties.<p>Assuming that is a system ready to break in the real-world many times. Someone adds a field to the DB object then all clients that use it can&#x27;t serialize&#x2F;deserialize. Real world serialization&#x2F;deserialization has to accept in basic types dict&#x2F;list, validate and then use as needed (some go to OO objects behind scenes). I see too many systems where people just have an EF object and expose that over a web api and just expect it to work, that is a bad example of poor encapsulation. Some fields don&#x27;t need to be serialized to public apis. In Microsoft land MVVM was created to help stop this practice but still creates two sets of OO objects and breaks on any new keys&#x2F;data (though breaking here may be desired for strong typing).<p>- Dict&#x2F;list data structures can be easily setup to have cleaner naming and keys without tons of attribute&#x2F;helpers<p>i.e. first-name key instead of first_name, FirstName, or firstName. This is more friendly to web&#x2F;url naming that is common.<p>- Noted in the article, less memory used in many cases and highly optimized time in basic lists&#x2F;dicts.<p>There are many more reasons...<p>Dicts, lists and basic string, numeric types are the base of all languages and computer science types. The reason this is common is it is simple to work with these types without added cruft of OO when needed.<p>OO does add complexity and if it isn&#x27;t necessary you are just upping complexity for no reason (and memory). It is similar to the complaints of C coders to C++, basic structs and sets are sometimes less complex than C++ OO objects. Same thing with dict&#x2F;list of some monstrosity of an OO serialization&#x2F;deserialization system that breaks on every new key or field and you have to update the server and client rather than just increment a version and validation. The longer you code the more you see this.<p>OO objects should not be used all the time just like dict&#x2F;lists shouldn&#x27;t be used all the time.
hardlianotionover 8 years ago
Completely agree.
jowiarover 8 years ago
With dynamic languages (certainly Python, Ruby, and JS), there&#x27;s a definite lack of nudge from the tooling to translate from &quot;interchange&quot; format into an internal &quot;smart&quot; format (procedural code + everything is a hash = easy hacks). Whereas with something like Scala, the tools make it very clear that if you serialize&#x2F;deserialize on the periphery, you&#x27;re in for a bag of hurt (or, at the very least, fighting with one hand tied behind your back).<p>This is not to say that one tool is better than the others, but tools do have opinions, and while being more permissive&#x2F;ambivalent makes throwing together a quick script easier, a tool that nudges you in the direction of building something in a more-sustainable way is useful when building nontrivial systems.
catnaroekover 8 years ago
&gt; If an object is just there to store data and no behaviour, then that&#x27;s fine - don&#x27;t add behaviour if it doesn&#x27;t need it.<p>In that case, you want values rather than objects. Alas, Python doesn&#x27;t have compound values.
评论 #12364117 未加载
评论 #12359050 未加载
评论 #12359697 未加载
评论 #12359295 未加载
评论 #12361706 未加载