Live data from Hacker News

The type system is a programmer's best friend

dusted.codes

321–330 of 467 posts

Re: The type system is a programmer's best friend

#321
post #314

Earlier quoted context omitted.

There isn't a reason why you shouldn't write something like $100 * (£20 / £47) as "int dollars = 100 * 20 / 47;". Note that this expression assicates to the left instead of the right, which can be the right thing to do if doing integer arithmetic. But it would not work with a strongly typed setup as in your example. In my experience trying to prevent accidental mistakes is a waste of time and often makes our lives mi…

But why would you use integer arithmetic when dealing with fractions?

It was just the first example from the top of my head. The expression above calculates the right thing, cast to int. In general, prescribing which units we can multiply and which not, is extremely silly if you consider how we learn it in school. You can multiply anything and everything, simply take care of the units. There isn't an obvious reason why we couldn't have 2000 dollar-pounds as a transient value in a longer computation.

The real problem is that most type systems aren't fit to track the units automatically. Solution: Don't beat yourself up, track the units in your mind / in comments / in variable names instead of the type system. And just get it right. It's not that hard - if you mix something up that's usually the type of bug that is immediately noticed and fixed.

Re: The type system is a programmer's best friend

#322

> A string value is not a great type to convey a user's email address or their country of origin. These values deserve much richer and dedicated types. I want a data type called EmailAddress which cannot be null. Sure, I'm on board: I also want an e-mail address type. Just not in your shit language in which something can be of type String, yet be null reference.

Are there any widely used statically typed languages where there is no such thing as null? Wish I could find a job using one of those!

Hmm, oh! C++ comes to mind. Of course, there is such a thing as null, but in:

   void fun(string x)  // std::string
   {
   }
x cannot be null. The reference semantics (like a copy of a string sharing the data with the original) is an implementation detail/optimization encapsulated inside what appears to be a value type.

The tools are there in C++ to create ideal types for your problem domain which just look like value types that have no null domain value (or any other value you don't want), and standard strings are like this.

Re: The type system is a programmer's best friend

#323

You know those "how to draw Bugs Bunny" art guides they used to include in children's art books? Where they begin with a circle with some guidelines and then do a whole bunch of stuff and the end result is Bugs Bunny? But you have no idea how they went from Point A to Point B? That's the same thing with Type Theory. PROFESSOR: Well, you see, there are different objects, like strings and numbers, that are shaped diffe…

If you actually pay attention to the “a whole bunch of stuff”, maybe you'll understand the latter statement.

Re: The type system is a programmer's best friend

#324
post #302

Earlier quoted context omitted.

"Just use it" is what lead to the big SQL injection fallout and even today we pay the price as not even a year ago thousands of crucial service were vulnerable via log4j because of the "Just Use It" mantra.

You don't avoid injection attacks by validation, but by escaping.

escaping requires validation. You don't know what to escape if you aren't allowed to validate.

Re: The type system is a programmer's best friend

#325
post #311

Earlier quoted context omitted.

I don't understand. What should you get instead?

The commenter you're replying to expressed it confusingly. The point is that in Go, 5 * time.Milliseconds(2500) is a type error, and instead you need to do time.Nanoseconds(5) * time.Milliseconds(2500).

`5 * time.Milliseconds(2500)` is not a type error, though `int(5) * time.Milliseconds(2500)` is.

(This is especially relevant because you really mean `5 * (2500 * time.Millisecond)` vs. int(5) * (2500 * time.Millisecond)`, as there is no `time.Milliseconds` function.)

Re: The type system is a programmer's best friend

#326
post #302

Earlier quoted context omitted.

You don't avoid injection attacks by validation, but by escaping.

escaping requires validation. You don't know what to escape if you aren't allowed to validate.

You don't know what you're talking about. For example, HTML escaping (a.k.a quoting) rules don't care what you're escaping - an email, a street name. It's just text.

And that's the point of it. You quote precisely because the container syntax doesn't know the syntax of what you're embedding. If it knew, there would be no need of the escaping.

It's called abstraction.

Re: The type system is a programmer's best friend

#327

> I want that data type to have helpful methods such as .Domain() or .NonAliasValue() which would return gmail.com and foo@gmail.com respectively for an input of foo+bar@gmail.com. No the hell you don't. Please please please do not attempt to separate the alias from an email address I submit. It's there for a reason - specifically, to hold you accountable if I experience a sudden influx of spam, and generally to keep…

> "recognize that trying to parse any meaning from an email address' local-part is blatantly ignorant of IETF specifications and almost certainly will create bugs" I am sorry but this makes no sense. You do realize that the only reason you are able to use aliases is because your email provider chooses to parse meaning out of the supposedly "opaque" text right? If your email provider is free to "break" the spec, so ar…

[deleted]

Re: The type system is a programmer's best friend

#328

Earlier quoted context omitted.

I'm adverse to debuggers as i've more than once caught myself following a rabbit hole of steping through code instead of thinking. IDEs have some use, and static analysis has proven to catch the same mistake over and over, but only as the authors of those tools have discovered that false positives cannot be allowed ever, once there is a false positive the tools is worthless.

I find debuggers more valuable with dynamically typed languages, especially Python. It's handy to be able to drop a `breakpoint()` in the middle of a script when you have no idea what a function is actually returning. The happens more often than you might think.

Of course, when this happens the bug you are hunting is not your only problem. You really should clean up the code to make it clear what is being returned from the function.

Re: The type system is a programmer's best friend

#329

> I want that data type to have helpful methods such as .Domain() or .NonAliasValue() which would return gmail.com and foo@gmail.com respectively for an input of foo+bar@gmail.com. No the hell you don't. Please please please do not attempt to separate the alias from an email address I submit. It's there for a reason - specifically, to hold you accountable if I experience a sudden influx of spam, and generally to keep…

True enough, as far as it goes. But if you are concerned about subscribing to something twice, you may want to try to check delivery uniqueness. They might be your own addresses. Of more interest to me, omitted from the presentation--as almost always--is anything about what is disliked about a malformed address. You see this when some web form says it doesn't like your address, but won't say why, leaving you to guess…

> Another example is the password filter that idiotically demands "at least one capital letter, one digit, and one swear character" in your already several-word passphrase, and dislikes your choice of swear characters but won't say so.

  1> (jp-hash "correct-battery-horse-staple")
  "Pyochu1ponu*fuson"
https://addons.mozilla.org/firefox/addon/jp-hash/

Re: The type system is a programmer's best friend

#330
post #311

Earlier quoted context omitted.

The commenter you're replying to expressed it confusingly. The point is that in Go, 5 * time.Milliseconds(2500) is a type error, and instead you need to do time.Nanoseconds(5) * time.Milliseconds(2500).

`5 * time.Milliseconds(2500)` is not a type error, though `int(5) * time.Milliseconds(2500)` is. (This is especially relevant because you really mean `5 * (2500 * time.Millisecond)` vs. int(5) * (2500 * time.Millisecond)`, as there is no `time.Milliseconds` function.)

Thanks, I don't remember exactly how it worked. It doesn't take away from the stupidity.
Post reply on HN