DEV Community

Discussion on: Else Before If In Rust

Collapse
 
jeikabu profile image
jeikabu

Interesting. Not entirely sure I agree, but interesting.

Thing is, the if branches aren't necessarily the "exceptions". Some compilers even optimize assuming the if branch is taken.

I guess it's a question if you write "if ok" vs "if ! ok".

Collapse
 
louy2 profile image
Yufan Lou

Yeah, I am not sure about it either, but it feels surprisingly ok.

Whether the branches mean "exceptions" in some sense may be less relevant than the fact that rarely do branches of a conditional in a real application get even chance of execution. So identifying the majority branch can be helpful, to other programmers and maybe to compilers.

That said, I've never heard of compiler optimization assuming a branch is taken. I only know about CPU doing branch predictions, but that's to prevent stalling the pipeline during branch execution. How does a compiler optimize based on branch prediction? The pipeline being the event loop?