I wonder what portion of all go code written is
if err != nil { return err }
It’s gotta be at least 20%
Submitted 1 year ago by mycroftholmess@lemm.ee to programmer_humor@programming.dev
https://i.imgur.com/7fQWObW.jpg
I wonder what portion of all go code written is
if err != nil { return err }
It’s gotta be at least 20%
People are scared of monads and think this is better.
My brain is too smooth to imagine a solution to this using monads. Mind sharing what you got with the class?
Swift and Rust have a far more elegant solution. Swift has a pseudo throw / try-catch, while Rust has a Result<> and if you want to throw it up the chain you can use a ? notation instead of cluttering the code with error checking.
The exception handling question mark, spelled ?
and abbreviated and pronounced eh?
, is a half-arsed copy of monadic error handling. Rust devs really wanted the syntax without introducing HKTs, and admittedly you can’t do foo()?.bar()?.baz()?
in Haskell so it’s only theoretical purity which is half-arsed, not ergonomics.
Can anybody explain the rationale behind this?
Exceptions don’t exists and ask errors must be handled at every level. It’s infuriating.
The language was designed to be as simple as possible, as to not confuse the developers at Google. I know this sounds like something I made up in bad faith, but it’s really not.
The key point here is our programmers are Googlers, they’re not researchers. They’re typically, fairly young, fresh out of school, probably learned Java, maybe learned C or C++, probably learned Python. They’re not capable of understanding a brilliant language but we want to use them to build good software. So, the language that we give them has to be easy for them to understand and easy to adopt. – Rob Pike
"It must be familiar, roughly C-like. Programmers working at Google are early in their careers and are most familiar with procedural languages, particularly from the C family. The need to get programmers productive quickly in a new language means that the language cannot be too radical. – Rob Pike
The infamous if err != nil
blocks are a consequence of building the language around tuples (as opposed to, say, sum types) and treating errors as values like in C. Rob Pike attempts to explain why it’s not a big deal here.
a desperate fear of modular code that provides sound and safe abstractions over common patterns. that the language failed to learn from Java and was eventually forced to add generics anyway - a lesson from 2004 - says everything worth saying about the language.
btw lua handles error in exactly the same way
If this language feature is annoying to you, you are the problem. You 👏are 👏 the 👏 reason 👏 it 👏 exists.
I worked in places where the developers loaded their code full of unused variables and dead code. It costs a lot of time reasoning about it during pull request and it costs a lot of time arguing with coworkers who swear that they’re going to need that code in there next week (they never need that code).
This is a very attractive feature for a programming language in my opinion.
PS: I’m still denying your pull request if you try to comment the code instead.
That’s 👏 what 👏 CI 👏 is 👏 for
Warn in dev, enforce stuff like this in CI and block PRs that don’t pass. Go is just being silly here, which is not surprising given that Rob Pike said
Syntax highlighting is juvenile. When I was a child, I was taught arithmetic using colored rods. I grew up and today I use monochromatic numerals.
The Go developers need to get over themselves.
Yeah, insisting on things like a variable being used will result in people using work arounds. It won’t result in people not doing it.
Then, because people trust the language to police this rule, the work-arounds and debug code will get committed.
func main() { test := true }
Oops, golang doesn’t like that.
func main() { test := true _ = test }
Perfectly cromulent code.
If they really wanted to avoid people having unused variables, they should have used a naming convention. Any variable not prefixed by “_” or “_debug_” or whatever has to be used, for example. Then block any code being checked in that still contains those markers.
reading my code after being up for 18 hours and having my eyes glaze over trying to parse the structure of my monochromatic code but then I remember Rob Pike said syntax highlighting is juvenile so I throw my head against that wall for another 3 hours
What’s a situation where you need an unused variable? I’m onboard with go and goland being a bit aggressive with this type of thing, but I can’t think of the case where I need to be able to commit an unused variable.
That’s a problem with your workplace, not the language.
You could have a build setting for personal development where unused variables are not checked, and then a build setting for your CI system that will look for them. It gives you freedom to develop the way you want, but will not merge your PR unless the stricter rules are met.
Why in the world would you want to develop something that doesn’t follow the coding rules required by your org, just so you can go back and fix everything before submitting a PR? That’s just extra work.
I concur, it is a problem with that workplace. (In this case, OP is just sharing a funny meme. I wouldn’t suggest this meme means they’re a problem. I could have made this meme and I love the feature.)
Developing on a team at a company is like the “Wild West.” What’s considered to be acceptable will not only vary from workplace to workplace, but it can also fluctuate as developers and managers come and Go. Each of them have their own unique personality with their own outlook on what “quality” code looks like. (And many of them do not care about code quality whatsoever. They just need to survive 1-2 years there, make management happy with speedy deliveries, and then they can move on to the next company with a 30% pay bump.)
Having experienced working with developers who frequently filled with code base with unused code while having no control over who will leave or join as a contributor to the code base, I think features like this make for a more sane development experience when you’re developing with a team of seemingly random people that you never personally invited to contribute to the code base.
will not merge your PR unless the stricter rules are met.
This doesn’t fly when you work in big corporate and the boss doesn’t care about the code meeting stricter rules. “A working prototype? No it’s not- that’s an MVP! Deploy it to production now and move onto the next project!”
That’s what warnings are for. The jokes about programmers ignoring warnings are outdated - we live in an age where CIs run linters and style checkers on pull requests, there is no reason for a CI to not automatically reject code that builds with warnings.
I mean, yeah that kind of stuff absolutely should not be in production. However, it’s easy to see how it could be annoying while testing something while working on it. It being annoying doesn’t make it a bad feature, just as finding it annoying doesn’t make you a problem imo.
It is VERY annoying when you’re hunting for a bug. For example, when you comment out a function call to debug an issue, the compiler might suddenly complains about unused variables, so you’ll need to comment them out as well. Repeat several times and you’ll start having an urge to smack the monitor.
Lol new copypasta unlocked 🔓
🫡
It costs a lot of time reasoning about it during pull request and it costs a lot of time arguing with coworkers who swear that they’re going to need that code in there next week (they never need that code).
You should go to your team leader and ask them to enforce a coding standard. I agree with other commenters that said this should be a warning instead of an error.
I was working for a team that did quality control on the code of an entire financial group and it’s still amazing to me the shit we let through.
I feel annoyed even having compiler warnings in my code and here we were downgrading errors into warnings so the code would go through, or adding rules exceptions for a program so the team responsible could push a hotfix to prod…
It’s all shit. All the way down.
I dream of working with such a strict language.
I’ll start calling SQL “squeal” now in the spirit of this comment
No amount of propaganda will ever get me to pronounce it “sequel”. I’ll die in that hill.
Go is not a programming language. It’s an angry rant of a bored Google engineer.
IDK, Brainfuck is still classified as a programming language and Go is not that far behind it.
Also Go: exceptions aren’t real, you declare and handle every error at every level or declare that you might return that error because go fuck yourself.
Because that’s sane and readable?
Wow. I’m honestly surprised I’m getting downvotes for a joke. Also, no. It isn’t. It really isn’t.
It’s better than “invisible” exceptions, but it’s still the worst “better” version. The best solution is some version of the good old Result monad. Rust has the BEST error handling (at least in the languages i know). You must handle Errors, BUT they are just values, AND there’s a easy, non-verbose way of passing on the error (the ? operator).
I’m with you, exceptions sound good but are a bug factory.
There’s nothing sane and readable about how Go insists you format dates and time. It is one of the dumbest language features I’ve ever seen.
And I fucking love it. Thank you Go!
lol what’s wrong with you
lints that underline unused vars as errors, and not notes or warns are the worst lints…
This makes me not want to use Golang at all.
I assure you, the feeling is mutual.
You have to manually provide a seed every time you want a random number. Gophers will defend this with their dying breath.
This is not needed anymore since version 1.20
You go Go!
OP never said he/she commits such code but wants to iterate, test, explore.
Of course, unused var should not be part of a commit.
you can assign it to itself and it’ll be just fine. can’t put a breakpoint right on it, but it works
Me when my wife wants to buy new clothes (her clothes are the variables)
I hate this in C++ when it does this with parameters of an overidden function. I don’t need that specific parameter, but if I omit the variable name, I reduce readability.
[[maybe_unused]]
Which is have been able to use since we switched to C++17 … a couple of months ago.
Comment the unused variable out and no security hole gets accidentally shipped.
The best part of these threads is no matter what someone comments, at least 2 people will reply either correcting or “clarifying” the original commenter.
Lol
Isn’t it the same with ES6?
You’re confusing it with your linter. Java script don’t care.
That’s why I said ESLint
Depends on your eslint config, but yeah that’s an option.
that’s just clean coding. use your fucking vars
_, _, _ = unused1, unused2, unused3
¯\_(ツ)_/¯
Its little called google language.
DarkDarkHouse@lemmy.sdf.org 1 year ago
As your future colleague wondering what the hell that variable is for, thanks Go.
Willem@kutsuya.dev 1 year ago
I prefer for it to be just a warning so I can debug without trouble, the build system will just prevent me from completing the pull request with it (and any other warning).
iforgotmyinstance@lemmy.world 1 year ago
Changing it will bring down the entire system.
We’ve spent ten million dollars and do not know why.
Nioxic@lemmy.world 1 year ago
Isnt the syntax highlighting it as mever used?
So why would they wonder?
Camilo@discuss.tchncs.de 1 year ago
If it is a pure value, I’d assume yes, but if it is tied to a side effect (E.g. write its value to a file), then it would be not used but still could break your app if removed.
I’m not familiar with rust language specifically, but generally that’s what could happen
MJBrune@beehaw.org 1 year ago
A quick “find all references” will point out it’s not used and can be deleted if it accidentally gets checked in but ideally, you have systems in place to not let it get checked into the main branch in the first place.
Flarp@kbin.social 1 year ago
Yeah that should be looked for in a CI line check, not a compilation requirement
aport@programming.dev 1 year ago
You mean a system like the compiler
anemoia_one@lemmynsfw.com 1 year ago
Yeah any compiler should support environments or confit files. Our CI would never with without
–env “stage”
ennemi@hexbear.net 1 year ago
If only there was some way the compiler could detect unused variable declarations, and may be emit some sort of “warning”, which would be sort of like an “error”, but wouldn’t interrupt the build, and could be treated as an error in CI pipelines
iammike@programming.dev 1 year ago
Some people simply ignore warnings, that’s the main issue. Trust me, I saw this way too often.
If you cannot compile it than you have to fix it, otherwise just mark unused variables as ‘not an error’ via
_ = someunusedvar
.