6 ms·
Nifty, but having "and", "or" and the rest before both of the arguments instead of in the middle is incredibly confusing to read. Very unintuitive. Why not rest
by ysavir 10y ago
Nifty, but having "and", "or" and the rest before both of the arguments instead of in the middle is incredibly confusing to read. Very unintuitive. Why not restructure it to be chainable with something like:
firstVar.or(secondVar.eq('Bob').and(thirdVar.eq(41)));
- ww520 10y agoPrefix operator is the Lisp notation and it has work pretty well. The syntax rule is very simple. Once got used to it, it's very easy to deal with it. Chainable calls work well with linear operations. With nested scope, it becomes less clear.
- ralusek 10y agoNot at all, I think that many people have come to expect this syntax from many ORMs. You will often see syntax like: { and: [ {or: [a, b]}, {a: {gt: b}} ] }
- kakarot 10y agoPrefix operators are pretty common, actually. They have their strengths
- lmkg 10y agoWhich style is more readable depends on whether in practice you end up with more sequence-like structures, or more tree-like structures. Chaining makes sense for sequences. For tree-like structures, they get confusing because the nesting levels aren't apparent. For example: In your code sample, it's a bit unclear what nesting level the .and() call is occurring at. It could be a method call from the chain that starts at secondVar, or the chain that starts at firstFar. Distinguishing which one requires counting all the intervening parentheses. This okay for the given example, but gets out of hand as the conditions get complicated. Ergonomics aside, I don't think the prefix notation is unintuitive. It's a bit verbose, since JavaScript doesn't have operator overloading, but to me it seems the most sensible and straightforward way of structuring the relationships.
- ysavir 10y agoGood point, and I agree that the code in my example wasn't perfectly clear, though I think some formatting could help it go a long way. I don't think the prefix notation is much more readable than what I suggested, if it is more readable at all... But without any hands on experience with the library, I am in no position to make that argument. In the spirit of excessive, needless suggestions: Maybe they can make it like the testing libraries, where you can import different packages for different syntaxes... (not serious)
- whatever_dude 10y agoAgree in essence - the code is basically saying something like "AND CONDITION1, CONDITION2" which is awful to me. In that context, I think "any" (for "or") and "all" (for "and") make a lot more sense. E.g. all(first, second) It'd read as "ALL of CONDITION1, CONDITION2". More complex combinations are also easier. all(any(first, second), third)
- haxy 10y agoThat would be a lot easier to do. Based on the example all you need to do is: var any = logic.or, all = logic.and;
- xlayn 10y agoWhy is such complication introduced and what are the benefits of it? firstVar || (secondVar=='Bob' && thirdVar==41) seems very clear to me. On the other hand JavaScript has always had logic operators... therefore has always been able to perform logic programming..... From http://www.ecmascript.org/es4/spec/overview.pdf http://www.ecmascript.org/es4/spec/overview.pdf page 18 “&&=” and ”||=” operators ES4 introduces assignment operators for the logical “&&” and “||” operators. These assignment operators are short-circuiting; if the value of the left-hand- side determines the value of the result then the right- handside is not evaluated.
- detaro 10y agoThis library can do Prolog-style solving of logical expressions. As in "here is a set of expressions, find me values for the variables that fulfill them." Completely different thing. classical example in the readme: https://github.com/mcsoto/LogicJS#goals https://github.com/mcsoto/LogicJS#goals
- xlayn 10y agoThanks for the reply; "Prolog-style solving of logical expressions in JS" sounds like a better title.
- kaoD 10y ago"Prolog-style solving of logical expressions" is actually called "logic programming". It's the name of the paradigm. https://en.wikipedia.org/wiki/Logic_programming https://en.wikipedia.org/wiki/Logic_programming IMO the title is fine.
- always_good 10y agoNext time please at least click the link before chiming in with a comment. The title was not the problem here.