Hacker News new | past | comments | ask | show | jobs | submit login
My Reality Moment. Why did I ever agree to do this? (intel.com)
46 points by rubikscube on Sept 18, 2015 | hide | past | favorite | 40 comments



Interesting article though it feels a bit incomplete. The writer very briefly introduces this concept of a "reality moment" , gives an example and that's that. The example is interesting and the concept rings true; I'd like to see more time spent discussing the concept in and of itself.

The only time I remember seeing this addressed outside of a "programmers are bad at estimating tasks so do x to get better estimates" context was a post by Jeff Atwood about the tendency of programmers to look at something like StackOverflow and see, basically, two CREATE TABLE statements[1]. The reality moment, then, sounds like the moment one sees past that "weekend worth of coding". A bit more fleshing out, maybe with an examination of why we think this way would be a great read.

[1]: http://blog.codinghorror.com/code-its-trivial/


Reality moment usually sets in a bit late for me. When I used to get a new job or contract I used to have incredible initial enthusiasm. Even three months in dealing with spaghetti code and inexperienced programmers I think to myself improvement in my working environment is around the corner and I'm going to be able to fix things myself. And then the reality moment sets in. Why did I agree to this In the first place - I'm working in a terribly unorganised company, being underpaid working with inexperienced programmers who never has a chance to improve properly because the company was too stingy to hire enough good and experienced programmers. But then I look at the state of things - they have been improving because I kept thinking I could fix it, the only issue is I should have been paid more as I've done a lot for the company. Stressed and frustrated I go looking for a new job. The process grants me a big boost of enthusiasm. I interview at a company that really likes me. I see some red flags with the way they produce software, but I ignore them and bury them in the back of my mind. "Do I stay at my place where my effort is having an effect or do I go to this new company?" This new company sounds so exciting!

This time I'm thinking I might be happier taking this job offer back to my current company and get a good pay rise, but I'm not 100% decided yet.


We need creativity mixed with discipline to make a difference here.

Or we could pick a non-insane language to standardize on.

Even better, we could pick a non-insane bytecode to standardize on.


Like WebAssembly (technically not a bytecode).


As a compiler developer I honestly shudder reading this. From the last example:

> $ node

> "5" + "1"

> '51'

> "5" - "1"

> 4

How is this in anyway reasonable behavior? '+' is string concatenation but '-' is interpret these strings as numbers and perform arithmetic?

Does the principle of least surprise apply here?


If you haven't seen the Gary Bernhardt "Wat" talk [1], I think you'll very much appreciate the sentiment.

https://archive.org/details/wat_destroyallsoftware


That video was amazing, thank you.


It's quite bad, and that's been pretty universally agreed upon, but back in the 90s we didn't think it would be too bad. It turns out in javascript it isn't actually that bad because you can write out a short list of very unambiguous rules to completely avoid loose-typing issues. And virtually everyone except people who just picked up the language follow these rules religiously.

My impression is that in PHP-land it's very hit-or-miss. Some developers adhere to strict guidelines to avoid these kinds of problems and others don't.

Perl programmers generally avoid loose-typing when writing libraries and larger programs, but not while writing scripts for personal use, which I think is a pretty reasonable attitude to have.


Eh I don't know if you can blame it on the 90s. I remember using VB and being utterly paranoid because I had no idea what it might end up doing in the name of being helpful.


>'+' is string concatenation but '-' is interpret these strings as numbers and perform arithmetic?

That's precisely it.

You cannot subtract a string so it coerces "5" and "4" to integers instead of treating them as strings. So it results in 5 - 4.

The first concatenates two strings, "5" and "1" and results in "51".

It's perfectly reasonable behavior. If you want to add numbers you should give it integers - namely 5 and 1 instead of two strings "5" and "1".

The unreasonable behavior is giving integers as strings.


>> It's perfectly reasonable behavior.

I get what you're saying literally, I strongly disagree that it is reasonable, at all. If you can't subtract them, there should be an error at that point, telling the programmer it can't be done.


It's unreasonable for the programmer to give integers as strings.

It's unreasonable for the computer to take integers given as strings and subtract them as integers anyway.

The former doesn't preclude the latter.


You cannot subtract a string

Maybe in Javascript you can't, but you sure could design a language where you could subtract strings, if you wanted to. You could treat the strings as two arrays of characters and subtract them. The result would be the characters that are in the first string but not in the second string. "Hello" - "o" would be "Hell".

I'm sure you could do this in Ruby by monkeypatching the string class to overload the minus method.


> The result would be the characters that are in the first string but not in the second string

A result would be that, but I certainly would not call it the result.

Another result would be to treat the strings as byte arrays and subtract one byte from another, e.g. [a1,a2,a3] - [b1,b2,b3,b4] would result in [a1-b1, a2-b2, a3-b3, -b4]. I'm sure you could do any number of different "correct" operations. And then it's up to you, as a language designer, to decide what you think string-minus-string should actually do... and there is zero probability that everybody in the programming community will agree with your decision.


How would your method subtract "lo" from "Hello" and leave "Hel" instead of "He"? It gets complicated enough that I think you'd be re-implementing a more limited version of regex.

Also I was speaking in context of Javascript so I fail to see how this applies.


More of a general observation about how arbitrary the type juggling rules and operator overloadings in programming languages are.


The unreasonable behaviour is this not being an error.


I'll freely admit I'm not a fan of dynamic languages (mostly due to hacking the Erlang HiPE compiler) and in a previous life I was a Haskell guy. So I'm a fan of strict typing.

> The unreasonable behavior is giving integers as strings.

That I can agree with. But in that case this notion of reusing '+' as string concatenation but '-' is coercing strings to integers is utterly bizarre.

I'm afraid to ask what '*' does (string replication?) and '/' "space magic"?


I absolutely prefer strict typing myself. The issue becomes whether or not one thinks type coercion should be a thing.

I'm personally against type coercion - but I also understand how it functions (and why it functions). I disagree with the "why" but as long as I understand the "how" I can avoid gotchas like string coercion. :)

Multiplication ( * ) and division ( / ) coerce to integers. Only + is used for strings, particularly concatenation. Personally I'm a fan of & being used for string concatenation and can't think offhand why not to use it, other than slight possibility of confusion over &&. However I'm not aware of a language that uses it.

  >>"5" * 5
  25
  >>"5" * "5"
  25
  >>"5" / 5
  1
  >>"5" / "5"
  1


> I absolutely prefer strict typing myself. The issue becomes whether or not one thinks type coercion should be a thing.

As a strong typing fanatic, I'd rather have the interpreter/compiler scream at me. In my opinion automatic type coercion is slightly less wrong than undefined behavior in C.

(Aside, did you know that if GCC spots a statically provable null pointer deference, it will insert __builtin_trap() after the deference? I didn't know either until yesterday. This is acceptable because undefined behavior can mean "eat you hard drive".)

If for strings '+' is concatenation, and '*' is for replication with '-' and "/" as exception behavior I can fully understand. (Admittedly '+' over '&' is either style or hard-core "the operation is different, therefore we need a different symbol choice").

Your example worries me. Personally I would not design a language like that.

(Though I may have abused it in the past.)


>Admittedly '+' over '&' is either style or hard-core "the operation is different, therefore we need a different symbol choice

The former more than the latter. The operation is different therefore we need a different symbol choice - and & also makes as much sense stylistically as + does.

"string1" and "string2", when read, makes sense. & is different from the conditional && like piping | is different from the conditional or ||

>Your example worries me. Personally I would not design a language like that.

Neither would I, but I like to think it was part of "made in 10 days" that allowed it to stick around.


> "string1" and "string2", when read, makes sense. & is different from the conditional && like piping | is different from the conditional or ||

Of course, in languages where & and | are already in use for bitwise operations on ints (etc.), using & for string concatenation would be a different form of the same clash as using + is.


> I'm afraid to ask what '*' does (string replication?)

In Python it does. But then, Python has strong types and won't coerce the strings to numbers for '-', what makes this sane.


What possibly value is there in coercing strings to integers for _some_ operations but not _all_? I can see rationales for always doing it or never doing it, but this seems like the worst of both worlds.


The rationale is that + is defined for strings, but - is not.


That's less a "rationale" and more a "cause," though. If adding quoted integers always treats them as strings, never integers, then allowing coders to subtract quoted integers as integers can only possibly lead to confusion. One or the other should be removed entirely (probably the second, but what do I know).


Our Java backend serializes strings that look like numbers as numbers in JSON. When the ObjC code deserializes, we have properties that might be the NSString we expect, or an NSNumber just because the user entered all digits.

I wonder if the Java library was comfortable doing that because of JS's quirks with types. I think it's still bonkers behavior though.


Even better, +"5" will coerce the string to an integer. I'm ashamed to admit that I've abused it.


To quickly cooerse to a boolean:

    !!0 === false; !!"zipzap" === true; !!NaN === false
To cooerse to a integer (not float):

    ~~0.5 === 0; ~~"12zipzap" === 0; ~~(Math.PI) === 3;
I tend to avoid it for readability (for other developers)


And those kinds of issues are why Perl has ==/!=/+ for numbers and eq/ne/. for strings.


Those are great. I wish PHP had borrowed eq and ne. It does have . for strings, which makes things a little easier.

Since JS doesn't have any of those string operators, you end up with the fun example that was posted in the article. It will default to using + to concatenate the strings, but - is not defined to strings, so it converts them to numbers first and then applies the operator. Easy to remember once you know what's going on, but still irrational.


> But the usage of "===" is fairly uncommon in most PHP programs.

I don't code PHP anymore, but the use of "===" has been standard practice for ages, and checking for it fully automated.

It's one of the many areas in which uninformed people like to criticize PHP for bad practices that in reality have been left behind a long time ago and are only still supported by the language for backward compatibility reasons.


I only have experience in dynamic languages. I don't get the point of the article? Is the article implying type coercion is bad because its inefficient in terms of CPU cycles? In a decade of programming in dynamic languages, I've never had string comparison be a bottleneck. It is usually IO that is the bottleneck, and the trade off is that by using dynamic languages like JS it becomes easy to make your IO non blocking. If you really have some logic that needs to compare millions of strings, you could always write that algorithm in a static compiled language, and shell out to it from your dynamic language.


> I don't get the point of the article?

The point -- as is often the case -- is precisely stated at the end of the article: "What this boils down to is that when we approach any of these server dynamic languages to optimize, we can't employ the same tried-and-true techniques we have used for decades (literally) in C, C++ and Java. We need creativity mixed with discipline to make a difference here."

Its not making a point of bad or good. Its discussing the features of the languages that make optimizing them challenging.


What's so special about JS's dynamic side that makes async IO easy? Any functional language should be able to do so. And even better than JS, as they may have syntax to avoid all those nested callbacks, while still flowing the environment/error handling.

In fact, JS doesn't seem to have anything that's specifically suited for async IO at all, apart from clumsy closures.

Also, string handling can often be a bottleneck. Anything dealing with HTTP the protocol, for instance, will have all sorts of crazy hacks to parse that messed-up text format as fast as possible. (And this is a major reason why HTTP/2 isn't text-based.)


12 years ago, I was working on a PHP library to parse XMLRPC requests, and I ran into it's numeric type slipperiness in a painful way. I was (naively) using PHP's eval to parse a decimal numeric string.

I noticed what seemed like random numeric errors in a random subset of RPCs. It turned out, after what seemed like eons of investigation, that some clients were stringifying decimal integers with leading zeros, which made PHP interpret them as octal numbers, and hence the random seeming errors.

Having come from a strongly-typed-language background (C/C++/Java), that was a major learning experience about the caveats of weakly typed languages.


What does typing have anything to do with this 'feature' in the number parser? If atoi in C also parsed octal numbers, you would have had the same problem.

Your issue is with the too liberal number parser.


It was a tangentially related personal anecdote from 12 years ago. You are welcome to ignore it if you find it irksome.



It wasn't dynamic (vs static) typing that was horrifying the author, it was weak (vs strong) typing.

The unsurprising strongly typed Python example was still dynamically typed.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: