diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8dd6d56 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +public/ +.hugo_build.lock diff --git a/.hugo_build.lock b/.hugo_build.lock deleted file mode 100644 index e69de29..0000000 diff --git a/public/404.html b/public/404.html deleted file mode 100644 index 9d42cf6..0000000 --- a/public/404.html +++ /dev/null @@ -1,234 +0,0 @@ - - - -
- - - - -- - - -
- -(alternative title: panic()
considered harmful)
errors in Go programs are represented by values. when calling a function that may return an error, it’ll return (T, error)
, and you can handle the error the same way you would any other value.
(note: the (T, error)
syntax doesn’t imply Go has tuples, because it doesn’t. functions can just return multiple values.)
now, this approach does have some problems. Go lets you ignore the error very easily, by using the discard operator _
rather than assigning it to a variable. it’s also extremely verbose–anyone who has used Go a substantial amount has written if err != nil { return err }
a thousand times. both of these issues are fixable, though: the compiler could emit a warning or an error (heh) when discarding an error value, and new syntax could be introduced to bubble errors up more easily.
what isn’t fixable, though, is the panic system. any function, no matter its signature, can call panic()
at any point, with any value. as soon as it’s called, the call stack unwinds all the way to main()
and the program crashes. a function doesn’t have to indicate that it can do this. in fact, it can’t, except in a documentation comment.
you can catch panics with a call to recover()
. you can’t put this below a function that might panic, though, you have to use defer
before the panic happens. recover()
will return the value passed to panic()
, or no value if no function panicked. (ironically, recover()
also doesn’t return an error, or even a boolean, when nothing panicked. you check if the return value is nil
instead.)
this is basically a worse version of try { } catch { }
in other languages. you can’t guarantee that the value returned by recover()
is an error, it can be any type. it moves error recovery away from the source of the error. and, of course, it’s inconsistent: the (T, error)
pattern lulls you into a false sense of security, making you think that you handled all possible errors in your application, when surprise! it can always panic.
simple: remove panic()
entirely. all functions that panic now must instead return an error, and the caller must handle that error. now when a function doesn’t return an error type, you know it can’t ever crash your program! problem solv–
oh. the language itself panics quite a bit as well. trying to access a slice index that doesn’t exist, trying to assign to a nil
map, dereferencing a nil
pointer, trying to write to a map from multiple goroutines simultaneously, and more can all cause panics. that requires a little more thought.
well, here we go:
-(T, error)
, or at least (T, bool)
, at all times, unless the index is known to exist at compile time. this would be extremely annoying in the current version of the language, but would be fine if a more streamlined way of bubbling errors up was added, such as borrowing Rust’s ?
operator.nil
maps should be removed entirely. every map is strongly typed to begin with, and there is no practical difference between a nil
map and an empty one. in my mind, the current behaviour is the language exposing an implementation detail (distinguishing between nil
and empty maps) for no real reason.(T, error)
. borrowing C#’s ?
operator to streamline accessing deeply nested values would help here. alternatively, do away with pointers entirely and replace it with a system similar to Rust’s Option<T>
type.sync.RWMutex
, but this would massively slow down single-threaded programs, which is why the developers decided against doing this in the first place. maybe assigning to a map could return a boolean, with false
meaning the write failed because another goroutine wrote to it at the same time. that’s not a perfect solution, though.but really, the biggest problem with panics is that all of the decisions surrounding them are set in stone, forever. Go intends to never release 2.0, which is understandable: they don’t want a repeat of the Python 2 to 3 transition. but it also means the language can never truly grow past some of its faults.
-luckily, this is all just a thought experiment. if i want a more reasonable error handling system, there are so many other languages to choose from. such as c#–System.NullReferenceException
…oh, never mind.
- -
- -- - - -
- -oh hey, it’s the stereotypical “hello world” post. look at me! i copy pasted a hugo tutorial!
-turns out making a blog is really difficult, and not for technical reasons. it’s just really hard to actually write things consistently. -i can’t promise anything in that regard–expect this site to get about one post a year, and that’s if i actually remember it exists.
-until then though, have fun reading the one rant i’ve already written. hope you like go because if not, it probably isn’t for you!
- -- -
- -- - - -
- -Rust is the big new1 thing in just about every programming niche. web frameworks? of course there’s a Rust one! in fact, there’s dozens of them. command line tools? good luck finding one that isn’t written in Rust these days. databases? oops, all Rust!
-all of this carcinisation2 has me feeling kind of left out, though. you see, i’ve never really vibed with Rust: i have tried it multiple times, even building a handful of (unfinished, naturally) projects with it, but i always bounce off of it eventually. for a long time, i attributed this to Rust just not being what it’s hyped up to be, that it’s actually a lot worse than everyone says it is, but… it really isn’t. it is a good programming language. a great one, in fact! i want to dig a little deeper into why i, personally, don’t vibe with it. (not very deep, mind you, because self-reflection is difficult as hell)
-starting off with the big one: Rust is a systems language. while it also works very well for web development, command line tools, and other software, it’s not what it was designed for. it was designed for scenarios where you can (and often need to!) squeeze as much performance as possible out of a system, where a garbage collector on its own would already not fit into the device’s memory, and where a string taking up any more space than its literal bytes is unacceptable.
-meanwhile, when i used Rust, i just wrapped everything in an Arc<RwMutex<T>>
3 and called it a day, because i don’t care about performance all that much. HTTP and database latency already destroy any optimization i could do to the business logic, so why even bother?
i’ll admit it: i didn’t really go out of my way to learn Rust. like every other language i’ve used, i just jumped in headfirst and started trying to rewrite my silly little programs into the silly little crab language. this was a mistake.
-Rust’s compiler is its strongest suit: it catches bugs that no C or C++ compilers can even imagine exist, and its borrow checker seems like dark magic at times. but what makes the compiler great for experienced Rust users makes it an absolute pain for people learning the language. other languages let you throw code at the wall and see what sticks; Rust won’t even let your code compile unless it works perfectly. this makes my learning process a lot more difficult, and as such, i never really learned more advanced Rust.
-macros are extremely powerful. many of the libraries i was using heavily relied on them–some, like sqlx
(no relation to sqlx
, the Go library4) are entirely built around them. they’re also a nightmare to reason with as an inexperienced rustacean.
i’m used to being able to ctrl-click on a function invocation to go directly to its implementation in other languages–Go, C#, and even Python all do this (well, as long as the Python package has type annotations, and you’re not relying on external ones). this just doesn’t work with macros, because the code is generated at compile time. it felt like i was feeding input into a black box and praying that whatever came out on the other side worked5.
-somewhat related to macros: some libraries like axum generate ridiculously long type names, which, while funny, are impossible to actually parse. this didn’t make debugging my code any easier.
-i don’t really have any conclusion to draw from writing this. i guess my learning process just doesn’t mesh with how Rust works, and that’s why i bounced off of it.
-i definitely think it’s worth thinking about this for any language–or any other tool in general, even–that you dislike. it’s honestly kind of depressing to be extremely negative about things without being able to articulate why. i’m sure i’ve annoyed plenty of friends by complaining about Rust or any other tool they like.
-in fact, i should write a post like this for every language i dislike! Go, Elixir, maybe even PHP–well, maybe not that last one. it’s been so long since i last used it that i’m sure all my problems with it have been fixed at this point.
-please ignore that it’s been stable for over 9 years. ↩︎
-wait a second, does that mean i just used a reference-counting garbage collector? ↩︎
-sqlx
, the Rust library that converts database rows to strongly typed structs using reflection magic (macros), not to be confused with sqlx
, the Go library that converts database rows to strongly typed structs using reflection magic (the reflection package). they are somehow completely unrelated. ↩︎
this is, coincidentally, why i don’t use LLMs. ↩︎
-- -
- -heya, i’m sam! this is my attempt at a blog which i will definitely update often, trust me.
-i deliberately don’t really have a public online presence, but you can find me here:
-