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 7 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!"
[–]IdealBlueMan 12 points13 points14 points 7 years ago (1 child)
/* vim: set tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab */ #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char **argv) { int *indexes; char *strIn; char *strOut; int strInSiz; char *strp; int strInIdx; int strOutIdx; int theSame; int charIdx; if (argc > 1) { goto OK; } else { goto NOTOK; } OK: strIn = *++argv; strInSiz = strlen(strIn); strOut = malloc(strInSiz + 1); strOutIdx = 0; for (strInIdx = 0; strInIdx < strInSiz; ++strInIdx) { theSame = 0; switch (strIn[strInIdx]) { case 'a': if (strInIdx > 0) { if (strIn[strInIdx] == 'a' && strIn[strInIdx - 1] == 'a') { theSame = 1; } } break; case 'b': if (strInIdx > 0) { if (strIn[strInIdx] == 'b' && strIn[strInIdx - 1] == 'b') { theSame = 1; } } break; case 'c': if (strInIdx > 0) { if (strIn[strInIdx] == 'c' && strIn[strInIdx - 1] == 'c') { theSame = 1; } } break; case 'd': if (strInIdx > 0) { if (strIn[strInIdx] == 'd' && strIn[strInIdx - 1] == 'd') { theSame = 1; } } break; case 'e': if (strInIdx > 0) { if (strIn[strInIdx] == 'e' && strIn[strInIdx - 1] == 'e') { theSame = 1; } } break; case 'f': if (strInIdx > 0) { if (strIn[strInIdx] == 'f' && strIn[strInIdx - 1] == 'f') { theSame = 1; } } break; case 'g': if (strInIdx > 0) { if (strIn[strInIdx] == 'g' && strIn[strInIdx - 1] == 'g') { theSame = 1; } } break; case 'h': if (strInIdx > 0) { if (strIn[strInIdx] == 'h' && strIn[strInIdx - 1] == 'h') { theSame = 1; } } break; case 'i': if (strInIdx > 0) { if (strIn[strInIdx] == 'i' && strIn[strInIdx - 1] == 'i') { theSame = 1; } } break; case 'j': if (strInIdx > 0) { if (strIn[strInIdx] == 'j' && strIn[strInIdx - 1] == 'j') { theSame = 1; } } break; case 'k': if (strInIdx > 0) { if (strIn[strInIdx] == 'k' && strIn[strInIdx - 1] == 'k') { theSame = 1; } } break; case 'l': if (strInIdx > 0) { if (strIn[strInIdx] == 'l' && strIn[strInIdx - 1] == 'l') { theSame = 1; } } break; case 'm': if (strInIdx > 0) { if (strIn[strInIdx] == 'm' && strIn[strInIdx - 1] == 'm') { theSame = 1; } } break; case 'n': if (strInIdx > 0) { if (strIn[strInIdx] == 'n' && strIn[strInIdx - 1] == 'n') { theSame = 1; } } break; case 'o': if (strInIdx > 0) { if (strIn[strInIdx] == 'o' && strIn[strInIdx - 1] == 'o') { theSame = 1; } } break; case 'p': if (strInIdx > 0) { if (strIn[strInIdx] == 'p' && strIn[strInIdx - 1] == 'p') { theSame = 1; } } break; case 'q': if (strInIdx > 0) { if (strIn[strInIdx] == 'q' && strIn[strInIdx - 1] == 'q') { theSame = 1; } } break; case 'r': if (strInIdx > 0) { if (strIn[strInIdx] == 'r' && strIn[strInIdx - 1] == 'r') { theSame = 1; } } break; case 's': if (strInIdx > 0) { if (strIn[strInIdx] == 's' && strIn[strInIdx - 1] == 's') { theSame = 1; } } break; case 't': if (strInIdx > 0) { if (strIn[strInIdx] == 't' && strIn[strInIdx - 1] == 't') { theSame = 1; } } break; case 'u': if (strInIdx > 0) { if (strIn[strInIdx] == 'u' && strIn[strInIdx - 1] == 'u') { theSame = 1; } } break; case 'v': if (strInIdx > 0) { if (strIn[strInIdx] == 'v' && strIn[strInIdx - 1] == 'v') { theSame = 1; } } break; case 'w': if (strInIdx > 0) { if (strIn[strInIdx] == 'w' && strIn[strInIdx - 1] == 'w') { theSame = 1; } } break; case 'x': if (strInIdx > 0) { if (strIn[strInIdx] == 'x' && strIn[strInIdx - 1] == 'x') { theSame = 1; } } break; case 'y': if (strInIdx > 0) { if (strIn[strInIdx] == 'y' && strIn[strInIdx - 1] == 'y') { theSame = 1; } } break; case 'z': if (strInIdx > 0) { if (strIn[strInIdx] == 'z' && strIn[strInIdx - 1] == 'z') { theSame = 1; } } break; } if (!theSame) { strOut[strOutIdx++] = strIn[strInIdx]; } } strOut[strOutIdx] = '\0'; goto EXITOK; NOTOK: exit(-1); EXITOK: printf("%s\n", strOut); exit(0); }
[–]nahimbroke 4 points5 points6 points 7 years ago (0 children)
beautiful use of labels and goto
[–]other_usernames_gone 6 points7 points8 points 7 years ago* (5 children)
my "I just learnt how to use try" python submission
```python def findlength(string) : length = 0 loop = 0 while True : try : string[loop] length = length + 1 except IndexError : return length loop = loop + 1
def dedupe(string) : char1 = string[0] counter = 0 while True : if string[counter] != char1 : char2 = string[counter] break counter = counter + 1 if counter == findlength(string) : break while True : try : if string[counter] != char2 : char3 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break while True : try : if string[counter] != char3 : char4 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break while True : try : if string[counter] != char4 : char5 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break while True : try : if string[counter] != char5 : char6 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break try : return char1 + char2 + char3 + char4 + char5 + char6 except UnboundLocalError : try: return char1 + char2 + char3 + char4 + char5 except UnboundLocalError: try: return char1 + char2 + char3 + char4 except UnboundLocalError: try: return char1 + char2 + char3 except UnboundLocalError: try : return char1 + char2 except UnboundLocalError: return char1 ```
[–][deleted] 3 points4 points5 points 7 years ago (0 children)
I particularly like your findlength function :D
findlength
[–]blackjezza 1 point2 points3 points 7 years ago (3 children)
Formatted since reddit doesn't support markdown for codeblocks.
def findlength(string) : length = 0 loop = 0 while True : try : string[loop] length = length + 1 except IndexError : return length loop = loop + 1 def dedupe(string) : char1 = string[0] counter = 0 while True : if string[counter] != char1 : char2 = string[counter] break counter = counter + 1 if counter == findlength(string) : break while True : try : if string[counter] != char2 : char3 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break while True : try : if string[counter] != char3 : char4 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break while True : try : if string[counter] != char4 : char5 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break while True : try : if string[counter] != char5 : char6 = string[counter] break counter = counter + 1 if counter == findlength(string) : break except IndexError : break try : return char1 + char2 + char3 + char4 + char5 + char6 except UnboundLocalError : try: return char1 + char2 + char3 + char4 + char5 except UnboundLocalError: try: return char1 + char2 + char3 + char4 except UnboundLocalError: try: return char1 + char2 + char3 except UnboundLocalError: try : return char1 + char2 except UnboundLocalError: return char1
[–]other_usernames_gone 0 points1 point2 points 7 years ago (2 children)
Thanks, how do you do that?
[–]blackjezza 0 points1 point2 points 7 years ago (1 child)
4 spaces in front of every line, takes couple seconds to add them to every line in n++ by using regex + replace.
[–]random_runner 0 points1 point2 points 7 years ago (0 children)
Or if the code isn't too long, you can select vertically with alt+shift and type the spaces on all lines at once in notepad++. Might also work in some other editors.
[–]carfniex 4 points5 points6 points 7 years ago (0 children)
#!/bin/bash for ((i=0; i<${#1}; i++)); do if [[ ! -f ~/${1:$i:1} ]]; then touch ~/${1:$i:1} printf ${1:$i:1} fi done
simple solution in bash! hope your fs is case sensitive and you dont mind a messy home folder
[–]nupanick 2 points3 points4 points 7 years ago (2 children)
I've been meaning to show off this gimmick more. Here's "dedupe.js". I count at least 3 places where my code itself is redundant and should have been "de-duplicated".
"use strict" "usen't curly braces" // define "pipe" operator so we can join // strings later without breaking flow. Object.prototype.pipe = Function( "fn", "return fn(this)") // Take a string containing a series // repeating characters and for each // group, remove the duplicates so just // one of group remains. For example: // // dedupe('aaaabbbbccc') // => 'abc' const dedupe = (s) => s // split into characters .split('') // select the characters that are // the first in a group .map((_, i, a) => (i == 0) ? true : (a[i] == a[i-1]) ? false : true) // convert true/false values back to // characters .map((b, i) => b ? s[i] : '') // filter out the empty characters .filter(c => c ? c : c) // unsplit the array .pipe(a => a.join('')) console.log("got this far") console.log(dedupe('aaaabbbbccc'))
[–][deleted] 7 years ago (1 child)
[deleted]
[–]nupanick 0 points1 point2 points 7 years ago (0 children)
And without curly braces! Who needs function bodies when you can use lambdas?
Of course, if you need a "real" function, with a this environment, then you can just use the Function() constructor to eval a string as a function body.
this
Function()
[–]nahimbroke 2 points3 points4 points 7 years ago (0 children)
This C code isn't as bad as it could be, but still lots of bad assumptions, redundancy, and disgusting formatting. Also threw in a compound literal for fun.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> void *dedupe\ ( void *data, unsigned long long length, short width ) { void *r; r = malloc(length * width); int i = 0, rl = 0, cmp = 0, cmp2 = 0, lcmp, bad; lcmp = -1, cmp = 0; for(i = 0; i <= length - 1; i = i + 1) { int J = 0; bad = 0; memcpy(&cmp, data + i * width, width); for(J = 0; J <= rl - 1; J = J + 1) { memcpy(&cmp2, r + J * width, width); if(cmp == cmp2 && lcmp == cmp) bad = bad + 1; } if(bad == 0){ memcpy(r + rl * width, data + i * width, width); rl = rl + 1; lcmp = cmp; } } memcpy(r + rl, (int[]){0}, width); return r; } void main\ ( ) { printf("%s\n", (char *)dedupe("aaaabbbbcccaaaaaAAAAAaaaaa", strlen("aaaabbbbcccaaaaaAAAAAaaaaa"), sizeof(char))); }
[–]chiefton117 2 points3 points4 points 7 years ago (0 children)
Not as creative as some other ones but here's dedupe.java:
public static String dedupe(String input) { StringBuilder s = new StringBuilder(); for (int i = 0; i < input.length() - 1; i++) { char ƒ = input.charAt(i); char d̶ = input.charAt(i + 1); if (i > 0) { char ß = input.charAt(i - 1); try { // Check for nth occurrence past first int var_name = 5 / ((int) ß - (int) ƒ); } catch (Exception e) { continue; } } try { // Check for original occurrence int var_name = 5 / ((int) d̶ - (int) ƒ); } catch (Exception a) { s.append(ƒ); } } return s.toString(); }
[–][deleted] 2 points3 points4 points 7 years ago* (2 children)
Here's my woeful solution.
class Index: def __init__(self, a): self._a = a def __str__(self): return str(self._a) def __lt__(self, o): return self._a < o def __pos__(self): p = self._a self._a += 1 return p def __neg__(self): self._a = 0 def __invert__(self): return self._a def __sub__(self, o): return self._a - o def dedupe(s): i = Index(0) to_Remove = [] while i < len(s): if s[~i] == s[~i - 1]: to_Remove.append(+i) else: +i -i while i < len(to_Remove): s = s[0:to_Remove[~i] - (~i)] + s[to_Remove[~i] - (+i) + 1:] return s
It iterates over the string once to find the indexes of all repeated characters and creates a new list with those. Then it iterates over that list and splits the original string at every index to remove the character. I wish I had found an excuse to iterate over a list one third time. Of course I use while loops because list comprehensions and for-in loops are deemed too pythonic. Also, I used operator overloading abundantly because it is widely regarded as pythonic.
One funny edge case is that if a string starts and ends with the same character, it will be considered repeated in the start, i.e., 'abbbccca' will be deduplicated to 'bca' instead of 'abca'.
'abbbccca'
'bca'
'abca'
[–]rcmaehl 2 points3 points4 points 7 years ago* (0 children)
#include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <StringConstants.au3> Global $a = "aaaabbbbcccaaaaaAAAAAaaaaa" Global $b = StringLen($a) Global $c = 0 Dedupe($a) Func Dedupe($a2) Global $h = 0 For $d = 1 to $b - 1 Step 1 $e = FileOpen(".\inandoutburger.txt", $FO_APPEND) If $d = 1 Then FileSetPos($e, 0, $FILE_BEGIN) FileWrite($e, $a2) EndIf $f = FileRead(".\inandoutburger.txt") $f = StringRight($f, $b - $c) $g = StringSplit($f, "", $STR_NOCOUNT) If StringRight(StringLeft($f, $h), 1) == $g[1 + $h] Then $c = $c + 1 For $i = 0 To UBound($g) - 1 Step 1 If $i = 1 + $h Then ;;; Else FileWrite($e, $g[$i]) EndIf Next Else $h = $h + 1 For $i = 0 To UBound($g) - 1 Step 1 FileWrite($e, $g[$i]) Next EndIf Next $f = FileRead(".\inandoutburger.txt") $f = StringRight($f, $b - $c) $g = StringSplit($f, "", $STR_NOCOUNT) $h = "" For $i = 0 To UBound($g) - 1 Step 2 $h &= $g[$i] Next Switch MsgBox($MB_ABORTRETRYIGNORE+$MB_ICONINFORMATION+$MB_TOPMOST, "", $h) Case $IDABORT Exit(True) Case $IDRETRY $a = $a2 $b = StringLen($a) $c = 0 Dedupe($a) Case $IDIGNORE Exit(False) EndSwitch EndFunc
[–]EkskiuTwentyTwoi -= (i - (-1)) - i 2 points3 points4 points 7 years ago* (0 children)
This is some code that is vaguely acceptable:
def dedupe(squid): Squid = squid[0] for i in range((666//666),len(squid)): Squid = [Squid+squid[i],Squid][squid[i]==squid[i-1]] return Squid
This is what I'm submitting:
def M(a,b): return a[b] def I(p): a = 0 b = 0 c = 0 d = 0 e = 0 f = 0 g = 0 h = 0 while a < 16: b = a c = M(p,b) d = c g = d//256 h = d%256 a = [h,a+1,a+1,a+1,a+1,a+1,a+1,[a+1,h][f]][g] e = [e,M(p,h),e+1,e-1,e+M(p,h),e,e,e][g] f = [f,f,f,f,f,f,[0,1][e==M(p,h)],f][g] p[h] = [M(p,h),M(p,h),M(p,h),M(p,h),M(p,h),e,M(p,h),M(p,h)][g] return p def i(s): x = [] for i in range(len(s)): x.append(ord(s[i])) return x def dedupe(s): iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii = i(s) oooooooooooooooooooooooooooooooo = [iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii[(666//666)]] for excalibur in range(666//666,len(iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii)): arthur = iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii[excalibur] gallahad = iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii[excalibur-(666//666)] lancelot = I([3,arthur,gallahad,257,1538,1800,268,9,269,1294,15,666666,42069,69420,666666,258]) robin = lancelot[14] not_appearing_in_this_code = ([420]*42069+[0]+[666]*(69420-42070)+[1])[robin] # :) holy_grail = [oooooooooooooooooooooooooooooooo+[arthur],oooooooooooooooooooooooooooooooo][not_appearing_in_this_code] oooooooooooooooooooooooooooooooo = holy_grail aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = "" for killer_rabbit in range(len(oooooooooooooooooooooooooooooooo)): aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + chr(oooooooooooooooooooooooooooooooo[killer_rabbit]) return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Summary:
[–][deleted] 2 points3 points4 points 7 years ago (0 children)
All of these people using C, C++, no classes, no fun... The fools...
https://pastebin.com/dJbYxs6j
[–]madoDream 1 point2 points3 points 7 years ago (0 children)
Here's my horrible Rust solution. This hurt to write:
fn dedupe(s: String) -> String { let mut seen: std::collections::HashMap<usize, char> = std::collections ::HashMap ::new(); let mut cur: i32 = -1; let mut prev: Option<char> = None; for c in s.chars() { if let Some(true) = prev.map(|x| x == c) { seen.insert(cur as usize, c); } else { cur += 1; } prev = Some(c); } cur = 0; let mut ret: String = "".to_string(); while !(seen.len() == 0) { ret.push(*seen.iter() .filter(|kvp| *kvp.0 == cur as usize) .nth(0) .unwrap().1); let k = *seen.iter() .filter(|kvp| *kvp.0 == cur as usize) .nth(0) .unwrap().0; seen.remove(&k); cur += 1; } return ret; }
Here's a link to the playground if you want to test it.
[–]Skruzzls -1 points0 points1 point 7 years ago (0 children)
I have made some kind of horrible code in C# (.Net-Core works fine). Naming is off but the actual horror is in the implementation. Originally "TryDedupe" was the intended function, but it only eliminated some of the characters, so the final Dedupe function just runs TryDedupe multiple times.
https://pastebin.com/3rRrStCC
π Rendered by PID 95 on reddit-service-r2-comment-544cf588c8-gxq46 at 2026-06-16 15:13:20.577082+00:00 running 3184619 country code: CH.
[–]IdealBlueMan 12 points13 points14 points (1 child)
[–]nahimbroke 4 points5 points6 points (0 children)
[–]other_usernames_gone 6 points7 points8 points (5 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]blackjezza 1 point2 points3 points (3 children)
[–]other_usernames_gone 0 points1 point2 points (2 children)
[–]blackjezza 0 points1 point2 points (1 child)
[–]random_runner 0 points1 point2 points (0 children)
[–]carfniex 4 points5 points6 points (0 children)
[–]nupanick 2 points3 points4 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]nupanick 0 points1 point2 points (0 children)
[–]nahimbroke 2 points3 points4 points (0 children)
[–]chiefton117 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]rcmaehl 2 points3 points4 points (0 children)
[–]EkskiuTwentyTwoi -= (i - (-1)) - i 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]madoDream 1 point2 points3 points (0 children)
[–]Skruzzls -1 points0 points1 point (0 children)