Top products from r/badcode

We found 3 product mentions on r/badcode. We ranked the 3 resulting products by number of redditors who mentioned them. Here are the top 20.

Next page

Top comments that mention products on r/badcode:

u/Magical_Gravy · 61 pointsr/badcode

My bad.

In Object Oriented Programming (OOP), there are lots of design patterns that end up getting repeated all over the place. You might have run in to the factory pattern, or perhaps the builder pattern?

If you can understand and notice these patterns, it means you can re-use old code more effectively, because code to handle a pattern in one place is probably very similar to code to handle a pattern in another.

In addition, if you're discussing a problem with somebody, it means you can refer to the patterns by name as a sort of shorthand notation for "put it together like this". Saying "use a decorator" is a lot quicker and easier than describing what exactly "a decorator" is from scratch every time.

The "Gang of Four" are four Computer Scientists who were among the first few to notice that these patterns kept popping up, and wrote a pretty well known book describing about 20 of the most common ones.

In this specific instance, the builder pattern would probably have proved useful. Rather than having a single, monolithic constructor, you create a separate "builder" class.

Character chararacter = new Character(xx, yy, life, kpph, kpyl, kvin, krak, kgr, kptgr, kbb, havepph, havepyl, havevin, haverak, haveya, isevented, x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, IE)

Can become

Character character = Character.builder()
.life(life)
.initialCoordinates(x1, y1)
...
.build()

This is waaayyy more readable (especially if you're assigning values as arguments, rather than named values. If you ever called createFrom(...) with a string of numbers, it'd be very difficult to work out which number was what), and a lot easier to lay out properly. It also means you can gather arguments for creation gradually over time rather than all at once.

Also looking more closely, and as /u/PM_ME_YOUR_HIGHFIVE pointed out, they're not actually using objects at all, which would be a good place to start.

u/antonivs · 18 pointsr/badcode

The code you posted was generated from a grammar definition, here's a copy of it:

http://www.opensource.apple.com/source/bc/bc-21/bc/bc/bc.y

As such, to answer the question in your title, this is the best code you've ever seen, in the sense that it embodies some very powerful computer science concepts.

It [edit: the Bison parser generator] takes a definition of a language grammar in a high-level, domain-specific language (the link above) and converts it to a custom state machine (the generated code that you linked) that can extremely efficiently parse source code that conforms to the defined grammar.

This is actually a very deep topic, and what you are looking at here is the output of decades of computer science research, which all modern programming language compilers rely on. For more, the classic book on the subject is the so-called Dragon Book, Compilers: Principles, Techniques, and Tools.

u/-manabreak · 1 pointr/badcode

For java, check out Effective Java by Joshua Bloch. It's basically a book around this kind of stuff and should be read by every Java developer.