Live data from Hacker News

Ask HN: What's the largest amount of bad code you have ever seen work?

news.ycombinator.com

261–270 of 601 posts

Re: Ask HN: What's the largest amount of bad code you have ever seen work?

#261

As a budding programmer (hobbyist), what can I do not to fall into the mistakes others talk about in the comments? Is there any particular set of mental habits that could lessen the chance of spaghetti occurring, or is it just a matter of when rather than if?

Here's some rule I'm using to keep my code maintainable (to me):

- Line should not be too long (120 max)

- Function should fit into 1 page in the view port of screen, even when your console and debugger take 1/3 bottom part of the screen.

- Variable name should be pronounceable and longer than 3 letters (to prevent name like `i`, `x`, `s`)

- File should not be too long (1000 lines max).

- Return/Exit/Throw as early as possible.

- Comment:

    - If an `if` take more than 2 condition, it's worth commenting.

    - If a funciton is longer than 10 lines, it's worth commenting.

    - All file's worth commenting.

    - If you ask yourself "should I add comment", add comment.

Re: Ask HN: What's the largest amount of bad code you have ever seen work?

#262
post #60

Ten years ago I was called in to remediate a new web application which had been subcontracted to an Indian development company. The PHP developers who'd put it together evidently didn't know about classes, and each page in the application was hundreds, sometimes thousands, of lines of spaghetti code, most containing the same duplicated (but subtly changed) blocks providing database connectivity etc. Security had not…

10 years ago PHP didn't have classes

Re: Ask HN: What's the largest amount of bad code you have ever seen work?

#263
post #168

I am maintaining one application in construction industry space. That application was created 25 years ago by construction worker that never wrote single line of code before, but because he caused a lot of problems on construction site they give him Programming 101 book and let him build it. 15 years later the app was close to half milion lines long of huge bowl of spaghetti code. Only comments in whole codebase were…

>> there is not enough budget to actually rewrote whole system,

As pointed out elsewhere, this is definitely solving a real problem, the longevity of the app is the proof.

Instead of rewriting, can you replace it with newer idioms? A MVP/PoC for a newer way of solving a problem (AR may be here) that the software solves with some tangible gains, the latter is more important, can lead to approval of a mini budget for that MVP and who knows what that can lead to.

Re: Ask HN: What's the largest amount of bad code you have ever seen work?

#264
post #97
post #60

Ten years ago I was called in to remediate a new web application which had been subcontracted to an Indian development company. The PHP developers who'd put it together evidently didn't know about classes, and each page in the application was hundreds, sometimes thousands, of lines of spaghetti code, most containing the same duplicated (but subtly changed) blocks providing database connectivity etc. Security had not…

> most containing the same duplicated (but subtly changed) blocks I had a similar experience with an offshore company. This was during the early-mid 2000s, at the height of the offshoring era when $10/hour programmers in India were aplenty and everyone felt their job was soon to be outsourced. Turns out, no joke, they were being paid per line of code. Despite having to maintain the heap of crap, I was amazed at the b…

What sorts of techniques did they use? I'm very curious to see/hear examples.

Re: Ask HN: What's the largest amount of bad code you have ever seen work?

#265
post #186

Earlier quoted context omitted.

I had a very very similar experience. I saw the craziest code in my entire career while debugging performance issues. Even though they were using a PHP MVC framework, they were pulling every record from a db table to iterate over to find the record using PHP string compare functions. I still can't believe it. The dev shop I worked for back then was even in the habit of hiring multiple teams for the same project in th…

> pulling every record from a db table to iterate over to find the record using PHP string compare functions As horrible as this sounds, this is actually good from a refactoring point of view because it should be straightforward to rewrite it to use actual queries.

D':

Re: Ask HN: What's the largest amount of bad code you have ever seen work?

#266
I used to own a C++ application that was a morass of abstractions and indirections so it was impossible to reason about. It took a number of hours to compile.

On one infamous occasion we were making a, relatively small, patch release. The debug version worked fine but the release version crashed systematically. Even when we backed out all the changes we had the same behaviour. We were screwed.

Until one of the team had a bright idea. She stripped strings from the debug build and tested it. To our surprise it not only worked, it was only slightly bigger than the previous release version and it was also slightly faster! We shipped.

This experience was the trigger to make me go all-in on a full re-write that I had been contemplating. One of only a couple of times in my career that I've made that decision on a major piece of software.

The re-write was a huge success. It was also about 10% of the original in terms of LoC. The day our testing finished, we held a ceremony where we deleted all the old code from the current version.

This caused a slightly different issue. At the time, code metrics were starting to get fashionable but LoC wasn't yet the pariah it became.

So, a couple of days later I got a concerned call from the metrics guys. Apparently, we had deleted more code than all the other teams combined had added in the previous measurement period. This caused their metric calculation to barf. Their solution? We should add all the code back in! This led to a somewhat heated argument that ended up with me persuading them that deleting code was good and they should, at least, abs(LoC) it. It didn't make the metrics any more useful but meant that we had an application we could reason about. Happy days.

Re: Ask HN: What's the largest amount of bad code you have ever seen work?

#267

As a budding programmer (hobbyist), what can I do not to fall into the mistakes others talk about in the comments? Is there any particular set of mental habits that could lessen the chance of spaghetti occurring, or is it just a matter of when rather than if?

I think the best starting point is

Robert Martin, Clean Code.

Most people that have read the book (and it is a classic so many have) will swear by it. I most certainly do.

Re: Ask HN: What's the largest amount of bad code you have ever seen work?

#269
Two projects with 300000 lines of Python code each at a small company handling physical access control systems with smartcards and stuff. Once I looked the most critical part; the logic which decides if a door should be opened or not and it was implemented with the assumption that in Python, the bitwise operators work the same way as in C (they doesn't at all).

Re: Ask HN: What's the largest amount of bad code you have ever seen work?

#270
post #134

Earlier quoted context omitted.

This is a complete fallacy IMO. The time is 10 fold down the line when people are attempting to reverse engineer in order to maintain it.

Presumably, down the road, you'll either be a defunct company or doing well enough to afford 10 times the manpower to fix things. Facebook was a spaghetti code mess in the beginning. I'm sure it caused them some growing pains, but moving too slowly early on would have likely been more costly.

I'm completely opposed to the view that bad craftsmanship is acceptable because of time constraints. You are paying for it very dearly, very soon. It is of the utmost importance to write the best code you can from the beginning, and I don't believe it slows you down very much, if at all.

If you've ever seen a software product where something that should take a weekend takes months to get out, it's often not because the problem is more complicated than you'd think, but because of a mangled, complex codebase which prevents anyone from getting real work done.

Edit: Removed a bunch of redundancy.

Post reply on HN