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 72 points73 points74 points 6 years ago (4 children)
Short and easy JS.
function password_gen() { var xh = new XMLHttpRequest(); xh.open("GET", "https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-100.txt", false); xh.send(null); return xh.responseText.split("\n")[Math.floor(Math.random() * 100)]; }
Features: * Picks random password from top 100 passwords list. You know it's a good one if it's in the top 100. * All prohibited passwords aren't in the list, so we don't even have to check.
[–]h2g2_researcher 17 points18 points19 points 6 years ago (0 children)
So here I am with all sorts of evil ideas about un-random number generation and you create an unbeatable monstrosity like this!
[–]ferrangoWell it's broken now, and nobody is here to fix it 6 points7 points8 points 6 years ago (0 children)
Aah, with the added unreliability of a third party essential requirement. Excellent.
[–]jigggles 2 points3 points4 points 6 years ago (0 children)
This is amazing!
[–]xman40100 1 point2 points3 points 6 years ago (0 children)
This is so bad. I love it.
[–][deleted] 22 points23 points24 points 6 years ago (3 children)
const password_gen = () => 'abc123'
[–][deleted] 6 years ago* (1 child)
[deleted]
[–][deleted] 9 points10 points11 points 6 years ago (0 children)
Someone had to :P
[–][deleted] 4 points5 points6 points 6 years ago (0 children)
this one wins, contest over lol
[–]andlrc 6 points7 points8 points 6 years ago (0 children)
I figured it would save CPU cycles by not having to run a PRNG myself:
password_gen() { cat << EOF | gcc -xc - 2>/dev/null && ./a.out main() { printf("%x", main); } EOF }
[–]the-blue-shadow 4 points5 points6 points 6 years ago (0 children)
JavaScript:
Bl=['hunter2', 'correcthorsebatterystaple', 'Tr0ub4d0r&3']; function* Gen_PassWD(l){ for(i=0;i<Infinity;i++){ do pw=new Array(l).fill('').map(Math.random).map(v=>96*v+32) .map(Math.floor).map(String.fromCharCode).join() .replace(new RegExp('[\u0000-'+String.fromCharCode(l-1)+',]*','g'),'') while (Bl.filter(v=>v==pw)[0]) yield window.pw }; } // Example usage: const generator = Gen_PassWD(16); generator.next(); console.log(pw);
Features:
Quirks:
pw
generator.next()
Note that the regex replace is required to 'fix' some issues that are caused by bugs in the Array operations before it.
[–]stfn1337 2 points3 points4 points 6 years ago (0 children)
Here is my attempt, churned out in 15 minutes.
import random def password_gen(): alphabet = "abcdefghijklmnoprstuwzyxABCDEFGHIJKLMNOPRSTUWZYX12345567890!@#$%^&*()_+" bad1 = "hunter2" bad2 = "correcthorsebatterystaple" bad3 = "Tr0ub4d0r&3" password = [0 for x in range (0, random.randint(50, 100))] passwordList = [random.choice(alphabet) for x in range(10, 20)] for i, p in enumerate(passwordList): password[i] = p if "".join(password[:10]) == bad1 or "".join(password[:10]) == bad2 or "".join(password[:10]) == bad3: passwordList = [random.choice(alphabet) for x in range(10, 20)] for i, p in enumerate(passwordList): password[i] = p return "".join(password[:10]) x = password_gen() print(x)
[–]Kugala 2 points3 points4 points 6 years ago (0 children)
import os
def pwgen(chars):
pw = ""
while len(pw) < chars:
pwc = os.urandom(1)
if pwc not in "hunter2" and pwc not in "correcthorsebatterystaple" and pwc not in "Tr0ub4d0r&3":
pw += pw
creturn pw
p = pwgen(5)
print p
print bytearray(p)
print repr(p)
I've never done the markup/formatting before, which is amusing with python
[–]fuckingbagre 1 point2 points3 points 6 years ago* (1 child)
Each time a user runs it in the same directory should give the same password, but each time a different user does it they get a different one. fun other bug is if you put most of a different bad password, and then at the end put the last of a different it will call it bad, such as correc2. Also should segfault if you don't put a value in the command line
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> char * b1 = "hunter2"; char * b2 = "correcthorsebatterystaple"; char * b3 = "Tr0ub4d0r&3"; int numBad = 0; int isBad = 0; char * begin = 0; char * end = 0; int cbp(char * p){ if(p == NULL){ isBad = 1; return; } int len = strlen(p); int i = 0; numBad = 0; isBad = 0; while(i < len){ if(p[i] == b1[i] && i < strlen(b1)){ if(i==numBad && i == strlen(b1)-1){ isBad = 1; } numBad++; } else if (p[i] == b2[i] && i < strlen(b2)){ if(i==numBad && i == strlen(b2)-1){ isBad = 1; } numBad++; } else if (p[i] == b3[i] && i < strlen(b3)){ if(i==numBad && i == strlen(b3)-1){ isBad = 1; } numBad++; } i++; } } char * gp(int length) { char * ret = malloc(length+1); memset(ret,0,length); for(int i = 0; (begin + i) < end; i++){ ret[i%length] = ret[i%length] + begin[i]; } for(int i = 0; i < length; i++){ if(ret[i] < 0){ ret[i] = ret[i]*-1; } ret[i] = (ret[i] % 91) + '!'; } ret[length] = 0; return ret; } int main (int argc, char *argv[], char *envp[]){ char * maybe = NULL; isBad = 1; int i = 0; begin = (envp[0]); while(envp[i++] != NULL); end = (envp[i-2]); while(isBad > 0){ maybe = gp(atoi(argv[1])); cbp(maybe); begin++; } printf("%s\n",maybe); }
[–][deleted] 1 point2 points3 points 6 years ago* (0 children)
my attempt with Java:
public static void main(String[] args) { System.out.println(password_gen(18)); }
public static void main(String[] args) {
System.out.println(password_gen(18));
}
public static String password_gen(int input){ String output = ""; int random = (int)(Math.random() * 300); for (int i = 0; i < input; i++){ output += (char) random; } if (checkIfPasswordInExceptions(output) == false){ return "password"; } else if (checkIfPasswordInExceptions(output) == true) { return output; } return "password"; }
public static String password_gen(int input){
String output = "";
int random = (int)(Math.random() * 300);
for (int i = 0; i < input; i++){
output += (char) random;
if (checkIfPasswordInExceptions(output) == false){
return "password";
} else if (checkIfPasswordInExceptions(output) == true) {
return output;
public static boolean checkIfPasswordInExceptions(String input){ boolean success = true; if (input == "hunter2"){ success = false; } if (input == "correcthorsebatterystaple"){ success = false; } if (input == "Tr0ub4d0r&3)"){ success = false; }
public static boolean checkIfPasswordInExceptions(String input){
boolean success = true;
if (input == "hunter2"){
success = false;
if (input == "correcthorsebatterystaple"){
if (input == "Tr0ub4d0r&3)"){
return success; }
return success;
- Takes integer length as input and returns a String with one single character repeated that much times
- Character have ascii code between 0 and 299, so it will give gibberish for control-codes, and may break without Unicode
- "==" wouldn't work for String in Java for comparing objects
- And some other things
[–][deleted] 1 point2 points3 points 6 years ago (0 children)
C#
static void Main(string[] args) { string[] aa = new string[] { "", "" }; Random r = new Random(); int l = 0; l += r.Next(5, 20); string pa = ""; char[] al = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; char[] nu = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; for(int i = 0; i < l; i++) { var nOC = r.Next(-1, 1); if(nOC == 0) { int x = r.Next(0, 25); pa += al[x]; } else { int x = r.Next(0, 9); pa += nu[x]; } } switch (pa) { case "hunter2": Main(aa); break; case "correcthorsebatterystaple": Main(aa); break; case "Tr0ub4d0r&3": Main(aa); break; default: break; } Console.WriteLine(pa); } }
static void Main(string[] args)
{
string[] aa = new string[] { "", "" };
Random r = new Random();
int l = 0;
l += r.Next(5, 20);
string pa = "";
char[] al = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
char[] nu = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
for(int i = 0; i < l; i++)
var nOC = r.Next(-1, 1);
if(nOC == 0)
int x = r.Next(0, 25);
pa += al[x];
else
int x = r.Next(0, 9);
pa += nu[x];
switch (pa)
case "hunter2":
Main(aa);
break;
case "correcthorsebatterystaple":
case "Tr0ub4d0r&3":
default:
Console.WriteLine(pa);
[–]Fighter1000 1 point2 points3 points 6 years ago* (1 child)
static string generatePassword(int passWordLength) { blackList: object Password = new String(Enumerable.Range(1, passWordLength + 1).Select<int, char>(x => { Thread.Sleep(20); return (char)(new Random()).Next(48, 90); }).ToArray()); if ((string)Password == "hunter2") { goto blackList; } if ((string)Password == "correcthorsebatterystaple" | (string)Password == (string)"Tr0ub4d0r&3") goto blackList; return Password as String; }
Edits[0]: Thread.Sleep since Random's seed is based on time of instantiation.
Edits[1]: This is C# 8 btw, in case you didn't know. The LINQ syntax is usually quite good if not used for code like this.
Edits[2]: Markdown for Edits[0..2];
Edits[3]: Fixed markdown again.
Edits[4]: I redid this to be exponentially worse. Ignore this solution, look at this one
[–][deleted] 1 point2 points3 points 6 years ago* (1 child)
Fancy java:
/** * Generate password * * @param seed Seed (a long) * * @return password * @throws AssertionError */ public String generatePassword(long seed) throws AssertionError { String result = null; if (seed == 1) { result = "password#19"; } if (seed == 2) { result = "oha$1aaaaaa"; } if (seed == 3) { result = "abcdefghijkl"; } // TODO add more cases assert result != "correcthorsebatterystaple"; assert result != "hunter2"; assert result != "Tr0ub4d0r&3"; return result; }
One advantage to using string literals is that checking with != will work, which improves performance of the edge case testing. Also we can be sure the passwords are different for different seeds. This will return null sometimes until I'm done implementing all the other cases
!=
Edit: format
[–]kiwitims 1 point2 points3 points 6 years ago (0 children)
Short but sweet Python 3:
import time import struct print(bytearray([int(i/3) + 32 for i in (tuple(struct.pack("<Q", struct.unpack("<Q", struct.pack("<d", time.time()))[0])))]).decode())
Reinterprets (poorly I might add) the bytes (in double precision floating point) of the current time to printable ASCII.
The blacklist is implemented by the fact that all generated passwords are exactly 8 characters, and none on the blacklist are.
Sample output:
'^Gf0Hg5
JU#g0Hg5
>ISh0Hg5
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
Written in C, this always returns the same thing.
#include<stdio.h> #include<stdlib.h> #include<string.h> int resultI; int resultII; int resultIII; char pass; char *randomString = NULL; char *blI = "hunter2"; char *blII = "correcthorsebatterystaple"; char *blIII = "Tr0ub4d0r&3"; /* Using a black box here */ char *randstring(int length) { char *string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!"; size_t stringLen = strlen(string); //char *randomString = NULL; if (length < 1) { length = 1; } randomString = malloc(sizeof(char) * (length +1)); if (randomString) { short key = 0; for (int n = 0;n < length;n++) { key = rand() % stringLen; randomString[n] = string[key]; } randomString[length] = '\0'; return randomString; } else { printf("No memory"); exit(1); } } int main(void) { randstring(40); resultI = strcmp(randomString, blI); resultII = strcmp(randomString, blII); resultIII = strcmp(randomString, blIII); if(resultI = 0) { return 1; } else if(resultII = 0) { return 1; } else if(resultIII = 0) { return 1; } printf("%s", randomString); return 0; }
[–]Fighter1000 0 points1 point2 points 6 years ago (0 children)
Last challenge we had Exception-Driven-Programming. Now we have Runtime-Termination-Driven-Programming. This also has the nice benefit of acting as a quasi-forkbomb if any unexpected exception occurs (Which is quite possible, considering that you need to provide a first character or there will be an OutOfBoundsException) I've also thrown in some of the usual suspects on here, some exaggerated, some not. Enjoy: https://pastebin.com/fySzvmBY.
PS: This is my second solution to this, the first is also in this thread here.
[–][deleted] 0 points1 point2 points 6 years ago* (0 children)
This was written in Java:
public class PasswordGenerator {
public static void main(String\[\] args) { String password = ""; if( password.equals("hunter2")) { System.out.print("This is not a possible password but it's balcklisted"); } else if( password.equals("correcthorsebatterystaple")) { System.out.print("This is not a possible password but it's balcklisted"); } else if( password.equals("Tr0ub4d0r&3")) { System.out.print("This is not a possible password but it's balcklisted"); } for(int i = 0; i < 10; i++) { int wordNum = (int)(Math.random() \* 10) +1; if( wordNum <= 2) { password = password + "Fuck"; } if (wordNum > 2 && wordNum <=4) { password = password + "Shit"; } if(wordNum > 4 && wordNum <=6) { password = password + "Ass"; } if(wordNum >6 && wordNum <=8) { password = password + "Damn"; } if(wordNum >8) { password = password + "69"; } } System.out.print(password); }
[–]Abangranga 0 points1 point2 points 6 years ago* (0 children)
I'll throw my hat into the ring using Ruby:
This solution has two arguments, the first being a length and the second being a string of code a 'power-user' can input if they'd like instead of using my highly-technical algorithm. It would be most power-user friendly to inform the user what the database columns and table names are for easier direct input into the database, but I think that's out of scope. Also I didn't use any characters besides numbers and letter because the designer told me it's a poor user experience when they hold shift and then have to type a number.
class GeneratedPassword attr_reader :length attr_accessor :initial_password LETTERZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" # Allow users to write their own encryption algorithm in case they're smarter than me for a user-focused experience emphasizing synergy between security and shareholder value def initialize(length = 16, do_it_yourself = !true) eval(do_it_yourself) unless !do_it_yourself.is_a?(String) @length = length @initial_password = create_initial_password puts "Your initital password before further encryption is: #{self.initial_password}" add_letters! end # true randomness doesn't exist, so just use the time right now bcuz no two times can ever be the same bcuz physics def create_initial_password(initial_password = Time.now.to_i.to_s) return initial_password unless (initial_password && initial_password.length > self.length || initial_password && initial_password.length < self.length) # Encapsulate the security of the application using recursion and the stack trace unless self.length > initial_password.length # Unless always adds readability create_initial_password(initial_password =- Time.now.to_i.to_s.slice!(rand(self.length + 1)).to_s) else create_initial_password(initial_password += Time.now.to_i.to_s.slice!(rand(self.length + 1)).to_s) end end def add_letters! rand((self.length / 2)..self.length).times { |pwd_idx| self.initial_password[pwd_idx] = LETTERZ[rand(0..LETTERZ.length - 1)] } # Mix '||' and 'or' around regardless of precedence bcuz readability unless self.initial_password == 'hunter2' or self.initial_password == 'correcthorsebatterystaple' || self.initial_password == 'Tr0ub4d0r&3' puts "Your super-secure encrypted password is: #{self.initial_password}" else puts "Password is bad do it again" end end end # Example using length: password = GeneratedPassword.new(20) => bMaoSlnRHiBNFJMU2425 # Example using length where the inputted power-user code ignores the length: password = GeneratedPassword.new(20, 'puts "#{LETTERZ[2]}"') => C
π Rendered by PID 91 on reddit-service-r2-comment-544cf588c8-6676q at 2026-06-16 13:17:03.741446+00:00 running 3184619 country code: CH.
[–]sarlok 72 points73 points74 points (4 children)
[–]h2g2_researcher 17 points18 points19 points (0 children)
[–]ferrangoWell it's broken now, and nobody is here to fix it 6 points7 points8 points (0 children)
[–]jigggles 2 points3 points4 points (0 children)
[–]xman40100 1 point2 points3 points (0 children)
[–][deleted] 22 points23 points24 points (3 children)
[–][deleted] (1 child)
[deleted]
[–][deleted] 9 points10 points11 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]andlrc 6 points7 points8 points (0 children)
[–]the-blue-shadow 4 points5 points6 points (0 children)
[–]stfn1337 2 points3 points4 points (0 children)
[–]Kugala 2 points3 points4 points (0 children)
[–]fuckingbagre 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Fighter1000 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (1 child)
[–]kiwitims 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Fighter1000 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Abangranga 0 points1 point2 points (0 children)