spotifyovercastrssapple-podcasts

If It Compiles It Works

What do we really mean when we say "if it compiles it works?" And how do we make sure our apps stay that way?
January 17, 2022
#48

Transcript

[00:00:00]
Hello, Jeroen.
[00:00:01]
Hello, Dillon.
[00:00:02]
So, what are we talking about today?
[00:00:07]
Today we're talking about a sentence that we hear a lot in Elm, which is, when it compiles,
[00:00:13]
it works.
[00:00:14]
And it is a good question.
[00:00:16]
If it compiles, does it work?
[00:00:18]
Yes.
[00:00:19]
Well, I just said so.
[00:00:20]
All right.
[00:00:21]
There we go.
[00:00:22]
End of the episode.
[00:00:23]
End of the episode.
[00:00:24]
That was a quick one.
[00:00:28]
So I posted an article about this, and there were some interesting conversations, and it
[00:00:33]
seemed to spark some ideas about, you know, I mean, it's a bold statement to say, if it
[00:00:38]
compiles, it works.
[00:00:40]
And obviously, if you write a main.elm, and then you get it compiling, is whatever application
[00:00:48]
you were trying to build now suddenly a completely functioning application with zero bugs and
[00:00:53]
all of your logic implemented?
[00:00:56]
Maybe not.
[00:00:57]
Maybe not?
[00:00:58]
If you type, text, hello world, is it the new Facebook?
[00:01:02]
No.
[00:01:03]
Sorry, the new meta.
[00:01:06]
That's right.
[00:01:07]
That's right.
[00:01:08]
Yeah.
[00:01:09]
So, clearly, there are limits to what that actually means when we say, if it compiles,
[00:01:15]
it works.
[00:01:16]
Well, what does it mean then?
[00:01:17]
Right.
[00:01:18]
There's the interesting question.
[00:01:20]
So I think that, I think we can agree that it doesn't mean that it's impossible to have
[00:01:26]
bugs and that your business logic will be working flawlessly.
[00:01:31]
But I think we can also agree that it does mean that, for one thing, your types are correct.
[00:01:38]
There's no way around that.
[00:01:40]
Yeah.
[00:01:41]
Unless you want to be really tricky.
[00:01:44]
No, I was going to say, like, the types are correct, but it doesn't mean that it works.
[00:01:51]
Right.
[00:01:52]
Well, it depends on what we mean by works, doesn't it?
[00:01:54]
Yeah, yeah.
[00:01:55]
But what I've been thinking is one thing that, like, when we think about that actual feeling,
[00:02:03]
that emotion of it works, that you get a little, you know, clean compiler message.
[00:02:11]
The screen is, you know, the compiler error that was on your dev window goes away, and
[00:02:15]
suddenly, the application is there.
[00:02:17]
You get that little dopamine hit because it's compiling again.
[00:02:20]
And you try it out, and it seems to behave exactly like you expected?
[00:02:24]
Yeah, what do we mean by that when it works?
[00:02:26]
Right?
[00:02:27]
Because we, you know, a lot of us in the Elm world have experience with a different programming
[00:02:33]
language where perhaps we run it, we don't have any syntax errors, but we have to go
[00:02:40]
through all of this tedium to manually test things and, you know, hopefully automate a
[00:02:48]
test.
[00:02:49]
But still, we don't feel confident that we've covered every case because we have to exercise
[00:02:53]
all these code paths to trust them.
[00:02:55]
To trust now, so to me, that says a lot about wiring.
[00:02:59]
I trust wiring, whereas in certain other programming languages, I don't trust wiring until I see
[00:03:07]
it for myself, until I've actually exercised that code path.
[00:03:10]
So I'd say that wiring is a big one.
[00:03:15]
If we stick to the definition, my definition is like, whenever you change Elm code, either
[00:03:22]
by doing refactoring, which usually means that the behavior has not changed, or when
[00:03:29]
you add new behavior, or when you change the behavior, then once your code compiles, it
[00:03:36]
behaves like you expected it to.
[00:03:39]
Doesn't mean that there's no bugs, but at least it does what you expected it to when
[00:03:44]
you sit down to write it.
[00:03:47]
That's my take on when compiling works.
[00:03:50]
Right.
[00:03:51]
That means, so if you do a large scale refactoring, that when you finally get to the end of the
[00:03:58]
list of errors that you initially received, and your code compiles, then it does work
[00:04:05]
exactly like it did before.
[00:04:08]
Right.
[00:04:09]
Right.
[00:04:10]
I like that train of thought.
[00:04:13]
If I could be a bit pedantic here, just to poke a little fun.
[00:04:20]
You said if, when we do a large refactoring, which usually means that we didn't change
[00:04:25]
the behavior, I would say it always means that if you use the word refactoring, because
[00:04:31]
that's what refactoring is, often people use that term imprecisely to mean changing the
[00:04:39]
code and the behavior.
[00:04:40]
But refactoring means changing code without changing behavior.
[00:04:45]
By definition.
[00:04:46]
I absolutely agree.
[00:04:47]
That's why he additionally added it.
[00:04:51]
Right.
[00:04:52]
Right.
[00:04:53]
And the reason I indulged in being a little bit pedantic there, for one thing, is as a
[00:04:59]
public service announcement, don't mix your refactorings with your behavior changes.
[00:05:05]
It's a really, I believe, a very important practice.
[00:05:09]
But another reason is I think that might illuminate a little bit about this experience you're
[00:05:16]
talking about where you go in and you refactor code or you change behavior separately from
[00:05:24]
refactoring and it does something that you expected.
[00:05:30]
I think that when things like wiring and some of these other things that we can get into
[00:05:34]
get out of the way, that you know you can trust those things, then there's less to think
[00:05:41]
about.
[00:05:42]
Just like when you're refactoring and you're only changing behavior and you're taking
[00:05:45]
small steps, you can reason about what's happening.
[00:05:48]
Like if you're in a math class and somebody says, hey, here's this giant formula and here's
[00:05:56]
another way we can write this and they just wave their hands.
[00:06:00]
You can't do that.
[00:06:01]
They say show your work.
[00:06:03]
And what does show your work mean?
[00:06:04]
It means break it down into manageable steps so that I can trust each step and then see
[00:06:10]
how it derives at that result.
[00:06:12]
And to me, that's what a good refactoring looks like too.
[00:06:15]
It's showing your work, doing each small step in a way that it's manageable for your brain
[00:06:21]
and you can follow what's going on.
[00:06:24]
Yeah, it's verifiable as well by others.
[00:06:27]
Right, right.
[00:06:28]
It's verifiable and you can trust it.
[00:06:31]
And so to me, because of these characteristics in Elm, it can get out of our way and we can
[00:06:38]
think about a change because we're not thinking about wiring.
[00:06:42]
We're not thinking about, wait a minute, I changed this thing, but am I passing the right
[00:06:47]
type through here?
[00:06:49]
We're not thinking about that.
[00:06:51]
And that freezes up to think about these other things.
[00:06:54]
So I think removing that noise is a really big thing.
[00:06:58]
So you know that I'm also an adept of tiny steps, but I don't think it really matters
[00:07:07]
all that much when we talk about when a composite works in the sense that the big aha moments
[00:07:14]
that I had with this when a composite works were when I did not do tiny steps.
[00:07:19]
When I did large scale refactorings, the things that took me hours before I learned how to
[00:07:25]
do tiny steps and it still worked.
[00:07:29]
Yeah, right.
[00:07:30]
But tiny steps definitely help that for sure.
[00:07:35]
Right, right, right.
[00:07:37]
I know what you mean.
[00:07:38]
And I do those big steps sometimes too.
[00:07:44]
I try to manage making small steps, but sometimes you do a refactoring and you can sort of see
[00:07:50]
the path.
[00:07:51]
And sometimes it's just very obvious.
[00:07:53]
You're changed.
[00:07:54]
I mean, sometimes you're just going to change the underlying data model of something and
[00:07:58]
you just go through and you change it in a whole bunch of places and you know it's just
[00:08:02]
going to work.
[00:08:03]
You were passing in, you know, a list before and now you're passing in a non empty list
[00:08:10]
with a tuple of value and a list of values or something and you just wire that through
[00:08:15]
a whole bunch of places and then it just works.
[00:08:17]
And there are certain ways to do those sweeping changes, but as a small step.
[00:08:23]
And I think I think you can still count that as a small step in a way where it touches
[00:08:27]
a lot of places.
[00:08:28]
Well, yeah, I guess it's when it's the smallest possible step.
[00:08:33]
Right, right, right.
[00:08:35]
And sometimes that can be like a focused change that affects a lot of places.
[00:08:38]
So like what what is it that makes those changes feel safe?
[00:08:45]
So do you want to talk about when the how does work?
[00:08:50]
Yeah.
[00:08:51]
You know what?
[00:08:52]
Okay.
[00:08:53]
Yeah.
[00:08:54]
Like what?
[00:08:55]
What is it that makes that feel feel like something we can trust?
[00:08:58]
So the reason why it works when it compiles is first of all, because there is a compiler
[00:09:05]
that blocks you as long as there are errors.
[00:09:09]
So if you do the same kind of changes in other languages, let's say JavaScript, and you make
[00:09:16]
a lot of changes.
[00:09:17]
The thing is, you don't know when you're done.
[00:09:21]
Right.
[00:09:22]
And in Elm, or with a compiled languages with good compilers, let's say, you cannot stop
[00:09:29]
earlier, maybe slightly early, but there's a lot of issues, especially as you said, like
[00:09:36]
wiring that you have to address, and that you could totally forget to do in JavaScript
[00:09:41]
and other languages.
[00:09:43]
And I think that's probably the main thing.
[00:09:46]
Then why is it that we get compilers is a different issue.
[00:09:52]
Right.
[00:09:53]
That's a great point.
[00:09:54]
So there's, yeah, it's almost like the difference between it compiles so it works or it runs
[00:10:00]
so it works.
[00:10:01]
And, you know, I mean, I talked about this in my types without borders talk that you
[00:10:06]
take Elm decoders versus working with JSON objects in JavaScript, where you're just going
[00:10:13]
through picking off values, passing them through places.
[00:10:18]
And what Elm decoders make you do is validate and parse those validations into data types
[00:10:26]
to as a result of those validations.
[00:10:28]
So you're checking those assumptions at the gate.
[00:10:32]
And if those assumptions are not true, you're finding out early.
[00:10:36]
So I think that does have something to do with it.
[00:10:38]
Now if you, you know, if you use this types without borders approach, the idea is that
[00:10:42]
we can move that even further to the left of instead of knowing as soon as you receive
[00:10:49]
a server JSON response, whether it's valid, you know, at compile time.
[00:10:55]
But either way, moving that further to the left means that you don't have to run every
[00:11:00]
single code path to test those assumptions.
[00:11:03]
Whereas if you're just picking off raw JSON values and passing them through, that means
[00:11:08]
that's like a deferred problem that you're finding you're passing values through and
[00:11:13]
you didn't actually validate them upfront.
[00:11:16]
So the errors can trickle through the system.
[00:11:18]
And that makes it very hard to trust code unless you actually run through the full code
[00:11:24]
paths.
[00:11:25]
So I think that's an important piece of it.
[00:11:27]
Yeah.
[00:11:28]
So for instance, some fields that you didn't decode in JavaScript could be null and you
[00:11:34]
only notice that at the end and you should have decoded or handled it differently.
[00:11:39]
Yeah.
[00:11:40]
So having to deal with those things upfront is really makes it feel different working
[00:11:46]
with Elm code.
[00:11:48]
And it gives you a lot more confidence when you get that compiling screen, the compiler
[00:11:52]
error goes away.
[00:11:54]
You really like my feeling working with JavaScript was when the syntax errors and everything
[00:12:01]
go away and the server live reloads and I'm looking at a page of running code.
[00:12:08]
It's not a feeling of relief.
[00:12:09]
It's a feeling of, all right, time to go through and see if this actually works and time to
[00:12:16]
run a bunch of tests and write a bunch of new tests.
[00:12:19]
Yeah.
[00:12:20]
Like the syntax errors are like the, Oh, okay.
[00:12:22]
I know that I still have errors.
[00:12:24]
I know where to find them and how to fix them.
[00:12:28]
But once you're past that, now it's time to find the tedious errors.
[00:12:32]
Exactly.
[00:12:33]
Exactly.
[00:12:34]
That's right.
[00:12:35]
And so now, I mean, what's the point of talking about this?
[00:12:41]
Are we trying, you know, like people understand we like Elm.
[00:12:45]
So, so why do you, it may come as a shock, but we like Elm.
[00:12:52]
So why is this conversation meaningful at all?
[00:12:56]
I think it's interesting because how can we do more of that?
[00:13:00]
How can we understand what makes, what gives us that feeling of relief when it's compiling
[00:13:07]
and how can we do more of that?
[00:13:09]
And to understand that better, I think it's interesting to break down how can we, how
[00:13:15]
can we get around that in Elm and not feel confident when it compiles?
[00:13:21]
What makes that happen?
[00:13:22]
What are the foot guns in Elm?
[00:13:24]
Like, like in JavaScript, for example, some of the foot guns, we talked about this sort
[00:13:30]
of like deferred validation, you know, the shotgun surgery thing that we talked about
[00:13:36]
in our Parse don't validate episode.
[00:13:39]
That's just a common pattern that you see in JavaScript code because it's so easy to
[00:13:43]
just reach for JSON and Elm forces you to validate types before you can use them.
[00:13:49]
But it's also a convention that you can avoid.
[00:13:51]
I mean, if you, if you wanted to, you could, you could pass around a JSON object and you
[00:13:57]
could run a JSON decoder and deal with a result type and you could do result with default
[00:14:03]
in places.
[00:14:04]
And you could do that if you wanted to, right?
[00:14:06]
Yeah.
[00:14:07]
Or you could just have a dictionary.
[00:14:08]
Same issue.
[00:14:09]
Right.
[00:14:10]
And you could do that in Elm, but you're probably not going to.
[00:14:14]
Yeah.
[00:14:15]
So what I'm pointing out here, I think, or touching on is that there are things that
[00:14:21]
the compiler knows when things are static.
[00:14:25]
So records, for instance, are static and the compiler can help you figure out issues about
[00:14:31]
those.
[00:14:32]
So if you want to rename a field, it will tell you all the places where the field is
[00:14:38]
used or used incorrectly, I guess.
[00:14:42]
But if you switch to a JSON object and decoding it on the fly or dealing with a dictionary,
[00:14:49]
then the compiler doesn't help you.
[00:14:52]
Right.
[00:14:53]
So in a way it's having dynamic or sometimes magic values can create some issues, create
[00:15:01]
some problems and uncertainty in how the program will behave.
[00:15:05]
Right.
[00:15:06]
Yeah, I think that the example of dealing with JSON decode values and passing them through
[00:15:14]
and running individual decoders lazily just in time to get, obviously, maybe nobody's
[00:15:21]
ever done that before.
[00:15:22]
Maybe somebody's done it.
[00:15:23]
Who knows?
[00:15:24]
It's a large world out there, but that's not a realistic problem.
[00:15:27]
Yeah, because it's just too tedious to do.
[00:15:31]
Right.
[00:15:32]
But that said, I think we can still learn something about...
[00:15:36]
Because I think default cases handle a lot.
[00:15:40]
Default cases happen a lot where we have a maybe and we say, okay, well, let me just
[00:15:45]
do a maybe dot with default.
[00:15:48]
I think that the farther you can move those checks up in the code...
[00:15:53]
I think one rule of thumb that I try to follow is I want code dealing with the happy path
[00:16:00]
all the way down and I want my error handling code around the edges so it doesn't pollute
[00:16:07]
my core business logic as much as possible, which is basically the pattern of a JSON
[00:16:13]
decoder.
[00:16:14]
Right.
[00:16:15]
You run a JSON decoder, you validate everything, you say that not only is this a string, but
[00:16:20]
it's a nonempty string and it's a string that I can parse into a date type or it's a valid
[00:16:28]
username and it matches the username format and I can parse it into that type and all
[00:16:35]
these things.
[00:16:36]
And if anything fails, you go down a different path, the sad path, and if all of those things
[00:16:42]
succeed, then you go to the happy path.
[00:16:45]
And now you've got this set of functions that are blissfully unaware of the sad path.
[00:16:50]
And I think that that's something we can all benefit from turning up to 11 and doing a
[00:16:57]
little bit more of, even if we're not going to the other extreme, even if it's not dialed
[00:17:02]
down to one with passing around JSON decode values and decoding them just in time.
[00:17:07]
I keep thinking about it and it just feels so painful.
[00:17:11]
Just so painful.
[00:17:14]
Well that's good.
[00:17:15]
Then it worked.
[00:17:16]
Then I'm scaring people into dealing with errors upfront and getting that sad path out
[00:17:23]
of the way.
[00:17:26]
So there are other ways that the compiler helps you to avoid any issues, which leads
[00:17:34]
to when a compiler works.
[00:17:37]
One of them, for instance, is pattern matching or rather the exhaustiveness of pattern matching.
[00:17:44]
So as you said, you can always have a default case like a wild card and those should be
[00:17:50]
avoided.
[00:17:52]
Because as soon as you do those, you lose some kind of guarantees and you forget to
[00:17:57]
handle some cases.
[00:17:59]
But when you don't, well, the compiler gives you all the reminders that you need.
[00:18:02]
So you're adding a new variance to a custom type.
[00:18:07]
Then the compiler tells you, hey, you need to handle it here.
[00:18:12]
And that's kind of one of the things that I mentioned earlier that in JavaScript, you
[00:18:16]
don't know when you're done.
[00:18:18]
In Elm, here you would get a reminder.
[00:18:20]
Right.
[00:18:21]
Exactly.
[00:18:22]
Which is sort of the premise of my violins and Vuvuzelas analogy that...
[00:18:28]
You're going to need to explain that one.
[00:18:30]
Yeah.
[00:18:31]
So Vuvuzelas, you're going to have a hard time making beautiful sounds on a Vuvuzela.
[00:18:37]
Now if you want to prove me wrong, I'm open minded about that, but I'm pretty sure it's
[00:18:43]
not possible to create beautiful music with a Vuvuzela.
[00:18:46]
I'm not actually sure you really want to hear people play Vuvuzela and counter argument
[00:18:53]
you.
[00:18:54]
That's fair.
[00:18:55]
That's fair.
[00:18:56]
But a violin, you can create screeches on it, but you can create the most beautiful
[00:19:02]
music you've ever heard.
[00:19:03]
I think that Elm is similar in that the expressive power is there, but it doesn't come for free.
[00:19:10]
You need to be able to take advantage of it.
[00:19:12]
And that's our job as Elm developers is to understand the tools we're given with its
[00:19:19]
expressive power and how to use those.
[00:19:21]
So those tools are, it gives us exhaustive case statement or case expressions.
[00:19:27]
That's a powerful tool if you use it, but you can opt out of it if you want to.
[00:19:33]
Just like you can opt out of safe JSON handling and that sort of thing.
[00:19:38]
It's hard to opt out of because you have to be very explicit about everything, but you
[00:19:43]
can.
[00:19:44]
So, but it's our job as Elm developers to use those tools to model the constraints of
[00:19:49]
our system so we can get guarantees about our domain because Elm doesn't know about
[00:19:54]
our domain.
[00:19:55]
It knows what an int is and a string is and JSON is, and it knows how to model constraints
[00:20:00]
about that, but it doesn't know how to model constraints about your specific business domain.
[00:20:05]
Yeah, thankfully it's easier to learn about these than to learn how to play the violin.
[00:20:10]
It doesn't take years of practice.
[00:20:13]
That's true.
[00:20:14]
That's true.
[00:20:15]
Well, at least hopefully.
[00:20:16]
Yeah.
[00:20:17]
I think it's probably easier to write a beautifully written Elm application.
[00:20:20]
Do you think I can learn the violin in like two days?
[00:20:24]
Probably not.
[00:20:26]
Maybe Vuvuzela.
[00:20:27]
Yeah, maybe.
[00:20:28]
I should try that.
[00:20:31]
I'm sure my family will love it.
[00:20:35]
And your neighbors.
[00:20:38]
Yeah.
[00:20:39]
Yeah.
[00:20:40]
No.
[00:20:41]
So, another thing that is part of the equation is that Elm doesn't have any side effects.
[00:20:49]
Yeah.
[00:20:50]
Right.
[00:20:51]
And the consequence of that is that the order of operations does not really matter.
[00:20:57]
So if you compute A and then B, that will always give you the same results than if you
[00:21:06]
computed B and then A. In JavaScript or languages with mutations, you can have spooky effects
[00:21:14]
at a distance, and that could be false.
[00:21:18]
And that is one of the trickier things when you do refactoring.
[00:21:22]
You move things around and that actually changes the behavior.
[00:21:26]
In Elm, that is not a problem and people move things all around all over the place all the
[00:21:32]
time.
[00:21:33]
Right.
[00:21:34]
Right.
[00:21:35]
And when you're testing, that is an incredible quality because you can trust what you're
[00:21:41]
testing.
[00:21:42]
So I think that's huge.
[00:21:44]
And testing in Elm to me is such a such a no brainer.
[00:21:47]
If you've got these nice pure functions and you just run them and check them and it's
[00:21:53]
so much easier to test in Elm than it is in other contexts.
[00:21:58]
Now a thing I've been thinking about it.
[00:22:00]
So like let's let's drill into this a bit more about spooky effects at a distance and
[00:22:05]
things being predictable and depending on implicit things.
[00:22:09]
Can you do that in Elm?
[00:22:12]
I kind of think you can.
[00:22:14]
And it's subtle, but I've felt that feeling before.
[00:22:19]
So like...
[00:22:20]
Doing spooky effects at a distance?
[00:22:21]
You can do that in Elm?
[00:22:23]
You can't directly do spooky effects at a distance in the sense that you can't do...
[00:22:30]
You can't depend on environment, global variables, implicitly defined values.
[00:22:36]
But what if you jam a whole bunch of context into some type and depend on it in a way that
[00:22:43]
has confusing semantics and depends on lots of stuff?
[00:22:48]
You can start to get back at that in a way.
[00:22:52]
And so...
[00:22:54]
So if you have very complex business logic, for instance?
[00:22:58]
Well, I would say if you depend on too much.
[00:23:04]
If you depend on too much and pull in too many things instead of having something expressed
[00:23:10]
in the simplest possible terms of the fewest things that can depend on and having things
[00:23:15]
sort of sliced down in that way, it can start to feel like...
[00:23:18]
So one thing I've been thinking about is the semantics of APIs.
[00:23:23]
So we think of...
[00:23:25]
When you do JSON.decode.oneof, it feels extremely intuitive in the sense that if you do one
[00:23:33]
of with a list of strings decoded or a single string or null, then it's going to fall through
[00:23:46]
to any of those cases and it's pretty intuitive what it's going to do.
[00:23:52]
If you're writing a parser in Elm, we talked about this in our parser episode, the semantics
[00:23:57]
of backtracking and committing in a parser, so you chomp a specific character and now
[00:24:06]
you've committed down that path unless you make it backtrackable, right?
[00:24:12]
Now it's a very elegantly designed package and those are important tools for writing
[00:24:18]
a parser to be able to commit or make something backtrackable.
[00:24:22]
But they're confusing semantics and I would certainly say when I'm writing Elm parsers,
[00:24:28]
I do not think that if it compiles it works.
[00:24:30]
I think that if my giant test suite passes, it works.
[00:24:34]
Yeah.
[00:24:35]
Well, I think that's kind of the same with JSON decoders.
[00:24:40]
So when the types match, it doesn't give you a real sense of guarantees.
[00:24:47]
So I'm thinking maybe that's because the types are not expressive enough in a way.
[00:24:52]
Like for instance, if you do something like JSON decode maybe compared to JSON decode
[00:24:59]
nullable, they have I think the same type but they have different meanings.
[00:25:07]
I think Simon Liddell might have turned me on to this that essentially JSON.decode.maybe
[00:25:12]
is never what you want.
[00:25:14]
Like basically you should never use it because it's basically saying if anything at all fails,
[00:25:21]
then do this.
[00:25:22]
It's not saying if it's null, then use this value.
[00:25:26]
But if it's a different shaped object, then I expected fail.
[00:25:30]
It's actually never going to fail.
[00:25:32]
Yeah.
[00:25:33]
I think a big reason why when a compiler works is because of all the types that indicate
[00:25:41]
what values are possible and what you can do with the values.
[00:25:47]
But the decoders and parsers, they're not expressive enough or they lose a lot of detail
[00:25:54]
and therefore type checking helps but not entirely.
[00:26:00]
It's not enough.
[00:26:03]
Is my gut feeling at least?
[00:26:04]
I mean if you're writing a parser or a decoder, in a sense, we were talking about like spooky
[00:26:13]
effects at a distance and having access to global state or large amounts of state.
[00:26:19]
Well, I mean if you're writing a parser or a JSON decoder, you have access to all of
[00:26:24]
the state.
[00:26:25]
I mean if you do JSON.decode.field, you don't exactly have access to all of the state, quote
[00:26:31]
unquote, you have access to that context.
[00:26:34]
So that does scope your reasoning to a certain extent.
[00:26:37]
But it's also like a very wide open thing that you can grab data from wherever you want
[00:26:44]
and do all sorts of things with it and squelch errors.
[00:26:47]
I would say for JSON decoders, the issue is that a lot of things depend on the parents
[00:26:54]
or whoever uses decoders.
[00:26:56]
So if you do JSON.nullable and then pass it to the decoder, then that decoder depends
[00:27:02]
on the parents.
[00:27:03]
And that does not always get, and then the types don't help you do exactly what you want.
[00:27:10]
Yeah.
[00:27:11]
I mean the semantics are really important and I think that the JSON.decode.maybe is
[00:27:18]
a good example of how important the semantics of the API are.
[00:27:24]
And Elm doesn't give you that for free.
[00:27:25]
So JSON.decode is like a core package, but when we're writing Elm code, we're building
[00:27:32]
our own suite of functionality and giving them semantics.
[00:27:37]
And it's really important to consider how the semantics you're defining will give you
[00:27:43]
a pit of success or a pit of failure.
[00:27:46]
And things like JSON.decode.maybe can make it very tempting to do something that you
[00:27:52]
probably don't want to do.
[00:27:53]
I think it's also when you do like a JSON.decode.field, some field name, and then you pass that another
[00:28:01]
JSON.decode.field, then that one depends on the parents and that can be kind of tricky
[00:28:09]
or surprising.
[00:28:11]
I wouldn't call it spooky action at a distance, but I get your point that it's not as obvious
[00:28:17]
as it could be or as other APIs are.
[00:28:21]
Right.
[00:28:22]
Right.
[00:28:23]
And I mean, I guess the bottom line is if you can constrain the data that you're passing
[00:28:29]
through and make it easier to reason about what it's going to do and make it depend on
[00:28:33]
fewer things than you should, which is basically what Richard Feldman's talk, I can't remember
[00:28:39]
what the name of the talk is, but one of his Elm...
[00:28:41]
Scaling Elm apps?
[00:28:42]
Scaling Elm apps.
[00:28:43]
Yeah.
[00:28:44]
That was like the core message was, hey, if you can depend on less data, then pass in
[00:28:49]
less data.
[00:28:50]
Yeah.
[00:28:51]
In a way, I think that's what functional programming is pretty good at.
[00:28:54]
Yeah.
[00:28:55]
So, right.
[00:28:56]
Right.
[00:28:57]
Right.
[00:28:58]
Yeah.
[00:28:59]
And yeah, like if you just pass in your whole model all over the place and you can change
[00:29:04]
it anywhere, that can sort of give you that feeling of spooky effects at a distance.
[00:29:09]
And maybe a more controversial one, I don't have a fully solidified opinion on this one,
[00:29:17]
but you can do things like defining your message type in a message module and then use that
[00:29:23]
message rather than injecting those message constructors and message variants as needed
[00:29:32]
so you can explicitly trace it.
[00:29:34]
There are pros and cons there, but anytime you can get to that feeling of spooky action
[00:29:42]
at a distance.
[00:29:43]
You just pass in your model everywhere, give everything access to messages.
[00:29:49]
So you have to be aware of what state you're passing in everywhere because you can sort
[00:29:54]
of Elm by default gives you these nice pure functions that take data and return data.
[00:30:00]
But if you just balloon that up to have access to everything and be able to change everything
[00:30:04]
from everywhere, you start to lose that sense of if it compiles, it works because it's unmanageable.
[00:30:10]
Right.
[00:30:11]
Right.
[00:30:12]
And it starts to feel like spooky action at a distance if you're not too careful there.
[00:30:17]
I think what you're saying is that it becomes hard to expect what the code will do because
[00:30:23]
the code is just so complex because you made some spaghetti code or some very hard to maintain
[00:30:31]
code.
[00:30:32]
So writing maintainable code helps you do what you expect it to do.
[00:30:37]
Right.
[00:30:38]
Right.
[00:30:39]
So it's like in a sense, there's if it compiles, it works.
[00:30:43]
But then there's what you were talking about earlier, which is if I make a change, did
[00:30:48]
the behavior change in the way I expect it to?
[00:30:51]
Or not change in a way I expect it to.
[00:30:54]
And I think that's an important point, too.
[00:30:56]
When you're trying to change behavior or when you're trying to not change behavior, how
[00:31:01]
easy is it to do that in an expected way?
[00:31:04]
No, I feel like I never go to the states where my code is so complex that I don't understand
[00:31:11]
what it does because I have this ease of refactoring because when it compiles, it works.
[00:31:18]
That I just do it all the time.
[00:31:19]
And therefore, my code is relatively maintainable always.
[00:31:24]
Improvable, sure.
[00:31:27]
And I improve it when I need to because I can refactor all the time.
[00:31:31]
It never gets all that complex.
[00:31:34]
And also because there's the Elm architecture, which is now deeply ingrained in my mind so
[00:31:38]
I know how an Elm app works.
[00:31:42]
Another thing that I think is worth mentioning, even if it's a tiny thing, is that you cannot
[00:31:49]
have issues because of shadowing.
[00:31:53]
So shadowing in Elm is forbidden.
[00:31:56]
So that is when you have two different variables in the same scope.
[00:32:01]
So you define a variable name and somewhere else underneath it, another variable name,
[00:32:09]
name, name, name, name, name, name.
[00:32:13]
And that is not allowed because the issue is that it's fairly easy to remove one of
[00:32:19]
the names and then all variables that pointed to the other one now point to the other one.
[00:32:28]
I think you're illustrating how confusing it is pretty well.
[00:32:32]
Exactly.
[00:32:33]
So because that is not a problem in Elm.
[00:32:37]
In Elm 19 plus, yeah.
[00:32:38]
I remember going through code bases from the 18 to 19 migration and going through and changing
[00:32:43]
a lot of cases that had shadowing violations.
[00:32:49]
By the way, I'm super happy that shadowing is not there because it makes Elm review so
[00:32:55]
much easier.
[00:32:56]
Interesting.
[00:32:57]
Yeah.
[00:32:58]
But yes, so if you don't have shadowing, you don't have the problem where, oh, if I remove
[00:33:03]
a variable, then the values point to something else.
[00:33:07]
Now it's just like, hey, this name, this value, this name variable does not exist anymore.
[00:33:14]
Please make it exist or change the reference.
[00:33:18]
Right.
[00:33:19]
Right.
[00:33:20]
Again, one kind of reminder that the compiler gives you so you don't forget to do what you
[00:33:25]
were supposed to do originally.
[00:33:27]
That's a great point.
[00:33:28]
It's like another case where if you make a change, the resulting behavior change is going
[00:33:34]
to be predictable.
[00:33:35]
Whereas if you rename a variable and suddenly the behavior changes, that's unexpected.
[00:33:43]
That shadowing rule of Elm 19 plus fixes that.
[00:33:47]
Yeah.
[00:33:48]
Yeah.
[00:33:49]
I mean, similarly, like sort of language semantics.
[00:33:52]
I mean, there's the Gary Bernhardt's Watt video about strange unexpected semantics in JavaScript.
[00:34:01]
Yeah.
[00:34:03]
If you add a string in a number or subtract a string in a number and just truthy values,
[00:34:10]
right?
[00:34:11]
Or something that happens to me a lot when I'm writing JavaScript is I'll forget to return
[00:34:17]
a value.
[00:34:18]
Yeah.
[00:34:19]
Well, it's just void.
[00:34:21]
It's just like returning undefined.
[00:34:23]
And now you have it undefined somewhere.
[00:34:25]
I have it undefined.
[00:34:26]
And I'm like, what the heck?
[00:34:27]
It's running this code.
[00:34:28]
I put a log in there.
[00:34:30]
I see it's running.
[00:34:31]
And like, I've been through that way too many times.
[00:34:36]
Those little foot guns are removed by the core Elm semantics.
[00:34:41]
But again, we're the violinists.
[00:34:44]
We've got this very expressive tool, but it's only as expressive as we're able to take advantage
[00:34:50]
of those tools.
[00:34:52]
So similar to this concept of the semantics of truthy values, that can be a liability
[00:35:02]
working with JavaScript.
[00:35:04]
Sometimes you see people using this Boolean constructor to turn something into an explicit
[00:35:09]
Boolean and things like that to try to be safer.
[00:35:12]
But you have to go out of your way.
[00:35:15]
Also things like saying if not with the exclamation mark variable, people often do that to make
[00:35:22]
sure that it's not null or undefined.
[00:35:25]
But then it's zero.
[00:35:28]
Or it's empty string.
[00:35:30]
Yeah.
[00:35:31]
Right.
[00:35:32]
Semantics are important.
[00:35:33]
And we may have fixed that issue in the core semantics because truthiness is not a concept
[00:35:41]
in Elm.
[00:35:42]
It's very explicit in terms of concatenating values of the same type together and checking
[00:35:48]
truthiness with real Booleans and that sort of thing.
[00:35:52]
But we still have to define semantics for our domain again.
[00:35:55]
So I think having these value types is very important.
[00:36:02]
This primitive obsession code smell of passing strings all over the place instead of a username.
[00:36:10]
If something represents a username, just wrap it in a type, make it an opaque type.
[00:36:16]
If you need to validate it, then use that pattern which we talked about in depth in
[00:36:20]
our opaque types episode that you conditionally return that type if it's validated.
[00:36:25]
This sort of parse don't validate.
[00:36:26]
You can say username.from string and return a maybe.
[00:36:32]
And the only way you can get it is if it checks that validation.
[00:36:35]
So now you have this type which has semantics which are actually meaningful because they
[00:36:39]
tell you something about a validation that's been performed on that type or the origin
[00:36:45]
of that type because you can only receive it from running an HTTP request or things
[00:36:50]
like that.
[00:36:51]
So using these type semantics and creating these value types I think is huge.
[00:36:58]
I really like creating types for a user ID, specific types of user ID versus product ID,
[00:37:06]
things like that.
[00:37:07]
Yeah, in general, any technique that makes errors impossible, just like making impossible
[00:37:14]
states impossible, writing tests, using the type system, using Elm review, all those reduce
[00:37:22]
the number of things that you will have to check manually once your code compiles and
[00:37:27]
your test pass.
[00:37:29]
So all the tools that you can add to your test suite just reduce the amount of manual
[00:37:35]
work that you will have to do and therefore the missed expectations that you can have.
[00:37:41]
Right.
[00:37:42]
Yeah, it's kind of like in Richard scaling Elm apps talk, you know, like put yourself
[00:37:47]
in the shoes of going through and debugging an issue and you say, all right, what do I
[00:37:53]
know?
[00:37:54]
Putting on your detective hat.
[00:37:55]
I'm at this point in the code.
[00:37:57]
I have this data.
[00:37:58]
I didn't expect to have this data.
[00:38:01]
What do I know about the context of where I am?
[00:38:03]
And well, given that you're in the context of now application at a baseline, you know
[00:38:09]
that your types are correct.
[00:38:12]
That's great.
[00:38:13]
You know that your conditionals were running on Boolean's.
[00:38:17]
You didn't accidentally pass the wrong pass, no in some somewhere and get the falsy path.
[00:38:23]
That's nice.
[00:38:24]
But if you're getting an int argument, does that represent the right thing?
[00:38:28]
So if you put yourself in the shoes of trying to understand what do I know in a given piece
[00:38:34]
of code?
[00:38:35]
Well, if you're receiving an int instead of a user ID, that's a piece of information that
[00:38:41]
you don't know that you can't be sure of.
[00:38:42]
So that's one more thing that you can't add to the if it compiles, it works things that
[00:38:49]
you can trust.
[00:38:50]
Yeah.
[00:38:51]
So we talked a lot about wiring, but we didn't really get into it.
[00:38:57]
So wiring is yeah.
[00:39:00]
What is wiring?
[00:39:01]
When I talk about wiring, I just have the word boilerplate in my head because that's
[00:39:06]
what other people from newcomers or from other languages have in mind.
[00:39:12]
Like, Hey, this is, has a lot of boilerplates.
[00:39:15]
You have to do a lot of wiring yourself.
[00:39:19]
And I have to admit, I just tried thinking of examples of boilerplate or of code wiring.
[00:39:26]
And I'm like, I cannot think of any in the sense that I don't see them that way anymore.
[00:39:32]
Because for me, they're super useful.
[00:39:35]
Yeah.
[00:39:36]
I mean, it's certainly, it can be tedious to be verbose.
[00:39:40]
Like, I mean, one thing that I find particularly tedious sometimes is having to wrap things
[00:39:47]
in custom types or, you know, combine together multiple possible types, something could receive
[00:39:54]
in a custom type or things like that.
[00:39:58]
It's verbose, you know, it's not as verbose as creating an abstract user factory in an
[00:40:09]
anonymous class to implement abstract user factory in Java.
[00:40:13]
You can do better.
[00:40:14]
You can do longer Dillon.
[00:40:15]
Yeah, I could if I tried.
[00:40:20]
Maybe it's a single Tintu, I don't know.
[00:40:21]
But yeah, I mean, there is a cost to a wrapper.
[00:40:26]
If you have a user ID, there's a cost, you know, it's like, oh, it's so much easier to
[00:40:31]
just pass an Int.
[00:40:32]
And, you know, if you need to prove that something was a user ID, but, you know, to a certain
[00:40:40]
extent, it's a question of like, are you going to optimize for change or creating code?
[00:40:47]
Because we read and change code and debug code far more often, we spend far more time
[00:40:53]
and energy doing that than we do writing code.
[00:40:56]
And also, if you like how much of the time and effort in writing code can be attributed
[00:41:02]
to trying to make sure that you're doing it correctly and not making any mistakes.
[00:41:08]
So if it can reduce that problem, I think that it's a very good trade off to have to
[00:41:14]
be a little bit more explicit, a little bit more verbose to have this clarity in your
[00:41:19]
types.
[00:41:20]
Yeah.
[00:41:21]
You also often talk about moving wrapping and unwrapping to the extremities.
[00:41:27]
Wrap early, unwrap late.
[00:41:29]
Yeah.
[00:41:30]
The problem with user IDs, instead of just Int, is that you need to wrap it somewhere,
[00:41:38]
maybe parse it, which can be annoying because you need to handle the error case.
[00:41:42]
And you need to unwrap it, which is also more syntax.
[00:41:46]
But all the places in between, it's just value passing.
[00:41:51]
It's not more annoying than dealing with an Int.
[00:41:55]
If you did that in JavaScript, then maybe you would have more checks in the middle like,
[00:42:00]
hey, is this user ID null?
[00:42:03]
Hey, is this user ID zero, which has some special meaning?
[00:42:07]
While it is a user ID, it is actually very simple to use, I think.
[00:42:12]
So the cost is offset somewhat.
[00:42:16]
Yeah, for sure.
[00:42:17]
And I think it takes experience to become comfortable navigating those things, knowing
[00:42:22]
when to create those abstractions and how to manage, oh, I have this opaque type.
[00:42:28]
I need to use it in this place.
[00:42:30]
I need to make sure that it can only receive this type of thing and be used in this type
[00:42:35]
of way.
[00:42:36]
That takes experience.
[00:42:37]
But on the whole, I think it's more maintainable, but it might feel more sluggish.
[00:42:42]
It might feel like boilerplate.
[00:42:46]
And Elm is very explicit.
[00:42:47]
So if you're doing browser.application, you can't not pass in subscriptions.
[00:42:53]
You can't not pass.
[00:42:54]
And if you made the API in such a way that you could pass nothing, well, now you have
[00:42:59]
to wrap that in a just if you have the actual value.
[00:43:03]
Yeah, so you cannot forget to pass in things that are necessary to some extent.
[00:43:10]
Right.
[00:43:11]
And that's part of this.
[00:43:13]
That's part of that feeling of if it compiles, it works.
[00:43:15]
And it's so I mean, the fact that Elm only has these tagged union types for the custom
[00:43:21]
types, there's no way to just say, oh, I'll accept a string here or you can pass me a
[00:43:25]
record with these fields or it's more verbose, but it's very predictable.
[00:43:31]
And it might be tedious, but it's easy and straightforward.
[00:43:34]
So there's a bit of a trade off that the Elm always chooses explicit and predictable and
[00:43:40]
straightforward over frictionless and easy and low, low boilerplate or, you know, sleek
[00:43:49]
that those aren't the choices that Elm goes.
[00:43:52]
So if we avoid boilerplate that usually works by doing dynamic accesses, dynamic writes,
[00:44:01]
or even just magic things like method overloading in object oriented programming, which I always
[00:44:08]
find surprising, like, hey, the behavior of this thing changes if you define a X method.
[00:44:16]
Like, huh?
[00:44:17]
Right.
[00:44:18]
Right.
[00:44:19]
Very hard to detect.
[00:44:22]
Whereas if you did that in Elm, like if you remove a method that you thought no one uses,
[00:44:30]
then you would get a compiler error.
[00:44:32]
If you do that in Python is where I had a bad experience with that.
[00:44:37]
Well, your behavior changed and you don't know why, because you had to look at the Django
[00:44:44]
docs deep, deep somewhere that I still haven't found, but a colleague told me it exists.
[00:44:50]
Right.
[00:44:51]
Yeah.
[00:44:52]
That was always, you know, in my Ruby on Rails days, that was my first job out of college
[00:44:57]
working at a Ruby on Rails shop.
[00:44:59]
And it was a struggle for me to keep those things in my head with the implicitness and
[00:45:05]
the magic and the method missing automatically resolving things dynamically when you call
[00:45:11]
a function that doesn't exist or method that doesn't exist.
[00:45:15]
And Rails depended on that quite a bit.
[00:45:18]
And it would use the method, it would include something that really should be an argument
[00:45:24]
in the method name.
[00:45:25]
You know, when you say path to users, it would be far better to do path to string users or
[00:45:34]
something like that should be an argument.
[00:45:38]
So yeah, Elm is more chooses the verbosity in those cases, but I think that does contribute
[00:45:44]
to that feeling of predictability that if it compiles, it works.
[00:45:47]
Totally.
[00:45:48]
So you've never gotten your code to a point where you felt like it started to become unpredictable
[00:45:53]
Elm code that you couldn't really get a grasp on?
[00:45:57]
Well, yeah, for complex business logic, for instance.
[00:46:01]
But that's usually where I write heavy tests.
[00:46:07]
Like actually some Elm review rules are very complex.
[00:46:10]
And I look at them every few months or so because someone reported an issue.
[00:46:16]
And I'm like, I don't know how this works anymore, but I'm super happy that I have an
[00:46:21]
armada of tests.
[00:46:25]
But at least if I try to change something like the type of a field or I change the context
[00:46:33]
or slash model, then the compiler has my back and changes become a lot smoother.
[00:46:42]
Interesting.
[00:46:43]
I've noticed like when I'm writing sort of framework code, like this happens a lot in
[00:46:50]
Elm pages that I'm creating these building blocks and I often have to work with the lowest
[00:46:56]
common denominator.
[00:46:59]
Where something can, it's unconstrained, like the data source API, you can end then, you
[00:47:04]
can go get glob data and all these things.
[00:47:08]
And it can become harder to keep the semantics in your head.
[00:47:14]
Like for example, if you have a decoder and you can do decode.end then, or you can do
[00:47:19]
decode.fail anywhere.
[00:47:22]
Now suddenly you've got these possible failure paths that they're easier.
[00:47:31]
It's really nice to be able to just fail fast in a JSON decoder, but it's almost like less
[00:47:37]
explicit in the way that throwing an exception is less explicit.
[00:47:43]
Like you may not know when something failed.
[00:47:46]
Yeah.
[00:47:47]
Why?
[00:47:48]
The ways that it might fail.
[00:47:49]
Like how can a JSON decoder fail?
[00:47:53]
What's the error type of a JSON decoder?
[00:47:55]
It's kind of an untyped error.
[00:47:58]
It's just a string basically.
[00:48:00]
It's not like a nice custom type that says this is a result type and these are the possible
[00:48:04]
ways it could fail.
[00:48:05]
Here are these five variants with some information associated.
[00:48:08]
So I think now when you're building frameworks in these building blocks, these are really
[00:48:15]
powerful tools to write this lowest common denominator that can do a lot of powerful
[00:48:21]
things.
[00:48:22]
But again, this sort of scaling L maps idea, like don't use the lowest common denominator.
[00:48:28]
Use the simplest thing that could possibly work and be as explicit as possible.
[00:48:33]
So I think those are the times when I think about Elm code getting unwieldy and hard to
[00:48:40]
predict what it's going to do.
[00:48:42]
I think a lot of it comes down to having a lot of state and a lot of possible ways to
[00:48:48]
fail that things are boiling down to the lowest common denominator.
[00:48:54]
Sometimes I find it hard to think about like if I do maybe dot end then, that's like a
[00:48:58]
simple thing, the semantic, but it hurts my brain just a little bit.
[00:49:04]
Yeah.
[00:49:05]
But that does that cause your changes to not work when you could compile?
[00:49:10]
It causes me to not trust that it will work when it compiles.
[00:49:15]
Like maybe I just put a maybe dot end then and I know it's going to compile, but I have
[00:49:20]
to go in and check it manually or run some tests around it.
[00:49:25]
Then it works.
[00:49:26]
Right, right.
[00:49:27]
It still works.
[00:49:29]
You don't trust it, but it works.
[00:49:32]
It's just that you're getting back to your roots of your Elm adventure.
[00:49:40]
You don't trust it compiler yet, but it still works.
[00:49:44]
But the semantics could surprise me.
[00:49:48]
And I think semantics are very important.
[00:49:50]
Like I don't know, I don't trust if I tried to explain the semantics of maybe dot end
[00:49:57]
then to you, I couldn't explain it clearly.
[00:50:00]
I couldn't write out a truth table of what it does exactly very clearly.
[00:50:04]
I'm sure if I thought about it for a couple of minutes, I could do that.
[00:50:08]
But that's sort of like a semantic concept in my brain that's not quite clear.
[00:50:13]
Just like decode dot maybe, the semantics are a little misleading.
[00:50:18]
But like the maybe dot extra dot or sort of helpers around the logical or and end and
[00:50:24]
those things, that I find intuitive.
[00:50:28]
Sometimes I think semantics are really important, even if the wiring you can trust.
[00:50:33]
So is it the name of and then that you find confusing?
[00:50:38]
Maybe?
[00:50:39]
Maybe.
[00:50:40]
Aha.
[00:50:41]
That's just it.
[00:50:44]
Well then I'm going to give you the task to just make that better.
[00:50:51]
Maybe then we'll have some good results.
[00:50:56]
I'm not going to give you a list of all the things to do though.
[00:51:04]
So what can people do to improve their chances of when it compiles it works?
[00:51:11]
So we mentioned do tiny steps because tiny steps have less risk to them.
[00:51:19]
You are less likely to forget to do something or to do it incorrectly when you get down
[00:51:27]
to it.
[00:51:28]
So tiny steps definitely one.
[00:51:30]
Yes.
[00:51:33]
And we talked about sort of wild cards and squelching errors or maybe types, just giving
[00:51:42]
them default values.
[00:51:43]
And more broadly, dealing with the sad path from the start at the gate.
[00:51:49]
Move that as far left as you can, ideally all the way to the left to compile time if
[00:51:54]
you can or to your entire give of constraints.
[00:51:58]
Move it to the Elm review step to static analysis time if you can't move it to compile time.
[00:52:04]
But if you can move it to compile time, types without borders, check things at compile time.
[00:52:09]
That's ideal.
[00:52:10]
But yeah, I think that that's going to, I mean, if you've written JSON decoders for
[00:52:17]
an API, you know that it can compile and not work.
[00:52:21]
Right?
[00:52:22]
Yeah.
[00:52:23]
Yeah.
[00:52:24]
But I think the issue of when it compiles, it doesn't work is for me that I forget to
[00:52:28]
do something.
[00:52:29]
So usually it means that in Elm at least, because I'm reminded to do most things, when
[00:52:36]
it doesn't work, it's because I intentionally or unintentionally didn't do anything, something
[00:52:42]
like I had a, I returned zero instead of a real value and added a common thing to do,
[00:52:50]
blah, blah, blah.
[00:52:51]
So I think it's useful to make those clear, like add comments saying to do make them easy
[00:52:58]
to find, make your editor or Elm review to report those to you.
[00:53:04]
Use debug.todo, add new tests that you can also do to do for those.
[00:53:11]
So yeah, give yourself reminders and help yourself help the compiler give you reminders.
[00:53:16]
So exhaustiveness, don't use wildcards, don't use defaults, stuff like that.
[00:53:22]
Right, right.
[00:53:23]
And if you're going to ignore errors, you know, which when you're trying to get something
[00:53:28]
on the screen, you want to do the simplest thing that could possibly work.
[00:53:31]
You want to get your tests to green.
[00:53:33]
There's nothing wrong with that.
[00:53:34]
Like that's, that's a good approach to start with getting the happy path wired through.
[00:53:40]
But like you said, when you have those kind of default values passed through deep within
[00:53:46]
the recesses of your logic, and your sort of data processing code and decoding code
[00:53:51]
and that sort of thing, then when you do go to handle those errors, which you'll need
[00:53:56]
to once you get past the first few steps, now you've made that job a lot more difficult,
[00:54:02]
because you've, you've covered them up, which you might forget about, like you said, you
[00:54:08]
want to give yourself reminders, you want to give Elm the opportunity, like Elm's ability
[00:54:12]
to remind you to handle every possible case is only as useful as your ability to be honest
[00:54:19]
about what those possible cases are.
[00:54:21]
So if you're not honest in your data modeling, or, you know, we were human, we make mistakes,
[00:54:28]
and we can come back and revisit things.
[00:54:30]
But when we do do that in a way where we're acknowledging the reality in our data types,
[00:54:35]
rather than covering them up.
[00:54:37]
So acknowledge the reality and if you have an error, handle it at the top level, not
[00:54:44]
in, you know, in the recesses of your code, because at the very least, now you you say,
[00:54:50]
okay, bubble up this error, you have the information in the central point, things are wired up,
[00:54:55]
and you're just saying, I'm just not going to handle that right now.
[00:54:59]
But at least you're acknowledging that it's there at the top level.
[00:55:03]
Yeah, because if you handle the error at a lower level, and not at the top level, then
[00:55:08]
at some point, you will need to cover it up, necessarily.
[00:55:12]
Right?
[00:55:13]
Yeah, I mean, that's sort of like the feeling I get when, you know, when I'm working with
[00:55:17]
languages that have sort of these these control flow mechanisms for exceptions, which is most
[00:55:25]
most languages.
[00:55:27]
When I'm dealing with exceptions, I don't, I don't know where a problem could come from,
[00:55:31]
I don't know when I might hit a snag and a problem might occur, because there's nothing
[00:55:35]
explicitly telling me an exception may occur here.
[00:55:39]
And I don't know where that's being handled.
[00:55:42]
I don't get the opportunity to say, hey, run this thing.
[00:55:45]
And if there's an error, I want to opt into doing something about it, because something
[00:55:50]
else is, you know, deciding what what to do about that.
[00:55:53]
So it makes it feel a little bit more magical and unpredictable when when you put you know,
[00:56:01]
when you do those types of shortcuts in your own code.
[00:56:04]
Yeah.
[00:56:05]
By the way, raising an error or throwing error also causes some code not to be executed.
[00:56:14]
And when they have side effects, that can lead your code to have different behavior.
[00:56:19]
So not having the exceptions makes Elm code easier in that regard as well.
[00:56:25]
Right?
[00:56:26]
So there's no special mechanism for for control flow, maybe some code will be executed.
[00:56:33]
But I mean, we're still just computing a value.
[00:56:36]
Yep.
[00:56:37]
I mean, that there was a consensus decade ago that go to statements were not helpful
[00:56:44]
in high level programming languages.
[00:56:46]
But essentially, an exception is not exactly a go to statement.
[00:56:52]
But it is a sort of special case in in control flow that creates this whole new mental model
[00:56:59]
that you have to hold in your head.
[00:57:01]
That makes it the control flow becomes implicit.
[00:57:05]
It's implicitly jumping to this spot.
[00:57:07]
So it's still too close to a go to in my opinion.
[00:57:10]
Yes.
[00:57:11]
Yeah, I think it feels similar.
[00:57:13]
And where it goes is actually not clear.
[00:57:16]
Mm hmm.
[00:57:17]
Yeah.
[00:57:18]
Compared to go to.
[00:57:19]
Yeah.
[00:57:20]
Yeah, I would say the biggest, biggest takeaway, I think biggest bang for your buck that people
[00:57:26]
could get from from listening to this is our advice on not squelching errors and of dealing
[00:57:32]
with the sad path at the top, not in all of the leaf nodes, and having nice semantic types
[00:57:39]
and taking advantage of opaque types.
[00:57:41]
Like I would really recommend going back and listening.
[00:57:44]
I think it was episode three, our opaque types episode.
[00:57:46]
That's true.
[00:57:47]
Well, it's true.
[00:57:48]
Episode two, see, that's how important it was.
[00:57:50]
It was more important than Elm review.
[00:57:53]
Don't say that.
[00:57:54]
People are going to believe it.
[00:57:55]
Hey, it's up there in your hierarchy.
[00:57:58]
If you can do it at the API level.
[00:58:02]
Yeah, I know.
[00:58:03]
I love opaque types, so I can't say anything bad about it.
[00:58:09]
I'm still very surprised that when it compiles, it works is so true to Elm.
[00:58:17]
Even with all that we said, and mostly just because there's no side effects and there's
[00:58:23]
a good, complete, exhaustive type system or sound one.
[00:58:27]
And I find that so surprising.
[00:58:30]
I mean, it's not even a very complex type system, right?
[00:58:36]
Which I think is a big part of it because its simplicity makes it very predictable.
[00:58:42]
Right, but it also limits some constraints that it could give.
[00:58:48]
That's true.
[00:58:49]
It doesn't say, oh, this number is always between one and five, or in the range of this
[00:58:55]
list or this array's length.
[00:58:58]
Right.
[00:58:59]
So yeah, I'm really surprised.
[00:59:02]
To be honest, I still find it hard to explain why just having this type system makes so
[00:59:09]
much of a difference.
[00:59:11]
But I'm super happy it does.
[00:59:14]
And I think it can be attributed as much to the Elm language as it can to the Elm philosophy
[00:59:22]
and how much Elm packages in the ecosystem embody that philosophy of modeling things
[00:59:30]
with APIs that are very oriented on constraints and giving you a minimal API.
[00:59:36]
Because you can write all sorts of APIs that allow you to do something, but don't really
[00:59:41]
model constraints.
[00:59:43]
You can make impossible states possible.
[00:59:46]
And there's nothing in Elm stopping you from doing that in a published package or in your
[00:59:52]
own code.
[00:59:53]
All my APIs are functions that take care of dictionary.
[00:59:57]
Right.
[00:59:58]
Right.
[00:59:59]
They receive a based on decode value and they could give a result anywhere.
[01:00:03]
Yeah.
[01:00:04]
So it could go wrong.
[01:00:06]
Maybe it won't actually happen, but just to be sure.
[01:00:09]
All my functions are as powerful as the JavaScript eval.
[01:00:13]
Right.
[01:00:14]
Every function returns a result.
[01:00:16]
You could do that.
[01:00:17]
And that's a testament to the philosophy and how well it's caught hold in the Elm ecosystem.
[01:00:24]
Yep.
[01:00:25]
Well, I think we've covered if it compiles, it works.
[01:00:30]
I think so.
[01:00:31]
All right.
[01:00:32]
Well, happy to hear that.
[01:00:33]
Well, well, happy, happy coding.
[01:00:36]
Enjoy that beautiful Elm compiler guarantees and the Yeroon until next time.
[01:00:40]
Until next time.