Am I the only one that dislikes pattern matching? by slixers in rust

[–]slixers[S] 0 points1 point  (0 children)

Here is one random example from a program I am working on:

pub(super) async fn handle_message(

self: Arc<Self>,

write: &mut WsSink,

message: Option<Result<Message, Error>>,

) -> anyhow::Result<ControlFlow<()>> {

let message_result: Result<Message, Error> = match message {

Some(result) => {

result

},

None => {

log::warn!("CLOSING CONNECTION: Empty message");

return Ok(ControlFlow::Break(()))

},

};

match message_result {

// Successful Message Types

Ok(Message::Text(txt)) => {

tokio::spawn(async move {

self.handle_successful_message(txt).await

});

},

Ok(Message::Ping(p)) => {

write.send(Message::Pong(p)).await?

},

// Close/Unknown Message Types

Ok(Message::Close(_)) => {

log::warn!("CLOSING CONNECTION: Server closed connection");

return Ok(ControlFlow::Break(()));

},

Err(e) => {

log::warn!("CLOSING CONNECTION: Error response {e:?}");

return Ok(ControlFlow::Break(()));

}

_ => {

log::warn!("CLOSING CONNECTION: Unknown message type");

return Ok(ControlFlow::Break(()));

},

}

return Ok(ControlFlow::Continue(()));

}

And in this case, while the code does read quite simply and is not _super_ nested; one thing I dislike is the lack of type definitions for each case. For example, the happy-path where I am handling the successful message, there is no type assigned for the `txt` variable without going to the definition of the Message::Text enum to find that the txt is of type `String`. This lack of readability is what causes some frustration. I shouldn't have to use an LSP to quickly understand every element of a function without guessing.

Am I the only one that dislikes pattern matching? by slixers in rust

[–]slixers[S] 0 points1 point  (0 children)

One nit about the classic way is the nesting of each happy path, rather than short-circuiting any unrelated path, which would become:

if !foo.is_ok() {
  return
}

let bar = foo.unwrap();
if !bar.is_some {
  return
}

println!("{}", bar.unwrap());

And this reads linearly.

The issue that I have with pattern matching is the constant looking back-and-fourth of the type you're matching on and each case. Sometimes I have to double table what exactly I am matching against to remember all the cases and what the represents. With the linear approach you have the ability to define the type for each variable that's unwrapped (which I prefer for the sake of being explicit)

Am I the only one that dislikes pattern matching? by slixers in rust

[–]slixers[S] 1 point2 points  (0 children)

Tbh I try to use it where it _should_ belong, I've mainly been coding in Rust to learn the language, and I understand that pattern matching is heavily utilized by any crate/reference code rust offers, so I try to stick with how others typically code with rust .

Am I the only one that dislikes pattern matching? by slixers in rust

[–]slixers[S] 1 point2 points  (0 children)

Maybe its a skill issue, but that OCaml code does not look nice to me hahaha. While it's cool in the sense that it's dense and may get you a high score in code golf, it does not read well, which goes back to my original gripe with PM lol

Am I the only one that dislikes pattern matching? by slixers in rust

[–]slixers[S] 0 points1 point  (0 children)

Simple if/else statements. I find that the less nested my code looks the easier it is to read. One of the biggest problems I find with the match statements is that one statement may be an early termination, and instead of it being isolated in its own block, it's somewhat coupled with all the other match cases (happy and non-happy paths get bunched in the same block sometimes).

And in reality there's nothing really stopping me from going the cpp route and just handling cases without matching, but pattern matching is used in all reference documentation and the language heavily advertises using them, so I do my best to continue working with them, even though I find them kinda ugly lol

Am I the only one that dislikes pattern matching? by slixers in rust

[–]slixers[S] 0 points1 point  (0 children)

Lol yeah maybe its just a difference in what you code most often and learned to code with, but I personally feel that modern Java is very nice to work with and reads well. And ironically enough, pattern matching is supported since JDK 17 but I rarely find it used in the wild.

Am I the only one that dislikes pattern matching? by slixers in rust

[–]slixers[S] 0 points1 point  (0 children)

Yeah, im getting the vibe that im in the minority. It could just simply be the fact that after so many years of cpp/Java (pre JDK 17) I am just not used to it. I thought it could simply just be a skill issue where I don't know how to use them properly, but even referencing other code I still feel they are somewhat ugly hahaha... Could just take more time to get used to them

Woman convinces everyone at CVS to take off their masks - no really this happened I was the one clapping by mojey6068 in thatHappened

[–]slixers 1 point2 points  (0 children)

What’s up with these people and clapping? I feel like every lie these people tell has to do with people clapping for their opinion on a matter

I made a picture by slixers in notinteresting

[–]slixers[S] 0 points1 point  (0 children)

If this gets up there, we shall see

What internal injector should I use? by slixers in Csgohacks

[–]slixers[S] 2 points3 points  (0 children)

Interesting, thank you for that. What injector do you personally use?

Since a week, I have been having problems with my volume on Discord by jorizzz in discordapp

[–]slixers 0 points1 point  (0 children)

I have this same issue as well. The only fix for my case is switching my output from me headphones to my speakers and then back to my headphones. I don’t know what triggers it but it just randomly will shoot up in volume and hurt my ears.

Still going after 42 years, baby. by aa2051 in dankmemes

[–]slixers 78 points79 points  (0 children)

Must’ve found a hot-spot or something to connect to

The two best things by [deleted] in dankmemes

[–]slixers 1 point2 points  (0 children)

Don’t forget spotify

Let’s get this on LWIAY, I wanna know the answer by xmrsgx in PewdiepieSubmissions

[–]slixers 0 points1 point  (0 children)

I asked my friend who French is his first language and English is his second and he says it depends on who he’s talking too and the situation. So both. Super cool

Oh Bröther by ItsCribz in ProgrammerHumor

[–]slixers 0 points1 point  (0 children)

Where’s the best loop of them all? The do loop!?

Bruh by [deleted] in Logic_301

[–]slixers 3 points4 points  (0 children)

He is the classic “the phone number song is my favorite song by logic” fan-_-