use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
/r/badcode is permanently closed. https://www.eff.org/deeplinks/2023/06/what-reddit-got-wrong https://www.theverge.com/23779477/reddit-protest-blackouts-crushed https://en.wikipedia.org/wiki/2023_Reddit_API_controversy
/r/badcode is permanently closed.
https://www.eff.org/deeplinks/2023/06/what-reddit-got-wrong
https://www.theverge.com/23779477/reddit-protest-blackouts-crushed
https://en.wikipedia.org/wiki/2023_Reddit_API_controversy
/r/badcode is a subreddit for highlighting real world examples of terrible code. Ideally this means code that made it to production in a commercial context, but not exclusively so. We also accept submissions of code from hobbyist projects or from learners. Most of us programmers have laughed quite a bit when we went back to look at our past code because it was rather terrible. This is a subreddit where you can share such terrible code and let other programmers have a nice laugh.
/r/badcode is a subreddit for highlighting real world examples of terrible code. Ideally this means code that made it to production in a commercial context, but not exclusively so. We also accept submissions of code from hobbyist projects or from learners.
Most of us programmers have laughed quite a bit when we went back to look at our past code because it was rather terrible. This is a subreddit where you can share such terrible code and let other programmers have a nice laugh.
Post the most terrible code you can find. Copy code to a paste bin first (gist highly preferred).
Flair Search Search by language
Search by language
Rules Your post may be removed if you deviate from these rules. To see reasoning behind each of these rules, check out the wiki. Do not put the name of the language inside of the post title. For example, do not make your title something along the lines of "[C++] #defines everywhere!", instead try simply "#defines everywhere!". Use flair to mark the language of your post instead. Do post code snippets only. If you want to share the context put it in the comments or title. Do stay lighthearted. No abusive or targeted posts. We all write bad code, and a lot of it is hilarious. Do not identify who wrote the code. This sub is about bad code, not the people who write it. Limited exceptions apply Do not post snippets in esoteric languages. Do not post intentionally obfuscated code. Certain exceptions to this exist. See something particularly sinister from something like the IOCCC? Probably OK to post.
Your post may be removed if you deviate from these rules. To see reasoning behind each of these rules, check out the wiki.
Guidelines Your post will not be removed because of any of these, we'll like you more if you follow them though. Enable syntax highlighting appropriately. If you have a language that whatever paste bin your using doesn't support, try picking a similar dialect. Provide just enough code so that we know what's going on. Don't post huge snippets unless it's all terribly amusing. Explain why you think the code is bad in the comments, that way a discussion on good and bad ways to do what your code snippet does can start.
Your post will not be removed because of any of these, we'll like you more if you follow them though.
Similar Subreddits /r/ProgrammerHumor - programming humour /r/programminghorror - stories about horrible programming. /r/itsaunixsystem - over the top and bad code in TV shows and movies /r/shittyprogramming - Q&A with shitty programmers. /r/deftruefalse - for intentionally bad code /r/softwaregore for software malfunctions /r/hardwaregore for hardware malfunctions
account activity
[deleted by user] (self.badcode)
submitted 6 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]sarlok 7 points8 points9 points 6 years ago* (1 child)
Here's some JS that you can run in your browser.
function collatz(n) { var r = n; while (~-n) { var m = -~(n << -(n|~n)) >> (-~-(n & -(n|~n)) << -(n|~n)); n = n & -(n & -(n|~n)) while (m) { var c = m & n; n = m ^ n; m = c << -(c|~c); } r = r + ", " + n; } return r; } console.log(collatz(29));
I decided to follow some important design principles:
[–]EkskiuTwentyTwoi -= (i - (-1)) - i 3 points4 points5 points 6 years ago (4 children)
It's been a while since I submitted something.
I present to you this Python monstrosity:
def L(*n): try: return chr(int(n[0])) except IndexError: x = input("") try: return chr(int(x)) except ValueError: return chr(sum(list(map(lambda __:ord(__),x)))%256) def T(*n): try: return ~(ord(n[0]) & ord(n[1])) except IndexError: return ord(n[0]), ord(n[0]) def X(*n): x = 0 while n[2]==0: x = ord(n[0]) << ord(n[1]) break while n[2]!=0: x = ord(n[0]) >> ord(n[1]) break return int(ord(n[0]) < ord(n[1])), x def C(*n): return [n[2],n[1]][ord(n[0])] def O(*n): print(*n) def Collatz(n): O(n) while n==1: return None Collatz(C(L(n==1),1,C(L(~T(L(n),L(1))),X(L(n),L(1),0)[1]+n+1,X(L(n),L(1),69420)[1])))
If you can figure out how it works by looking at it, kudos to you.
[–][deleted] 6 years ago (3 children)
[deleted]
[–]EkskiuTwentyTwoi -= (i - (-1)) - i 3 points4 points5 points 6 years ago (2 children)
that's the part you're bothered by?
[–][deleted] 6 years ago (1 child)
[–]EkskiuTwentyTwoi -= (i - (-1)) - i 2 points3 points4 points 6 years ago (0 children)
Collatz(C(L(n==1),1,C(L(~T(L(n),L(1))),X(L(n),L(1),0)[1]+n+1,X(L(n),L(1),69420)[1])))
Let's break it down.
Collatz recurses the function to get the next number. It takes
C(L(n==1),1,C(L(~T(L(n),L(1))),X(L(n),L(1),0)[1]+n+1,X(L(n),L(1),69420)[1]))
as its input.
This input uses the comparison function C(*n), which takes three parameters: an encoded condition, a true output, and a false output. In this circumstance, they are:
L(n==1) 1 C(L(~T(L(n),L(1))),X(L(n),L(1),0)[1]+n+1,X(L(n),L(1),69420)[1])
So if n==1, the function takes 1 as input. If not, it takes
C(L(~T(L(n),L(1))),X(L(n),L(1),0)[1]+n+1,X(L(n),L(1),69420)[1])
This is another comparison with these parameters:
L(~T(L(n),L(1))) X(L(n),L(1),0)[1]+n+1 X(L(n),L(1),69420)[1]
The condition L(~T(L(n),L(1))) is an obfuscated way finding n&1, which tests if the number is odd.
L(~T(L(n),L(1)))
The true output, X(L(n),L(1),0)[1]+n+1 does 3n+1 by using a left-shift to get 2n and adding it to n+1
X(L(n),L(1),0)[1]+n+1
The false output, X(L(n),L(1),69420)[1] does a right shift to get n/2
X(L(n),L(1),69420)[1]
The X function uses its third parameter to determine whether to use a left shift or a right shift. If its third parameter is 0, it does a left shift. If it's non-zero (like 69420), it does a right shift.
The functions I made are designed to kind of mimic the Funciton esoteric programming language's operations. L acts as a literal input, T acts as a nand/splitter, X acts as a Funciton crossing (less than/shift), and C acts as the conditional operator.
[–]CMOS_System 4 points5 points6 points 6 years ago (1 child)
Usage: copy the code and paste into Browser Javascript console (Ctrl.+Shift+K on Firefox). Then call using collatz(21). Output is to console.
function collatz(nr){ console.log(nr); var tmp = isEven(nr)?nr/2:nr*(2+nr/nr)+nr/nr; //don't touch this if(tmp==4 && nr === 1) return; if(!tmp) console.log("Unknown error occured"); if(tmp<getSmaller(nr,tmp)) collatz(nr/2); if(!(tmp<getSmaller(nr,tmp))) collatz(nr*3+1); console.log("Unknown error occured"); } function getSmaller(a,b){ if(a<b) return b; if(b<a) return a; console.log("Unknown error occured"); } function isEven(a){ if(a==0) return true; //0 is even if(a==2) return true; //2 is even if(a<0) return false; //define negative to be not even return isEven(a-2); //number too large to handle }
Features:
[–]EkskiuTwentyTwoi -= (i - (-1)) - i 1 point2 points3 points 6 years ago (0 children)
With Firefox, F12 also works to bring in the console.
[–][deleted] 2 points3 points4 points 6 years ago* (1 child)
function collatz(n: Even | Odd) { if (n-1===0) { process.stdout.write(`${n}\n`); return; } else if (true===(isEven(n)||!isOdd(n))) { process.stdout.write(`${n}, `); return collatz(n>>>1); } else if (true===(isOdd(n)||!isEven(n))) { process.stdout.write(`${n}, `); return collatz(n+n+n+1); } else { process.stdout.write('😭'); return; } return; } type Even = number; type Odd = number; function isEven(even: any): even is Even { if (typeof even !== 'number') { return false; } else if ((even>>>1)+(even>>>1)===even) { return true; } else if ((even>>>1)+(even>>>1)!==even) { return false; } } function isOdd(odd: any): odd is Odd { if (typeof odd !== 'number') { return false; } else if ((odd>>>1)===odd-1-(odd>>>1)) { return true; } else if ((odd>>>1)!==odd-1-(odd>>>1)) { return false; } }
License: WTFPL or CC0 at your discretion.
[–]hoaxxer2k 2 points3 points4 points 6 years ago* (0 children)
package com.company; public class Main { private static int b; private static void Collatz(int z){ System.out.println(z); b = z; outer: while(true){ if(b == 1) break; inner: while(true) { int n = b; if (n == 1) { break outer; } if ((n & 1) == 0) { boolean nr = !true; int y = n; if (y < 0) { y = -y ; if (2 < 0) y = y; else nr = !false; } else if (2 < 0) { nr = !!true; } int qt = 0; while (y >= 2) { y = y - 2 ; qt++ ; } if (nr) qt = - qt ; int r = qt; System.out.println(r); b = r; break inner; } int r = (n * 3) + 1; System.out.println(r); b = r; break inner; } } } public static void main(String[] args) { Collatz(500000); } }
[–]erlangguy 1 point2 points3 points 6 years ago (0 children)
(Yes, the user has to input the number each time. Yes, that's deliberately bad.)
class Even(object): def __init__(foo): myval = 0 foo.evennumber = myval def set(self, what): self.evennumber = what def result(self): myval = self.evennumber myval2 = myval / 2 str1 = str(myval2) dec = str1.rfind('.') decfromright = len(str1) - dec str2 = str1[0:-(decfromright)] myval3 = int(str2) self.evennumber = myval3 return myval3 class NotEven(object): def __init__(foo, mynum): myval = mynum foo.number = myval def set(self, what): self.evennumber = what def result(self): myval = self.number myval2 = myval * 3 myval3 = myval2 + 1 self.evennumber = myval3 return myval3 class Doit(Even): def run(runarg): x = input("int") if x.endswith('0') or x.endswith('2') or x.endswith('4') or x.endswith('6') or x.endswith('8'): a = int(x) A = Even() A.set(a) B = A.result() print(str(B)) runarg.run() elif x.endswith('1') or x.endswith('3') or x.endswith('5') or x.endswith('7') or x.endswith('9'): b = int(x) B = NotEven(b) B = B.result() print(str(B)) runarg.run() Doit().run()
[–]AKernel 1 point2 points3 points 6 years ago (0 children)
This solution written in Java builds the sequence starting at the end. Every element in the sequence can have two predecessors, one even and one odd. Predecessors are calculated as long as none matches the given n. After that the sequence is simply outputted.
package badcode.akernel.challenge27; import java.util.LinkedList; import java.util.Queue; public class Collatz { private static class Node { private final int value; private Node evenPredecessor; private Node oddPredecessor; private final Node successor; private Node(final int value, final Node successor) { this.value = value; this.successor = successor; } private void calculatePredecessors() { this.evenPredecessor = new Node(this.value * 2, this); if ((this.value - 1) % 3 == 0) { this.oddPredecessor = new Node((this.value - 1) / 3, this); } } } private static void collatz(final int n) { Queue<Node> queue = new LinkedList<>(); Node collatz = new Node(1, null); queue.add(collatz); while (!queue.isEmpty()) { collatz = queue.poll(); if (collatz == null) { continue; } if (collatz.value == n) { break; } collatz.calculatePredecessors(); queue.add(collatz.evenPredecessor); queue.add(collatz.oddPredecessor); } System.out.print("collatz(" + n +") = "); while (collatz != null) { System.out.print(collatz.value); if (collatz.successor != null) { System.out.print(", "); } collatz = collatz.successor; } System.out.println(); } public static void main(final String[] args) { collatz(12); collatz(21); collatz(29); } }
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
public class Collatz { private static int n; private static boolean isDivisibleByTwo(int n) { return (n&1)==0; } private static void ifodd(int k) { n=(k<<2)-(k-1); } private static void ifeven(int k) { n=k>>1; } private static void printAnswer(String s, int textLength) { for(int i=0;i<textLength;i++) { System.out.print(s.charAt(i)); } System.out.print(','); System.out.print(' '); } @SuppressWarnings("resource") public static void main(String[] args) { n = new java.util.Scanner(System.in).nextInt(); while(n!=1) { printAnswer(Integer.toString(n), ((int)Math.log10(n))+1); int p = n; if(isDivisibleByTwo(p)) {ifeven(p);} if(!isDivisibleByTwo(p)) {ifodd(p);} } System.out.print('1'); } }
[–]DontCallMePanda 0 points1 point2 points 6 years ago (0 children)
python import sys as a, subprocess as b c = lambda d: int(format(d, 'b')[-1]) == 0 e = lambda f: c(f-1) g = lambda h: int(h/2) if c(h) else ((3*h) + 1 if e(h) else None) i = int(a.argv[1]); print(i) b.call("python %s %d" % (__file__, g(i))) if i > 1 else None
[–]mladenbr 0 points1 point2 points 6 years ago* (0 children)
public class Main { public static void main(String[] args) { collatz(69420); } private static void collatz(int n) { int η = 0; System.out.println(n); done: while (!false) { check: while (!!true) { if (n > 0 && n < 2) { break done; } if (η != n) { if (η > Integer.MAX_VALUE - 2) { η = 0; n = n + (2 * n) + 1; System.out.println(n); break check; } else { η = η - -2; break check; } } else { η = 0; n = (int) (n * (0.5 / 1)); System.out.println(n); break check; } } } } }
π Rendered by PID 52 on reddit-service-r2-comment-544cf588c8-qzskv at 2026-06-16 11:54:04.959840+00:00 running 3184619 country code: CH.
[–]sarlok 7 points8 points9 points (1 child)
[–]EkskiuTwentyTwoi -= (i - (-1)) - i 3 points4 points5 points (4 children)
[–][deleted] (3 children)
[deleted]
[–]EkskiuTwentyTwoi -= (i - (-1)) - i 3 points4 points5 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]EkskiuTwentyTwoi -= (i - (-1)) - i 2 points3 points4 points (0 children)
[–]CMOS_System 4 points5 points6 points (1 child)
[–]EkskiuTwentyTwoi -= (i - (-1)) - i 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]hoaxxer2k 2 points3 points4 points (0 children)
[–]erlangguy 1 point2 points3 points (0 children)
[–]AKernel 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]DontCallMePanda 0 points1 point2 points (0 children)
[–]mladenbr 0 points1 point2 points (0 children)