from Hacker News

Ask HN: Examples of bad open-source code to learn what to avoid?

by theSage on 5/19/19, 7:27 AM with 72 comments

What are some of the bad examples of code that you have seen? Something you would want to avoid?

I'm looking for examples which fall along the lines of "fail to see the forest for the trees".

  • by neya on 5/19/19, 10:48 AM

    I feel like this could lead to very opinionated, non-constructive comments/flame, but if were to give you an example, I'd suggest taking a look at Wordpress eco-system. While Wordpress core's codebase has improved significantly over the years, some of the plugins haven't.

    The top pick goes to WooCommerce, although an open source E-commerce solution on top of Wordpress, it has some terrible decisions under the hood.

    The top pick would go to mixing presentational logic with business logic. For example, to render a table, instead of exposing an array of objects to allow the developer to loop through it as he/she sees fit, WooCommerce will force you to use a PHP function that renders a table for you and there's actually no way to modify the presentation logic if you wanted to.

    It's a really fundamental programming paradigm that even top open source companies fail to adhere to.

    Again, I'm not saying this to attack them or the maintainers behind the code, just my opinion of why I think it's bad quality code while respecting the fact that developers still do take time and effort for us to enjoy something with freedom and zero cost.

  • by ben_bai on 5/19/19, 3:01 PM

  • by codr7 on 5/19/19, 10:39 AM

    It doesn't work, energy follows thought and it makes no sense to focus it on what you want to avoid. Take or leave.

    That being said, the worst I ever saw was the in-house business nonsense I was paid to deal with as a Java consultant. The worst code isn't open source from my experience, subjecting it to public scrutiny would mean suicide for the companies involved.

  • by jasode on 5/19/19, 1:33 PM

    An example of bad code that always stuck with me was the flawed CDDB disc id hash algorithm.[0]

    I was reminded of that short-sighted decision every time I ripped a bunch of CDs and saw how importing song titles was not automatic because a dozen different discs had the same hash ids which resulted in collisions[1]. It ended up creating needless friction for millions that depended on that discid.

    What's sad is I'm not even sure if one can extract any useful "lessons learned" from it! The programmer that wrote it was not an amateur script kiddie; he had a computer science degree from Uni California. Apparently, he didn't realize he was writing a flawed hash algorithm as he wrote it.

    One could say that hash algorithms should be "peer reviewed". Well, he got unsolicited peer review that pointed how his homegrown hashing computation was flawed but he ignored the suggestion to improve it.

    [0] >Ti Kan wanted to use a hash. He could have chosen something like CRC32, which would have given him a 32 bit number, yielding 4 billion unique IDs. Instead he wrote his own hash. [...] Ti Kan was made aware (not by me) of this problem back in 1994, and given a script to convert this format into a CRC32-based format, but he rejected it because the deployed base was too big. At that point it was probably in the high dozens. -- excerpt from http://quimby.gnus.org/circus/notes/cddb.html

    [1] https://forums.macrumors.com/attachments/multiple-matches-jp...

    [2] wiki: https://en.wikipedia.org/wiki/CDDB#How_CDDB_works

  • by AnaniasAnanas on 5/19/19, 10:51 AM

    Here you go https://github.com/progwml6/Natura/blob/1.7.10/src/main/java...

    Tip: If you ever end up in a situation where you have to copy-paste code with minor changes then there is something that you are doing wrong. In this case using arrays and loops would be a much better solution.

  • by spion on 5/19/19, 12:26 PM

    How about examples of open source code to learn whats really, really good, together with why it was designed that way? Seems like that would be way more useful.

    Or examples of projects that did things one way, but later refactored, and why they refactored.

  • by kissgyorgy on 5/19/19, 3:03 PM

    That's a really bad idea. You need to have a good counterexample, otherwise it's just wasting time at the best case. A lot of people learn from really bad codebases and picking up the same style which is terrible. You should look at GOOD codebases instead!
  • by otras on 5/19/19, 3:06 PM

    If you’re interested in a case of unnecessary optimization and effort, the infamous left-pad npm library has been refactored to only add to the string O(log(n)) times. It is short but not sweet.

    https://github.com/left-pad/left-pad#readme

  • by Sir_Cmpwn on 5/19/19, 3:21 PM

    Here's some old code of mine:

    https://github.com/vatt849/LibMinecraft/blob/master/LibMinec...

    The whole library is a trip if you want to read a bunch of bad C#. Highlights:

    - Generated documentation

    - Giant switch/case instead of a more organized dispatch map

    - Large swaths of commented code instead of using version control

    - try...catch statements that just eat the errors

    - Inconsistent code style

    - This thing:

    https://github.com/vatt849/LibMinecraft/blob/master/LibMinec...

    I've written something similar from scratch since, which I'm still not entirely satisfied with, but is much better for reference:

    https://github.com/ddevault/TrueCraft

    The client-side networking code lives here:

    https://github.com/ddevault/TrueCraft/blob/master/TrueCraft....

    https://github.com/ddevault/TrueCraft/blob/master/TrueCraft....

    https://github.com/ddevault/TrueCraft/tree/master/TrueCraft....

    Notable improvements:

    - Handwritten docs only where necessary

    - Uses a stream implementation for decoding this particular wire format

    - Has a different and better abstraction for reading packets out

    Still has bad error handling though.

  • by slezyr on 5/19/19, 11:43 AM

    See Syobon Action it's source code as bad as the game itself.

    https://github.com/angelXwind/OpenSyobonAction/blob/master/m...

  • by jstarfish on 5/19/19, 6:51 PM

    The source for Terraria is notoriously terrible.

    https://github.com/TheVamp/Terraria-Source-Code

    It is another example of how even inelegant code full of hardcoded values can be successful.

  • by twhitmore on 5/19/19, 12:05 PM

    Libraries can be useful despite imperfections, and poor design decisions can occur in overall good libraries. So we can't judge too harshly.

    Having said that:

    1) iText PDF library used to have some fairly poor & duplicated code. Column layout was a highlight. Also strange ideas overemphasizing subclasses, eg. for paragraph styles. (Correct approach: use values rather than types.)

    2) Tomcat webserver back around 2007 used to have some amazing 'clustering' code to deploy your webapp across multiple servers. But it lacked proper knowledge & hence control of what it was doing. IIRC there was no clear master, and a server couldn't tell what had been started on it versus what had been replicated since a peer was seen to be running it. Effect: replication would be additive only, contexts would just replicate everywhere uncontrolled, and there was no good way to stop/ undeploy an app across the cluster.

  • by ddebernardy on 5/19/19, 12:42 PM

    OpenSSL (before HeartBleed) springs to mind.

    https://news.ycombinator.com/item?id=7640378

  • by type0 on 5/19/19, 3:31 PM

  • by blattimwind on 5/19/19, 1:16 PM

    Drupal 6/7 would be an example for "widely used [at the time] but pretty bad". I don't know how many of the issues across all layers were addressed in later versions.

    OpenSSL is still an excellent example for very messy code where even maintainers / frequent contributors regularly get lost. Also a good example for designing many bad APIs and poor docs. libsodium is a good counterexample, although the internal structuring of the code base is a bit atypical, it is logical and consistent. (It does have some API idiosyncrasies which cater specifically to dynamic bindings, like providing a constant always as a #define/macro but also as an exported function; and it has a bit of an issue where you have both legacy APIs and newer APIs, but the docs are pretty clear on which is which).

    BorgBackup is an example of how you don't want to mix C and Python code, and also contains various bits that only 1-2 people on the planet really bothered to understand, besides demonstrating other issues of organically grown code bases.

  • by bradknowles on 5/19/19, 5:00 PM

    There are an infinite variety of things that you don’t want. If you focus all your energy on those things, then you won’t have anything left to do the positive things you do want.

    You have to turn that equation around. Even if all you know right now is the negative thing you don’t want, you have to figure out how to reframe that into the positive thing you do want. On,y then can you make positive progress towards that thing you want — and by the way, you will naturally avoid the things you don’t want by focusing on the things you do want.

    Sure, examples of bad stuff can be instructive, but only so far as it helps you further clarify the good stuff you’re actually trying to achieve.

  • by superpermutat0r on 5/19/19, 2:54 PM

    I've always found Calibre to be a huge mess.
  • by arthev on 5/19/19, 1:37 PM

    While usually focusing on smaller snippets, thedailywtf.com is a site about (mostly) code wtfs. A fair number of management wtfs too, though.
  • by pjc50 on 5/19/19, 1:24 PM

    > fall along the lines of "fail to see the forest for the trees"

    I think I'm going to need an example to understand this?

    Having said that, one of the most informative programming books I've ever read was C Traps And Pitfalls. Flags common easily-made errors and explains them, which in turn fixes misconceptions about the language. I feel most languages could do with one.

  • by ksaj on 5/19/19, 5:25 PM

    Here is something atrocious I wrote in Lisp. You actually can make Lisp ugly! Who knew?

    https://github.com/ksaj/Capitalize.Lisp

  • by RickJWagner on 5/19/19, 1:34 PM

    Just a side note-- this is the beauty of Open Source. If there is some bad code (we all write it), it can be improved with a little help from Open Source "friends".

    All of us are stronger than any one of us. Long live Open Source!

  • by craftoman on 5/20/19, 4:50 AM

    Many JavaScript libraries like Fastify (Node.js) for example. You always get a nice & clean API but if you look under the hood you would be amazed at how much spaghetti code can be written in a project.
  • by frostburg on 5/19/19, 3:22 PM

    Praat: https://github.com/praat/praat I'm not sure that this is a good way to learn anything, however.
  • by sam_lowry_ on 5/19/19, 12:28 PM

    Jgit is amazingly bad for a piece of software built on top of well thought out data structures if git. Some if its flaws could be attributed to Java IO design, though.
  • by peterwwillis on 5/19/19, 3:22 PM

    Anything related to OpenStack, but particularly jenkins-job-builder is rather horrible.
  • by jxub on 5/20/19, 11:33 AM

    Many, if not most OSS packages which are released by academics or universities.
  • by yamann on 5/19/19, 10:49 AM

    https://github.com/mholt/caddy not only a mediocre code, but the guy behind it received lots of money from Mozilla as an innocent promising open source project author, then he made it as a paid product.