Live data from Hacker News

Please do not attempt to simplify this code

github.com

131–140 of 327 posts

Re: Please do not attempt to simplify this code

#131
post #126

Earlier quoted context omitted.

How would this work if I need to update multiple variables? int value1 = 0; int value2 = 0; if (condition) { value1 = 8; value2 = 16; } else { value1 = 128; value2 = 256; } Would I have to repeat the if expression twice? int value1 = if (condition) { 8 } else { 128 }; int value2 = if (condition) { 16 } else { 256 };

Depends on the language a bit, but a common feature in these languages is the tuple. Using a tuple you would end up with something like: let (value1, value2) = if (condition) { (8, 16) } else { (16, 256) } Or else you’d just use some other sort of compound value like a struct or something. Tuple is just convenient for doing it on the fly.

hah we gave basically the same example on the same minute.

I love destructuring so much, I don't know if I'd want to use a language without it anymore.

Re: Please do not attempt to simplify this code

#132

Am I weird in feeling like the code in this file is really really... normal? Like, it's verbose in certain ways due to being written in Go, as well as due to not relying on any deep abstractions (and I don't mind this - abstractions are a double-edged sword), but in general, as code, it seems typical - and if the header text didn't exist I wouldn't think twice about the style it's written in. Maybe the disconnect her…

I wish code like this still felt normal to me, but over the past ~10 years it seems that many people have come to value brevity over explicitness. I strongly prefer the explicitness, at least for important code like this. More than once in my career I've encountered situations where I couldn't figure out if the current behavior of a piece of code was intentional or accidental because it involved logic that did things…

Every time some code reviewer comes into my PR and says something along the lines of "you know you can just write it this way" where "this way" means obfuscating the code because "clever" and "shorter," I die a little on the inside. This is from experienced devs who should know better. At one point I wrote a comment write above a section I knew would be targeted by this kind of thinking explaining it must be written this way for clarity. Sometimes that works.

Re: Please do not attempt to simplify this code

#133

Earlier quoted context omitted.

A simple example for anyone who might not appreciate why this can be so nice. In languages where if is a statement (aka returns no value), you'd write code like int value; if(condition) { value = 5; } else { value = 10; } Instead of just int value = if(condition) {5} else {10} Some languages leave ifs as statements but add trinary as a way to get the same effect which is an acceptable workaround, but at least for me…

It’s only an acceptable workaround in the case of two conditions, but you’re still out of luck if you have >2 branches and no match expression.

You can technically do some craziness with nested ternary operators but they look awful and if you write them you will regret it later.

Re: Please do not attempt to simplify this code

#135

Earlier quoted context omitted.

You appear to have misread "expression" as "exception"; this is completely unrelated. An expression-based language is one that lets you do `let blah = if foo then bar else baz`, for example.

i honestly struggle with this because its a "i know when i see it" thing, ex. here, const boo = foo ? bar : baz suffices which brings in ~every language I know. My poor attempt at a definition, covers it in practice in languages I'm familiar, but not in theory, I assume: a language where switch statements return a value

Yeah you nailed the limitation. Switch type expression that returns a value is a pretty universal feature in expression based languages, often in the form of a pattern matching based expression.

Check out the ‘case’ statement in elixir for an example.

In languages that support it, it usually becomes an incredibly commonly used expression because it’s just so applicable and practical.

Re: Please do not attempt to simplify this code

#136

Earlier quoted context omitted.

Depends on the language a bit, but a common feature in these languages is the tuple. Using a tuple you would end up with something like: let (value1, value2) = if (condition) { (8, 16) } else { (16, 256) } Or else you’d just use some other sort of compound value like a struct or something. Tuple is just convenient for doing it on the fly.

hah we gave basically the same example on the same minute. I love destructuring so much, I don't know if I'd want to use a language without it anymore.

It’s actually so painful to go back to languages without destructuring and pattern matching.

Re: Please do not attempt to simplify this code

#137
post #119

Earlier quoted context omitted.

I wish code like this still felt normal to me, but over the past ~10 years it seems that many people have come to value brevity over explicitness. I strongly prefer the explicitness, at least for important code like this. More than once in my career I've encountered situations where I couldn't figure out if the current behavior of a piece of code was intentional or accidental because it involved logic that did things…

Agreed. Explicitness and comments are very useful in understanding the intended functionality and logic, whether or not the code actually implements that intent correctly (an in providing that intent, they can help identify bugs earlier than they would be identified otherwise).

But comments go out of date, and the compiler doesn’t check them against the implementation. To document + enforce the intended functionality, use tests.

Re: Please do not attempt to simplify this code

#138

Earlier quoted context omitted.

hah we gave basically the same example on the same minute. I love destructuring so much, I don't know if I'd want to use a language without it anymore.

It’s actually so painful to go back to languages without destructuring and pattern matching.

As someone who writes a fair bit of c# making switch and if's into expressions and adding Discriminated Unions (which they are actually working on) are my biggest "please give me this."

Plus side I dabble in f# which is so much more expressive.

Re: Please do not attempt to simplify this code

#139

Earlier quoted context omitted.

It’s only an acceptable workaround in the case of two conditions, but you’re still out of luck if you have >2 branches and no match expression.

You can technically do some craziness with nested ternary operators but they look awful and if you write them you will regret it later.

True, but I would have to categorize that as an unacceptable workaround haha

Re: Please do not attempt to simplify this code

#140

Am I weird in feeling like the code in this file is really really... normal? Like, it's verbose in certain ways due to being written in Go, as well as due to not relying on any deep abstractions (and I don't mind this - abstractions are a double-edged sword), but in general, as code, it seems typical - and if the header text didn't exist I wouldn't think twice about the style it's written in. Maybe the disconnect her…

I know. I would never remove an "empty" branch full if comments, that's the compiler's job.
Post reply on HN