LWN.net Logo

Reitter: Answering the question: "How do I develop an app for GNOME?"

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 5, 2013 19:39 UTC (Tue) by khim (subscriber, #9252)
In reply to: Reitter: Answering the question: "How do I develop an app for GNOME?" by oldtomas
Parent article: Reitter: Answering the question: "How do I develop an app for GNOME?"

JavaScript's core is fine, but it was frozen too early.

Not even close.

> test = Array(8, 9, 10)
  [8, 9, 10]
> test.sort()
  [10, 8, 9]

Looks logical? Let's fix it:

> test.sort(function(a,b){return a-b})
  [8, 9, 10]

Cool. What about strings?

> test = Array("8", "9", "10", "b", "a")
  ["8", "9", "10", "b", "a"]
> test.sort(function(a,b){return a-b})
  ["8", "9", "10", "b", "a"]

Hmm... That's strange. Looks like we'll need to check types of variables. Can we at least compare few variables?

> a = 10
  10
> b = "11"
  "11"
> c = "9"
  "9"
> a < b
  true
> b < c
  true
> c < a
  true

Apparently not. Can we at least compare two?

> a = "a"
  "a"
> b = 10
  10
> a <= b
  false
> b <= a
  false

Looks like the answer is still "not".

And final example.

test = "Hello"
  "Hello"
j = 0; for (i in test) j+=i; j
  "001234"

Can you understand WFT goes on here and why the following fix works?

j = 0; for (i in test) j-=-i; j
  10

PHP is known as fractal of bad design but JavaScript is close second. It's not even close to scheme in this regard: scheme is sane language JavaScript and PHP are not.


(Log in to post comments)

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 5, 2013 21:38 UTC (Tue) by nix (subscriber, #2304) [Link]

There is only one possible response to the tidal wave of lunacy in that post of yours: WAT.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 8:21 UTC (Wed) by osma (subscriber, #6912) [Link]

For a slightly expanded WAT in video format, also covering some absurdities in other programming languages besides JavaScript, see Gary Bernhardt's talk.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 5, 2013 22:25 UTC (Tue) by smokeing (guest, #53685) [Link]

Thank you for such a clear exposition of JavaScript absurdity.

One can indeed live a long, happy and rich life as a programmer and have no regrets at the end -- all without knowing what JavaScript is.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 0:03 UTC (Wed) by FranTaylor (guest, #80190) [Link]

What's funny about your comment is that it can be repeated almost verbatim in any computer language. Really I challenge you to find a computer language where you CANNOT make absurd constructs like this.

"Look at me, I can create create syntactic constructs that seem to make no sense! Wow how clever is that?"

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 6:09 UTC (Wed) by Cyberax (✭ supporter ✭, #52523) [Link]

Do it with built-in types in Haskell, OCaml or even Java.

Thanks in advance.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 15:12 UTC (Wed) by randomguy3 (subscriber, #71063) [Link]

...and you'll find it won't compile. Your examples mostly just made no sense - they worked in an unexpected way because there was really no sane expected behaviour (why should "a" be in any way comparable to 10, for example? What does it mean to add up the contents of a string?).

Not that I don't think that JS is a bit mad in places, or that functional languages aren't a good thing, mind. I just think your examples were somewhat disingenuous.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 15:37 UTC (Wed) by adobriyan (guest, #30858) [Link]

Examples are fine. It is the programming language which allows both "a <= b" and "b <= a" to be true or false simultaneously is not.

Sane programming languages do not have this property.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 15:53 UTC (Wed) by mpr22 (subscriber, #60784) [Link]

"a <= b" and "b <= a" being simultaneously true is fine because of the '=' part; I presume you mean that "a < b" and "b < a" being simultaneously true is indicative of a problem (which it is).

On the other hand, "a <= b" and "b <= a" being simultaneously false is sometimes compulsory (IEC 60559 floating point requires that all comparisons involving NaNs, including comparing a NaN to itself, return false.)

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 19:05 UTC (Wed) by JoeBuck (subscriber, #2330) [Link]

Well, yes; it's OK if < is a partial ordering with incomparible values (there can exist a,b such that neither a<b nor b<a are true).
<p>
But data structures and algorithms that depend on sorting break if there exist a,b,c such that a<b, b<c, and c<a are all true, so warts like this are a significant source of bugs.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 19:13 UTC (Wed) by drag (subscriber, #31333) [Link]

I am a bit confused about all the above.

Especially the parts about using great-than and less-than to compare strings. Trying to program this way seems broken no matter what the language.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 7, 2013 9:25 UTC (Thu) by micka (subscriber, #38720) [Link]

There are orders over the strings. One of them is even a total order, and is even understandable and useful (well, for alphabetic ones, it's sometimes a bit hard to generalize to even US-ASCII, and worse for UTF-8+locale), the lexicographic order (the one you learn when one gives you a dictionary).

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 20:34 UTC (Wed) by khim (subscriber, #9252) [Link]

Your examples mostly just made no sense - they worked in an unexpected way because there was really no sane expected behaviour (why should "a" be in any way comparable to 10, for example? What does it mean to add up the contents of a string?).

Of course the examples made no sense! That's exactly the point!

You see, there are statically typed languages (Java, with some limitations C/C++, etc: if you don't use floating point values you are pretty safe there): errors like these will be detected at compile time and you'll fix them before program will be even started for the first time.

There are dynamically typed languages (Lisp, Python, etc): errors like these will be detected at runtime and end-user will see cryptic error message - embarrassing, to be sure, but not the end of the world.

And finally there are languages like PHP or JavaScript: errors like these will be detected when your applications it cracked and is happily sending spam all over the internet (or, alternatively, when your credit card info is stolen and your account is emptied).

Yes, JavaScript [...] [is] surprisingly good considering the speed at which it was created, I fully agree. It's still a crazy language which would never be used for anything serious in a better world. In our world it must be avoided as much as possible: there are cases where you are forced to use it, but there are no need to propagate this insanity further then necessary.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 2:00 UTC (Wed) by waucka (subscriber, #63097) [Link]

Hint: in C-like languages (like Javascript!), testing for equality is typically done with "==".

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 2:01 UTC (Wed) by waucka (subscriber, #63097) [Link]

Err...never mind. I can't read.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 9:15 UTC (Wed) by epa (subscriber, #39769) [Link]

There are a few restricted subsets of Javascript, sometimes with different syntax, such as CoffeeScript. They fix some of the more obvious warts in the language but still get the benefit of its wide adoption and high quality runtimes. I imagine GNOME programming could be done in one of those.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 19:27 UTC (Wed) by mathstuf (subscriber, #69389) [Link]

I tried CoffeeScript for a bit, but ended up just using JavaScript directly. JS has one rule that I get tripped up on (though much less so these days): the semicolon insertion. I kept getting mixed up with CoffeeScript's whitespacing and other magicallisms that I decided making sure the JS was clean and ensuring "use strict" was being used was going to be less of a burden.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 11:04 UTC (Wed) by canatella (subscriber, #6745) [Link]

Indeed and on the same vain in C

int add(int a, int b) { return a + b; }

has undefined behavior due to a possible signed integer overflow.

If you read the ecmascript spec, you'll see that the language behavior is well defined. Fyi, the <= operator is explained at 11.8.5. It's all clearly documented.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 20:16 UTC (Wed) by Cyberax (✭ supporter ✭, #52523) [Link]

Integer overflow is expected, that's a well-known problem after all.

>Fyi, the <= operator is explained at 11.8.5. It's all clearly documented.
That doesn't make it less bogus.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 21:11 UTC (Wed) by renox (subscriber, #23785) [Link]

> Integer overflow is expected, that's a well-known problem after all.

Yes, well known as it has created quite a few security vulnerabilities!

Javascript's craziness doesn't excuse C's own craziness: undefined behavior for integer overflow by default is a *premature optimization*..

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 22:00 UTC (Wed) by k8to (subscriber, #15413) [Link]

I disagree.

It's sort of a crazy language to the programmer but the cause is not premature optimization, but rather an attempt to be a very thin layer of helpful syntax over the system's native behavior. Since systems behave differently, you don't know what will happen, so it's undefined.

You can argue this was a bad design goal, but that's a different kind of error.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 7, 2013 6:00 UTC (Thu) by khim (subscriber, #9252) [Link]

Since systems behave differently, you don't know what will happen, so it's undefined.

If systems behave differently then it begs for “implementation-defined behavior”, not for “undefined behavior”. Much simpler and safer to deal with these. Indeed, the fact that C has so many “undefined behaviors” is a premature optimization - but the fact that someone made such error many years before is not an excuse to propagate this madness in newer, more modern languages.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 7, 2013 9:24 UTC (Thu) by ncm (subscriber, #165) [Link]

Implementation-defined behavior is worse than undefined behavior. The next version of the implementation will define a different behavior, and your previously well-defined program becomes wrong.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 7, 2013 11:46 UTC (Thu) by khim (subscriber, #9252) [Link]

You assume people write program to the spec. Well, newsflash to you: they don't. The very fact that both GCC and LLVM are trying (and failing) to catch a lot of undefined behaviors shows that people don't write to the spec. People write code, they write tests, if they pass - they are happy. That's it.

If program triggers implementation-defined behavior then tests are usually enough to catch problems (because compiler may do different thing from what the programmer expects but it consistently does different thing) while with undefined behavior compiler is absolutely free to do one thing in one case and completely different thing in another case. Which means that program may suddenly crash after compiler upgrade - and you have no idea why.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 8, 2013 14:41 UTC (Fri) by jwakely (subscriber, #60262) [Link]

> has undefined behavior due to a possible signed integer overflow.

No, the possibility of overflow does not make adding two integers undefined. It only has undefined behaviour if the overflow actually occurs.

Reitter: Answering the question: "How do I develop an app for GNOME?"

Posted Feb 6, 2013 13:49 UTC (Wed) by sorpigal (subscriber, #36106) [Link]

You may not like JavaScript, and it certainly has some issues, but comparing it to PHP is totally uncalled for. JS has a spec, well defined behavior, fairly consistent behavior and a fairly small set of gotchas. PHP is far, FAR worse

# php
function foo(){
return array('a', 'b');
}

print foo()[0]; // SYNTAX ERROR. Intermediate variable required.

// JS:
function foo(){
return ['a', 'b'];
}
alert(foo()[0]); // Of course this works!

The difference in levels of crazy is impossible to under-emphasize!

There are a lot worse languages than JS. PHP is inescapably broken and thoroughly unsalvageable without creating what is effectively a brand new language. All JS needs is typing, a package or module system, and maybe some improvements to its standard library. Its other flaws are no more serious than those of pick-your-favorite-language.

The main issue with JS is that for a long time serious programmers ignored it and as a result it was only used by the ignorant and the short sighted. The second worst problem with JS is the DOM, which really has nothing to do with it. The third is that a lack of packages has made large and inter-connected JS code pointlessly painful. I fail to see what of any of this is as intractable as PHP's broken type system, broken object system, broken parser, etc., etc..

So, please, bash JS all you like, but don't be vulgar and bring PHP in to it.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds