logoalt Hacker News

analog31 • yesterday at 9:12 PM • 12 replies • view on HN

Take this from a rank amateur "scientific" programmer. I write functions with maybe a half dozen lines, then I piece them together. Sometimes my programs work, sometimes they don't, and I fix them. I've done it this way since I first learned Pascal 40+ years ago. I'm not allowed to touch production code, so don't panic.

When I ask AI to write code with similar functionality, it's about 10x more code. Likewise when I look at the code for well respected open source projects. It's good code in terms following accepted practices, but hard to read because there's too much of it to see where the actual "working" code is.

A lot of the code is "defensive," i.e., type and bounds checking, trapping error conditions, etc. When I've looked at the unit tests, most of them test the defensive code.

I tell my engineering friends: "Engineers like to over-complicate things." Maybe the AI has picked up on this and is serving it back to us.


Replies

jrmg • yesterday at 9:39 PM

> I write functions with maybe a half dozen lines, then I piece them together. Sometimes my programs work, sometimes they don't, and I fix them.

I think you might be under a misapprehension about how non-rank-amateurs code. Almost all coding is trial-and-error if we're honest. No-one, no matter how well they plan, writes towering pristine monoliths of code.

The code might sometimes look like that after they're done, but that's only because so much time has been spent testing and 'polishing'.

Most - maybe all - professional programmers I've worked with spend longer in the debugger or inserting and reading logs than they do in writing code.

Sometimes it's possible to plan an architecture beforehand and implement it as-designed, but even that is usually a fool’s errand - the plan needs to change as the architecture meets reality and unforeseen complications arise.

➕ show 4 replies
Ekaros • today at 9:45 AM

Defensive coding often is added because shit happens. So you need to take in account lot of scenarios you did not even consider at first.

My latest example is verifying JWT token. Works fine. Then just for some testing run with old token... Not verifying against any key I stored... Keys are pulled at start... And the service which sings JWTs might change them... Well now I needed to add logic to repull keys.

For long lived projects this sort of stuff happens over and over again. And thus more of these edge cases are added. And they are actually needed to solve some edge scenario. Even if that edge scenario can be very rare.

metalcrow • yesterday at 9:43 PM

Isn't defensive code just outright better to do? Sure if you're making scripts for single use or small one-offs, it's unneeded. But the moment you make a usable product or a library or a tool that more people use, if your code isn't defensive won't the program just be strictly worse? It's always better to avoid crashes or bugs or exploits and you need to be defensive to do that.

➕ show 4 replies
pxc • yesterday at 10:06 PM

I have a computer science degree and have bounced around security engineering, software engineering, and DevOps roles. I currently produce code for a living, although it's internal tools rather than application code for end users.

LLMs typically write 2x-25x as much code as they should. Occasionally they write 50x as much code as they should. It's not mimicking competent developers, or even the "over-complication" of perfectionists. It's plain bad.

➕ show 2 replies
aetherspawn • yesterday at 9:35 PM

In python yes, but if you add or use only static types it clears this all up and the LLM will generate short, optimal code without edge case fluff.

cataflam • yesterday at 10:34 PM

> Likewise when I look at the code for well respected open source projects. It's good code in terms following accepted practices, but hard to read because there's too much of it to see where the actual "working" code is.

> A lot of the code is "defensive," i.e., type and bounds checking, trapping error conditions, etc. When I've looked at the unit tests, most of them test the defensive code.

> I tell my engineering friends: "Engineers like to over-complicate things."

I took your message to be in a half-joking tone and I mean mine in the friendliest spirit, from someone who's been where you are, in response to the half-serious part :)

I used to be an "amateur scientific" programmer like you, learned Pascal ~35 years ago, used Fortran and Delphi extensively (my field was experimental physics). At some point, I switched careers and become a "professional" programmer, working on operating systems and embedded software.

Your code simply has (more) bugs. All those unchecked bounds, uncaught error conditions, etc. Your short functions probably work fine on most inputs you give them and, if they occasionally don't, with some luck you notice and you're there to fix them (or if you're less lucky, it propagates into some calculations and results unnoticed...)

I had a hard time accepting it the first time I was told. "But that's not a bug, it's "just" an uncaught memory allocation error." Yeah, that's a bug.

That defensive code probably has bugs too, but fewer bugs. It really depends on the field and the application, and the ease of update / consequence of failure, how fewer bugs you need. For production code, that runs unattended, where uncaught errors can have bad consequences, the 10x extra code is there. That's also why most of the tests focus on those edge cases (the main path will get tested heavily every time you're running the program anyway).

I don't mean your approach is wrong for you, it may very well be best for your use cases. But that "over-complication" is there for very good reasons.

➕ show 1 reply
14113 • yesterday at 9:28 PM

Software engineers like to over-complicate code in the same way that structural engineers like to over-complicate buildings.

➕ show 1 reply
spoiler • yesterday at 9:32 PM

I think a lot of the complexity comes with having to handle unknowns or weird edge conditions, most of which are there because code runs on hardware which follows the laws of physics. So really, we need to blame the physicists for not drafting better laws!

➕ show 1 reply
rf15 • yesterday at 9:46 PM

my experience has been similar, working for over 20 years as an egineer: engineers like to talk too much, even with their code.

dude250711 • yesterday at 9:33 PM

> When I ask AI to write code with similar functionality, it's about 10x more code.

Using AI is still a skill in 2026. You do not have to one-shot it, you can prompt it to reduce the amount of code e.g. by dismissing unlikely edge cases or pointing it at some preexisting reference code.

antonvs • today at 9:57 AM

Sorry, but what you're describing sounds like ignorance in your part.

Code that needs to run in production environments has to deal with all sorts of issues that go far beyond just doing some sort of calculation. Scaling, concurrency, data integrity over time, maintenance over time, edge cases, code worked on by tags if people - all sorts of non-functional requirements which are not obvious to an amateur, which is what you are in this context.

If you're unfamiliar with all of that - as most scientific and ML programmers are - you're just not going to even recognize it when you see it, or recognize the need for it. And if you have the kind of personality that assumes anything you don't understand is unnecessary, you end up posting comments like the above.

tayo42 • yesterday at 10:04 PM

I feel like alot of code complexity comes from making it testable in practice. To make unit testing possibly you need so many ways to hook into things and you need abstraction everywhere to make it possible.