Live data from Hacker News

Traps to Developers

qouteall.fun

51–60 of 113 posts

Re: Traps to Developers

#51
Largely a good listicle. Some feedback:

> Unicode unification. Different characters in different language use the same code point. Different languages' font variants render the same code point differently. 語

This isn't a trap. The given example character means the same thing in Chinese and Japanese, and the Japanese version was imported from China. People from both languages recognize both font variants as the same conceptual character.

The author is making it sound like the letter 'A' in English should have a different code point than an 'A' in French. Or that a lowercase 'a' with the top tail should be a different character than a lowercase 'a' without the top tail.

Anyway, this is discussed at length in https://en.wikipedia.org/wiki/Han_unification

> There is a negative zero -0.0 which is different to normal zero. The negative zero equals zero when using floating point comparision. Normal zero is treated as "positive zero".

And there are two ways to distinguish negative zero from normal zero: By their integer bit patterns, or by the fact that 1.0/-0.0 == -Inf vs. 1.0/0.0 == +Inf.

> It's recommended to configure the server's time zone as UTC.

Big yes. I use UTC for servers, logs, photos, and anything that is worth archiving and timestamping properly. Local time is only for colloquial use.

> For integer (low + high) / 2 may overflow. A safer way is low + (high - low) / 2

Yes, but if low and high could be negative numbers, then you've just shifted the overflow to a different range. This matters for general binary search over an integer range, as opposed to unsigned binary search over an array.

> C/C++

I'm going to throw in one of my lists of pitfalls - just using integer types and arithmetic correctly in C/C++ is a massive developer trap. That's like the most basic thing in programming. https://www.nayuki.io/page/summary-of-c-cpp-integer-rules

> Rebase can rewrite history

"Can" is a weasel word; rebase does nothing but rewrite history.

Re: Traps to Developers

#52
post #25

> Java, C# and JS use UTF-16-like encoding for in-memory string That’s incorrect for Java, possibly also for C# and JS. In any language where strings are opaque enough types [1], the in-memory representation is an implementation detail. Java has been such a language since release 9 ( https://openjdk.org/jeps/254 ) [1] The ‘enough’ is because some languages have fully opaque types, but specify efficiency of some opera…

Yeah, I think they didn't mean max "accurate" integer and rather meant max "safe" integer.

Re: Traps to Developers

#53
post #25

> Java, C# and JS use UTF-16-like encoding for in-memory string That’s incorrect for Java, possibly also for C# and JS. In any language where strings are opaque enough types [1], the in-memory representation is an implementation detail. Java has been such a language since release 9 ( https://openjdk.org/jeps/254 ) [1] The ‘enough’ is because some languages have fully opaque types, but specify efficiency of some opera…

> possibly also for C# and JS The representation for C# is very much fixed, as it allows, and very commonly uses, direct access into the string buffer as a ReadOnlySpan or a raw char pointer, where char is the type of UTF-16 codepoints. JS could maybe get away with it.

When you have code that works a lot with strings the cost overhead of building an app on iso-latin-1 but encoding as utf-16 can be substantial.

I think Java moved away from this back around 8, or possibly 9.

Re: Traps to Developers

#54
post #43
post #37

> A method that returns Optional may return null. projects that do this drive me bananas If I had the emotional energy, I'd open a JEP for a new @java.lang.NonNullReference and any type annotated with it would be a compiler error to assign null to it public interface Alpha {} @java.lang.NonNullReference public interface Beta {} Alpha a = null; // ok Beta b = null; // compiler error javac will tolerate this Beta b; if…

I question the wisdom of even having Optional in a language with nulls. It would raise some eyebrows if a function in Python returned an Optional type object rather than T | None. You have to do a check either way unless you're doing some cute monad-y stuff.

There's a lot of quality-of-life stuff enabled by it in Java, since the base language's equivalents to Optional.empty(), Optional.ofNullable(...).orElse(...), etc are painfully verbose by comparison.

Re: Traps to Developers

#55
post #25

> Java, C# and JS use UTF-16-like encoding for in-memory string That’s incorrect for Java, possibly also for C# and JS. In any language where strings are opaque enough types [1], the in-memory representation is an implementation detail. Java has been such a language since release 9 ( https://openjdk.org/jeps/254 ) [1] The ‘enough’ is because some languages have fully opaque types, but specify efficiency of some opera…

> > Java, C# and JS use UTF-16-like encoding for in-memory string

>

> That’s incorrect for Java,

Maybe so, technically, but if you Base64 encode a string in a language that uses UTF-8 (or another UTF-16 with another endian) and decode it in Java, Java's UTF-16 representation will be the problem you will be dealing with.

Re: Traps to Developers

#56
post #38

> Unset variables. If DIR is unset, rm -rf $DIR/ becomes rm -rf /. Using set -u can make bash error when encountering unset variable. sweet mercy :O Someone call the Inquisition

Instead, say rm -rf $DIR That is, skip the trailing slash. Then if $DIR is not set, it becomes an invalid command, because no file names were supplied.

Better to make the requirement explicit, instead of relying on the argument-parsing details of rm or some other command:

    # Default message
    $ rm -rf "${DIR:?}"
    bash: DIR: parameter null or not set

    # Custom message
    $ rm -rf "${DIR:?It is not set OMG}"
    bash: DIR: It is not set OMG

Re: Traps to Developers

#57
post #34

Does anyone truly understand all the little edge cases with CSS? I've write tons and tons of CSS, have done for a decade. I don't sit and think about the exact interactions, I just know a couple things that might work if I'm getting something unexpected. I don't really see it possible to commit that to memory, unless I literally start working on an interpreter myself.

I think there can be a different way to think about CSS that can help with that feeling of never understanding it all. Recently I’ve heard people influential in the CSS world describe it as a “suggestion” to the browser. The browser has its own styles, the user might have some custom stylesheet on top of the browser’s version, extensions, etc etc and at some point CSS is really more a long list of “suggestions” about…

But you need to, you know, actually float something in a text. I think to do it with flexbox/grid you need JS that calculates heights and than manually splits the text into boxes with heights, so essentially you are doing rendering.

Also is there another way to position boxes side-by-side in an inline context without float?

Re: Traps to Developers

#58

Earlier quoted context omitted.

What's the trap for this one? I can't think of any engine that parses this to mean anything other than the letters a through z.

In some common implementations if $LANG is set to certain values, it will fail to match some ASCII letters. This is because not all latin character using languages put Z last in the alphabet. Try this (you probably need to enable and generate the locale first) echo y | LANG=lv_LV.UTF-8 grep '[a-z]' Locales in general should be considered a "trap", just look at Windows CSV separator handling, etc.

Not in general, but using locales for something different than affecting presentation.

Re: Traps to Developers

#59
post #29

Earlier quoted context omitted.

What's the trap for this one? I can't think of any engine that parses this to mean anything other than the letters a through z.

[A-z] though is a fun one though as it includes a few extra symbols between upper and lowercase.

Does it? I thought Regex are defined on character classes not on numeric ASCII values. What would a Regex do on a different encoding then?

Re: Traps to Developers

#60
post #18

Earlier quoted context omitted.

CSS cascade for text properties more or less makes sense. I have been unable to comprehend CSS layout from any perspective: page designer, implementer, user, anything. It must have someone in mind but I have no idea who I that is.

https://every-layout.dev has by far the best explanations and coherent usage of CSS I've encountered since I started doing webdev for a living in 1998.

Every Layout changed how I look at and do CSS. Great resource with a good philosophy behind it: CubeCSS. It really made CSS fun for me again.
Post reply on HN