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:
Source: reddit.com/r/badcode/comments/lgrgxe/i_present_sleepsort
-
(1 point.) In what language is SleepSort implemented?
-
(2 points.) In no more than two sentences, why is SleepSort correct? Put another way, how does it work?
-
(2 points.) In no more than two sentences, in what way is SleepSort poorly designed?
Consider optimisedRandomNumber
, below:
Source: reddit.com/r/badcode/comments/hkxndd/i_mean_its_o1_so_who_cares
- (2 points.) Why is the running time of
optimisedRandomNumber
in \(O(1)\)?
Consider getrandomnumberbetween1and10
, below:
Source: reddit.com/r/badcode/comments/m2b4e2/my_friends_take_on_random_integers
Though the above image from the original poster itself does not make it clear, line 33 (not fully pictured) does appear to contain a }
; regardless, you should assume there is one there when answering the next question.
-
(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? -
(3 points.) In a file called
getrandomnumberbetween1and10.js
, re-implementgetrandomnumberbetween1and10
(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):
Source: reddit.com/r/programminghorror/comments/dx65ys/found_on_facebook
-
(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 ins
? In no more than 2 sentences, why or why not? -
(2 points.) In no more than two sentences, in what way is
stringSize
poorly designed?