Code Reviews

Among Reddit’s most popular communities are r/badcode, a “place for the very worst code you’ve ever laid your eyes on,” and r/programminghorror, a place to share “strange or straight-up awful code.” Let’s review some of the code ourselves.


Consider SleepSort, below:

SleepSort

Source: reddit.com/r/badcode/comments/lgrgxe/i_present_sleepsort

  1. (1 point.) In what language is SleepSort implemented?

  2. (2 points.) In no more than two sentences, why is SleepSort correct? Put another way, how does it work?

  3. (2 points.) In no more than two sentences, in what way is SleepSort poorly designed?


Consider optimisedRandomNumber, below:

optimisedRandomNumber

Source: reddit.com/r/badcode/comments/hkxndd/i_mean_its_o1_so_who_cares

  1. (2 points.) Why is the running time of optimisedRandomNumber in \(O(1)\)?

Consider getrandomnumberbetween1and10, below:

getrandomnumberbetween1and10

Source: reddit.com/r/badcode/comments/m2b4e2/my_friends_take_on_random_integers

  1. (2 points.) Is this implementation of getrandomnumberbetween1and10 correct? Put another way, does it return a pseudorandom number between 1 and 10, inclusive? In no more than 2 sentences, why or why not?

  2. (3 points.) In a file called getrandomnumberbetween1and10.js, re-implement getrandomnumberbetween1and10 (in JavaScript) in such a way that your implementation is both correct and well-designed.


Consider stringSize, below, which is implemented in a language called Java (not JavaScript):

stringSize

Source: reddit.com/r/programminghorror/comments/dx65ys/found_on_facebook

  1. (3 points.) Assuming this implementation of stringSize is syntactically correct, is it also logically correct? Put another way, does it return the number of characters in s? In no more than 2 sentences, why or why not?

  2. (2 points.) In no more than two sentences, in what way is stringSize poorly designed?