Live data from Hacker News

Please do not attempt to simplify this code

github.com

261–270 of 327 posts

Re: Please do not attempt to simplify this code

#261
post #92
post #59

Earlier quoted context omitted.

There is no single canonical suite of best practices. There is also nothing harmful or unharmful about the length of a function or the lines of code in a file. Different languages have their opinions on how you should organise your code but none of them can claim to be ‘best practice’. Go as a language doesn’t favour code split across many small files.

> There is no single canonical suite of best practices. There kind of is, though. Most software engineering books argue for the same things, from the Mythical Man Month to Clean Architecture. > Different languages have their opinions on how you should organise your code In general best practices are discussed in a language-agnostic manner.

Software engineering is context dependent.

Take a look at e.g goto

Using goto in C is normal thing

Using goto in C# web dev will get you a weird look during review

Using goto in C# stdlib dev is viable

So as you see good practices arent just tech dependent, but also product (and many more) dependent

Re: Please do not attempt to simplify this code

#263
post #26

Earlier quoted context omitted.

100 missions. Not 100 code executions.

Sure, but I imagine at least some components only really execute a small number of times per flight, or possibly never in the case of certain error handling code. Stretching the metaphor more than is probably appropriate, I'd treat launching the shuttle and having it come back as a big integration test. A system that passes it's integration test 100 times isn't necessarily particularly impressive in terms of reliabil…

> A system that passes it's integration test 100 times isn't necessarily particularly impressive in terms of reliability.

So extending your own metaphor and using 100 as the number of missions, the integration test failed 2% of the time.

Re: Please do not attempt to simplify this code

#265
There's quite an interesting discussion from Richard Hipp on converting sqlite code to aviation standards https://corecursive.com/066-sqlite-with-richard-hipp/#testin...

>DO-178B. It’s a quality standard for safety-critical aviation products... Your tests have to cause each branch operation in the resulting binary code to be taken and to fall through at least once... took a year of 60 hour weeks... It made a huge, huge difference. We just didn’t really have any bugs for the next eight or nine years.

Re: Please do not attempt to simplify this code

#266

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…

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…

I’m not a dev, but I manage them. On one team they were spending many hours on code golf and nothing was being built. I pushed the devs to passing testing=PR accepted.

In your opinion, what problems might come from removing opinionated code reviews? Why do some reviewers gravitate toward “Here’s how I would have written it?”

Re: Please do not attempt to simplify this code

#267

Earlier quoted context omitted.

Because sometimes, there's "No Other Way(TM)". Arbitrary line limits tend to unnecessary fragmentation. Add includes, licenses, glue code and comment; and you have an unapproachable spaghetti. Try to keep methods to 200 lines in high performance code, and see your performance crash and burn like Icarus' flight. When you read the comments in the code, you can see that they simplified the code to a single module, and e…

> Try to keep methods to 200 lines in high performance code, and see your performance crash and burn like Icarus' flight. Are these loops in Kubernetes so hot that extra microseconds for some program stack manipulation will affect performance? I never took Kubernetes as a hyper-real time application. >Do not obey them blindly and create problematic code bases. I don't know the code so won't question it specifically,…

> Are these loops in Kubernetes so hot that extra microseconds for some program stack manipulation will affect performance?

Actually, looking at the code itself, pv_controller doesn't look overly hot, but extremely high value. In this case the long methods are intended to keep the logic confined, so one can read end to end and understand what is going on.

The code even doesn't use automatic type inference in Go (the := syntax), in some cases to make code more readable.

From what I understand, this code needs to be "kernel level robust", so they kept the overly verbose formatting and collected all three files to a single, overly verbose file.

I don't think this is a bad thing. This is an important piece of a scale-out system which needs to work without fault (debate of this is another comment's subject), and more importantly it's developed by a horde of people. So this style makes sense to put every person touching the code on the same page quick.

> I feel Space shuttle programming's job in many ways is in fact to try and remove ambiguity from code. But not by explaining the language, but the variables and their units. I sure wouldn't mind spamming "units in cm" everywhere or explaining every branch logic if it's mission critical.

A code comment needs to explain both the logic, and how the programming language implement this logic the best way possible. In some cases, an optimized statement doesn't look like what it's doing in the comment above it (e.g. the infamous WTF? comment from id Games which does fast_sqrt with a magic number). In these cases I open a "Magic Alert" comment block to explain what I'm doing and how it translates to the code.

This becomes more evident in hardware programming and while working around quirks of the hardware you interface with ("why this weird wait?", or "why are you pushing these bytes which has no meaning?"), but it also happens with scientific software which you do some calculation which looks like something else (e.g.: Numerical integration, esp. in near-singular cases).

> Not so much this inconsistent doxygen/javadoc style documentation on every variable/class. If you're going to go full entrprise programming, commit to it.

This is not inconsistent. It's just stream-of-consciousness commenting. If you read the code from top to bottom, you can say that "aha, they thought this first, then remembered that they have to check this too, etc." which is also I do on my codebases [0]. Plus inline comments are shown as help blobs by gopls, so it's a win-win.

I personally prefer to do "full on compileable documentation" on bigger codebases because the entry point is not so visible in these.

> "a proper linter configuraion would have really helped enforce these rules".

gopls and gofmt do great job of formatting the codebase and enforcing good practices, but they don't touch documentation unfortunately.

[0]: https://git.sr.ht/~bayindirh/nudge/tree/master/item/nudge.go

Re: Please do not attempt to simplify this code

#268
post #166

Earlier quoted context omitted.

tests -> verify intended functionality implementation (the how is right). comments -> why intended functionality was implemented that specific way (marketing wanted X because of Y, so we had to do it like Z with a bit of A). > But comments go out of date Just like updating the tests when code is changed, update the comment when the code is changed.

> Just like updating the tests when code is changed, update the comment when the code is changed. Well, yeah. But the point is that tests can be run in a pipeline that can fail if the tests fail. Comments going out of date has to get caught by a human, and humans make mistakes.

> humans make mistakes

All software is built by humans in some way. All software has mistakes.

Perfection is an impossible goal.

Re: Please do not attempt to simplify this code

#269

Earlier quoted context omitted.

Not even close to safest vehicle unless mean space vehicle. Space Shuttle was in orbit for 21k orbits and traveled 542 million miles. Which gives 28 deaths per billion miles. Airliners are running 0.01 deaths per billion miles. Driving is 15 per billion miles. So it was worse than driving. Airliners beat it by 3 orders of magnitude. Which isn't surprising when US airliners travel distance of Space Shuttle in less tha…

You are off by a factor of at least 5, because it's _passenger_ miles, not _vehicle_ miles. This is also why airlines are "so safe," because we put 300 people on them at a whack, if you were wondering where the "three orders of magnitude" actually comes from. It's still a man made machine being operated by humans. So it's 5.1. Three times safer than driving. You might apocryphally conclude they were at greater risk t…

Nah, the van goes slow and they have medical people right there, you could die there but it's essentially not going to happen.

Re: Please do not attempt to simplify this code

#270

Earlier quoted context omitted.

tests -> verify intended functionality implementation (the how is right). comments -> why intended functionality was implemented that specific way (marketing wanted X because of Y, so we had to do it like Z with a bit of A). > But comments go out of date Just like updating the tests when code is changed, update the comment when the code is changed.

Comments go out of date because of bad developers. The same people who do the bare minimum for tests not to explode. But won’t add a new test case for the new branches they just introduced. The same people who will mangle the code base introducing bizarre dependencies or copy paste the same piece of code rather than refactor. People who fail to handle errors correctly. My favorite: by wrapping code in a if statement…

> Comments going out of date is a shit excuse for not writing comments as far as I’m concerned.

I agree.

> Comments go out of date because of bad developers

I disagree.

Comments can also go out of date because

- developer is having a really shit time atm and their head is not in the game (bad looking after people management)

- developer is on a one day a week contract and doesn’t have the time in their contract to write big header comments explaining nuances (bad strategy)

- developer thought it looked obvious to them but it’s not obvious at review time (developer is being classic human)

- developer is getting pushed to deliver the thing now now now (bad workload management)

Most of those are the result of some decision made by someone who was not the developer (they’re all real cases). And they are the “non-code blockers” that good managers solve for us, so we can focus on getting good stuff done.

I’ve been where it seems like you are at. Blaming others for being bad didn’t help me. I had to lower my expectations of others, keeping my expectations for myself. Then get on about teaching willing people how they could be better. Might be making a few assumptions/projecting a bit there, but that’s my experience with “bad developers”.

Being any type of “leader” is lonely. Whether that’s an official role assigned to you or not. Or if it’s just a skill level thing. No one can quite match up to the expectations or understand what we see and why. But explaining it goes a long way with the right ones.

Post reply on HN