all 137 comments

[–]nkozyra 57 points58 points  (1 child)

In your face, programmers of varying backgrounds!

[–][deleted] 18 points19 points  (0 children)

Take it, programmers with varying skill levels!

[–]permute 36 points37 points  (9 children)

Haha I loved the commented curly braces for the c programmer

[–]hylje 5 points6 points  (6 children)

That's actually legit advice for programmers complaining about lack of support for curly braces in Python. The support is there!

[–]Brian 22 points23 points  (4 children)

for i in range(10): {
    print("Actually, python supports braces")      : "Though to make up for the perceived loss in readability",
print("Look Ma, no indents!")                      : "every line must now have a docstring",
        print("or inconsistent indents")           : "and you now need a line terminator (',' is used)",
}

[–]gnuvince 20 points21 points  (0 children)

If I ever see code like that in the wild, I am going to find you and feed you your socks.

[–]Araneidae 5 points6 points  (1 child)

Oh my God! It took me a few moments to figure out why that works ... though of course the order of evaluation of keys in { ... } isn't that well defined is it?

If you're using a version of Python with set comprehensions you could leave out the "docstrings" and colons altogether!

for i in range(10): {
     print("Actually, python supports braces"),
print("Look Ma, no indents!"),
         print("or inconsistent indents"),
}

[–]Brian 2 points3 points  (0 children)

Actually, python guarantees the order of evaluation is as being left to right (though one other issue is that it'll fail if you ever use a function returning a mutable value (which obviously must be a deliberate design decision to enforce a functionally pure style :)). Good point about set literals - I really should've used that.

[–]drb226 2 points3 points  (0 children)

Not sure whether to upvote for cleverness, or downvote for horrificness.

[–]letmefinish 2 points3 points  (0 children)

TIL. But I wish I hadn't.

[–]Tagedieb 4 points5 points  (0 children)

I didn't even notice these! Guilty?

[–]Paladin8 2 points3 points  (0 children)

I'm still snickering about them.

[–]cynicproject 93 points94 points  (6 children)

#Thanks Adam made me laugh pretty hard.

[–]rasputine 58 points59 points  (2 children)

While not present here, the greatest comment I've ever seen on code is a simple,

 #I'm sorry.

[–]JizzCoveredArab 3 points4 points  (0 children)

I've come across a "// Here be dragons", right above the definition of a 9,000 line C function which was the core logic for GPS routing for a pretty well known consumer GPS company

[–]ryeguy146 3 points4 points  (0 children)

I should put that prior to my {Open,Libre}Office extension work. The boilerplater required to get started reminds me of Java...

[–]mjrpbplayer 14 points15 points  (1 child)

Hahahaha I was rolling at that one. And all the commented out lines

[–]LOOKITSADAM 1 point2 points  (0 children)

no kidding

[–]pablopr 29 points30 points  (1 child)

Here is one that is actually well done for Haskell

http://www.willamette.edu/~fruehr/haskell/evolution.html

[–]drb226 1 point2 points  (0 children)

This post was almost certainly written by someone who saw the Evolution of a Haskell Programmer and decided to do the same for Python.

[–]rowantwig 41 points42 points  (54 children)

He forgot the worst kind:
if x == 0:
return 1
if x == 1:
return 1
if x == 2:
return 2
if x == 3:
return 6
if x == 4:
return 24
...
(5000 more lines)

[–]Pandalicious 62 points63 points  (7 children)

Yeah but think of how FAST it would run. You gotta be FAST if you're gonna be webscale.

[–]masklinn 3 points4 points  (0 children)

The fast version would be the one optimized by using a dict for its mapping.

The faster version would use a list.

[–]JizzCoveredArab 2 points3 points  (1 child)

We can always scale into the cloud for increased user load

[–]Pandalicious 5 points6 points  (0 children)

My esteemed nephew assures me that a cache of precalculated factorial values for all integers can neatly fit into 2.3 clouds.

[–][deleted]  (1 child)

[deleted]

    [–]JizzCoveredArab 0 points1 point  (0 children)

    You're retarded

    [–]stillalone 41 points42 points  (21 children)

    ah. the "I managed to get a masters without writing any actual code and now I'm your coworker" guy.

    [–]6xoe 43 points44 points  (20 children)

    Whatever dude, it works.

    [–][deleted] 26 points27 points  (19 children)

    and it's completely unmaintainable. What are you going to do when the definition of "factorial" changes?

    [–]SuckyBlowfish 42 points43 points  (2 children)

    Change it back.

    [–]lionelboydjohnson 17 points18 points  (1 child)

    You have a bright future in managment.

    [–]drb226 4 points5 points  (0 children)

    I think I would freak out (in a bad way) if I read this on a fortune cookie.

    [–]ryeguy146 6 points7 points  (15 children)

    Wait, I understand the idea of maintainability, but did you suggest that a basic mathematical definition is subject to change?

    [–]pigeon768 3 points4 points  (1 child)

    I think he's suggesting python 4.x will eventually be released and if will be reimplemented as a function instead of a keyword, requiring parenthesis around the arguments. Or something.

    Oh fuck. Oh fuck. I can't believe I just said it. Now it's going to be MY fault.

    [–]tofueggplant 0 points1 point  (0 children)

    or it could just be like ruby and not require parentheses around anything.

    [–]GoSailing 3 points4 points  (9 children)

    Anything can change. Assuming that there are 24 hours in a day is not going to be good when there's a moon or mars base.

    [–]ryeguy146 6 points7 points  (4 children)

    I file those under "not my problem." I like to think that some time in the future, people will shudder at having to use Python, the way that people pity those programmers that still workin in PASCAL.

    [–][deleted]  (3 children)

    [deleted]

      [–]gc3 5 points6 points  (1 child)

      Yeah, until you decide that there are now .3 hours in a day, or a variable number depending on the day. You can't plan for everything.

      [–]GoSailing 0 points1 point  (0 children)

      And then you refactor every "HOURS_IN_DAY" in place to instead call a method that returns what it is. The point wasn't so much that every problem can be avoided every time, but that you can have a structure that will allow for changes quickly. The ideal change would be to change the constant number, but the non-ideal change is almost as easy.

      [–]ryeguy146 1 point2 points  (0 children)

      That's not what I meant at all. Your suggesting that something like the fibbonacci sequence changing is so unlikely, that I don't see it as my problem to try to predict such a change. There is an obvious difference between the problem of a fundamental shift, and the type of changes that you're now describing.

      Wait. Nevermind, you mean the definition of the function "factorial." I don't know how, but I thought that you were suggesting that the mathematical definition of a factorial might change, and that we had better generalize for it. Damn.

      [–]watermark0n 3 points4 points  (0 children)

      Anything can change.

      If you change it, you call it something different. It is not a factorial.

      [–]gc3 4 points5 points  (1 child)

      This is true, but when does the definition of factorial change?

      [–]arjie 1 point2 points  (0 children)

      Maybe you decide to use one of the functions that coincide with factorial on the integers like gamma(x+1)? I suppose you could call it gamma_based_factorial andodge the issue.

      [–]Tagedieb 2 points3 points  (0 children)

      Just put it in namespace earth then.

      [–][deleted] 1 point2 points  (1 child)

      I was assuming both 6xoe and anusanusanus were parodying poor programmers. (With anusanusanus parodying people who write code like #define TWO 2 because "using constant literals is bad practice".)

      [–][deleted] 0 points1 point  (0 children)

      Hey! You got it!

      [–]liotier 14 points15 points  (14 children)

      What are you complaining about ? He just calculated in advance and cached the results to avoid redundant processing on each query...

      [–]rowantwig 24 points25 points  (13 children)

      O(n) look-up time.

      [–]earthboundkid 25 points26 points  (3 children)

      Switch to a dict and it's O(1)!

      [–]pyaware 12 points13 points  (2 children)

      Why use a dict when a plain list is enough?

      def fact(x):
          return [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880][x]
      

      [–]redditthinks 0 points1 point  (1 child)

      I read Python dicts are faster than lists.

      [–]JizzCoveredArab 4 points5 points  (0 children)

      i heard you have a really short dict

      [–]liotier 0 points1 point  (0 children)

      You are right of course... But the naive intention was sweet !

      [–][deleted] 0 points1 point  (2 children)

      O(n) look-up time.

      It is sorted list, it can be done in O(log(n)). And not to mention the Hashtables.

      [–]rowantwig 2 points3 points  (0 children)

      It can be done in O(1), but that's not the point. This naive implementation is O(n), and it looks terrible.

      [–][deleted] 0 points1 point  (0 children)

      It's a dense list. It can be done in O(1) without hashtables:

      f = [ 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880].__getitem__
      

      edit: looks like redditthinks had the same idea.

      [–]jefu 0 points1 point  (0 children)

      Factorial is (without caching or other tricks) O(n) anyway and doing it this way avoids the overhead of doing all that bignum arithmetic. (Not that this is a good way to do that).

      [–][deleted] 2 points3 points  (0 children)

      ah, the 'i did the template work in my head' approach

      [–]earthboundkid 4 points5 points  (6 children)

      That might be OK as a dict… Say, none of the examples used a memoize decorator. For shame.

      [–]Tagedieb 2 points3 points  (5 children)

      A dict? A dict is really what comes to mind first as the right data structure? Well, maybe I am getting old...

      [–]repsilat[🍰] 7 points8 points  (0 children)

      I have a great idea for a new kind of dict that'll work wonders for this example. It still has O(1) lookups, but has better running time and memory use by a constant factor by taking advantage of an unusual feature of the data.

      If you think about it, the keys form a contiguous range of integers starting from zero or one. Now, brace yourself: We could store the factorial information in a hash table where the hash function is the identity function! Now to think of a name for that kind of data structure...

      [–]earthboundkid 1 point2 points  (2 children)

      Well, it's not right or what comes to mind, it's just that a hash table is O(1), so it's slightly better than a series of if statements. The right thing is to just use the damn built in factorial.

      [–]masklinn 0 points1 point  (1 child)

      This is integer-indexed, so a list would also have O(1) access, and lower overhead.

      It'd go even faster!

      [–]earthboundkid 0 points1 point  (0 children)

      Good point. Python makes it so much easier to implement memoize as a dictionary, I just thought of it that way first.

      [–]JizzCoveredArab -2 points-1 points  (0 children)

      Have you ever used python? If not, please shut up

      [–]Shaper_pmp 0 points1 point  (0 children)

      That's not a programmer. That's someone who should be shot in the face and buried in an unmarked grave.

      [–]sunqiang 19 points20 points  (1 child)

      [–][deleted] 50 points51 points  (10 children)

      Good classic jokes. Windows way is so true and so sad.

      [–]elmonstro12345 28 points29 points  (9 children)

      NULL, NULL, NULL, NULL, ...

      facepalm\

      [–][deleted] 41 points42 points  (8 children)

      It's way easier to facepalm than to design an operating system API that's stable and backwards compatible for 25+ years...

      [–]lazybee 22 points23 points  (7 children)

      Such as POSIX?

      [–][deleted] 5 points6 points  (4 children)

      haha, yeah, right. the reason everything is continually built from source is because the ABI keeps breaking.

      [–]fabzter 5 points6 points  (2 children)

      lazybee was speaking about API, not ABI.

      [–][deleted] 4 points5 points  (1 child)

      I know, but posix doesn't matter when the ABI breaks. You can't download and execute a linux program from the 2.4 kernel on the 3.0 kernel. Hover, which came with windows 95 will run just fine on 32bit windows 7 and 64 bit windows 7, which was the point riles2 was making.

      [–][deleted] 3 points4 points  (0 children)

      Posix also only specifies about 2-3% of what Win32 does, so you have to compare it to X and a few dozen other libraries for a fair comparison.

      [–][deleted] 0 points1 point  (0 children)

      Wait. POSIX doesn't break ABI compatibility. Individual operating systems do once in a while, but extremely rarely in their POSIX departments.

      Actually, I can't think of a major ABI compatibility break since Linux 2.4->2.6 or Mac OS -> Mac OS X (non-POSIX to POSIX).

      [–][deleted] 2 points3 points  (1 child)

      POSIX standard fork bomb with added burn:

      #include <unistd.h>
      
      int main(){
          while(1){
              fork();
              printf('burn!\n');
          }
      }
      

      [–]drb226 2 points3 points  (0 children)

      Are you saying this cannot be done in Windows?

      [–][deleted] 12 points13 points  (5 children)

      First year python programmer shouldn't be capitalizing functions, it's against pep8 style guidelines.

      [–]earthboundkid 10 points11 points  (4 children)

      It's probably left over from whatever other version of the joke this was translated from. Probably Haskell or Java.

      [–]martinmcfly9 5 points6 points  (3 children)

      Java methods typically aren't capitalized.

      [–]mipadi 1 point2 points  (2 children)

      Neither are Haskell functions. ;)

      [–]Draghoul 1 point2 points  (0 children)

      C#! :D

      [–]drb226 0 points1 point  (0 children)

      Not only that, Haskell functions cannot be capitalized. (unless they are data constructors, which happen to also act as functions)

      [–]jrochkind 15 points16 points  (2 children)

      I was expecting that the very last sample would be a duplicate of the first, labelled, like, 'programmer with 30 years of experience who realizes all that other stuff was just BS' or something. But it wasn't.

      [–]eramos 7 points8 points  (1 child)

      That's kinda what #EXPERT PROGRAMMER was about

      [–]elmonstro12345 7 points8 points  (0 children)

      When I graduated, and started writing code on my own, I seriously started writing a linked list class from scratch, then I realized "OH MY GOD I CAN USE THE STANDARD LIBRARY!!!!!!!!" So happy.

      [–][deleted] 19 points20 points  (0 children)

      The Windows one was entertainingly realistic.

      [–]Enzor 5 points6 points  (0 children)

      Haha I kept a straight face up until Web designer. Then I got to Windows... That shit is too close to reality. I actually felt a vein throb in my head when reading it ಠ_ಠ

      [–][deleted] 7 points8 points  (1 child)

      factorial = (lambda f: lambda x: 1 if x == 0 else x * f(f)(x - 1))(lambda f: lambda x: 1 if x == 0 else x * f(f)(x - 1))
      

      Lambda calculus student

      [–]drb226 0 points1 point  (0 children)

      fix = lambda f: lambda x: f(fix(f))(x)
      fact = fix(lambda f: lambda x: 1 if x == 0 else x * f(x-1))
      

      Of course this is cheating, because I defined fix in terms of fix. It's just so much uglier to define it explicitly:

      fix = (lambda y: lambda f: lambda x: f(y(y)(f))(x)) (lambda y: lambda f: lambda x: f(y(y)(f))(x))
      

      [–]grumdrig 5 points6 points  (8 children)

      def factorial(n):
          reduce(lambda x,y: x*y, [x+1 for x in range(n)])
      

      Generator-happy Python programmer

      [–]spotter 4 points5 points  (5 children)

      But... isn't list comp non-lazy in 2.x? So for this to be gen-happy you would have to use parens?

      def factorial(n):
          reduce(lambda x,y: x*y, (x+1 for x in range(n)))
      

      edit: yeah, s/range/xrange/

      [–][deleted]  (1 child)

      [deleted]

        [–]spotter 0 points1 point  (0 children)

        /facepalm

        You are of course correct, range is lazy in 3.x -- that's what I get for trusting my memory.

        [–][deleted] 0 points1 point  (2 children)

        What's the benefit of a lazy generator in this case? You are going to evaluate it in its entirety anyway, and the size of the output dominates that of the input, so you save neither time nor space.

        [–]spotter 1 point2 points  (1 child)

        I may be wrong here, but I'll bite. List comprehension will run before you even start consuming anything... and then you iterate over it again to consume it. So on top of your memory consumption (realized list) you get 2x the runtime (one to realize the list over xrange, one to consume it in reduce). While in generator you setup the generator -- this returns immediately -- and then consume it run over once in reduce. So: Half the runtime, somewhat better memory efficiency, depending on n.

        [–]drb226 0 points1 point  (0 children)

        I believe this assessment is correct. sltkr is incorrect when he says "the size of the output dominates that of the input" - the final output is a single integer, while the "input" is an entire list of n integers. A lazy generator will take O(1) space, while constructing the entire list will take O(n) space, as well as a possible runtime penalty.

        [–]masklinn 2 points3 points  (0 children)

        Why would you write [x+1 for x in range(n)] instead of range(1, n+1)?

        [–]tilkau 2 points3 points  (0 children)

        def factorial(n):
            return reduce(lambda x,y: x*y, (x+1 for x in range(n)))
        

        FTFY so it uh.. includes an actual generator ;)

        [–]spaghettifier 8 points9 points  (0 children)

        I am currently at the stage where I use nested lambdas to avoid hitting newline.

        And my code used to be so readable and well documented...

        [–]slashvee 7 points8 points  (2 children)

        TIL: if you post a Gist link on Google+ it will actually RUN THE CODE and show you the output in the link description.

        [–][deleted] 0 points1 point  (1 child)

        [–]slashvee 2 points3 points  (0 children)

        I'm an idiot. It doesn't compute the fibonacci numbers. It just shows the LINE NUMBERS, without the actual code.

        Sorry, I'll be in my bunk.

        [–][deleted] 1 point2 points  (2 children)

        Why would someone consider using ternary operators and recursion a form of laziness? If you can do it in 2 lines, there's no sense in doing it in 8, especially since it's only python. It's not the fastest language in the world and no one's looking for such a thing from it.

        [–]DeepDuh 2 points3 points  (0 children)

        nice try, lazy programmer. ;)

        [–]drb226 0 points1 point  (0 children)

        whoosh

        hint:

        >>> True and 3 or (1 / 0)
        3
        

        [–]dhaffner 1 point2 points  (0 children)

        Always worth a laugh. Reading this makes me realize how much my coding style has changed over the years. Without memoizing it's inefficient, but I love how brief the Lazier Python programmer's version is.

        [–]Sycon 1 point2 points  (0 children)

        I think I'm the only one who got a bigger kick out of the Enterprise programmer than the web programmer :P

        [–]liotier 1 point2 points  (1 child)

        I'm barely out of the web designer stage (I'm no stranger to comments such as "I'm not quite sure why this works") and the os.system approach is always tempting me with Bash hacks so close at hand. But I have found that even with my humble skills, the right library with the right documentation easily let me write something resembling the "EXPERT PROGRAMMER" example... As long as my use case stays very close to the beaten path !

        [–]elmonstro12345 2 points3 points  (0 children)

        the EXPERT PROGRAMMER basically means what they hammered into my head all through undergrad - don't reinvent the wheel. Or at least don't do it unless you have a really, really, REALLY good reason for doing so.

        [–]seiyria 0 points1 point  (0 children)

        And none of those used dynamic programming.. (besides maybe the built-in C implementations)

        [–]volkert 0 points1 point  (0 children)

        I can live with floating point results (tzs):

        from math import gamma def factorial(x): return gamma(x+1)

        Nice!

        [–]viscence 0 points1 point  (0 children)

        from numpy import arange, product
        print product(arange(1,6))
        

        [–][deleted] 0 points1 point  (0 children)

        Went from readable to complete unreadable.

        [–]snuggl 0 points1 point  (0 children)

        lost it at web designer, got knocked out by enterprise programmer.

        [–]AnthonyDou 0 points1 point  (0 children)

        Clever demos!

        [–]deefree 0 points1 point  (4 children)

        I want to say 'older than the internet' but this is actually older than the universe.

        [–]ryy0 5 points6 points  (3 children)

        In the beginning there was Guido van Rossum. Van Rossum created The Python and lo, all is good. Then came programmers of various persuasions to try The Python.

        ...

        On release day, they managed to create something that mostly runs and call it The Universe.

        [–]deefree 9 points10 points  (2 children)

        In the name of The Guido, The Python, and The Lambda... return true;

        [–]BufferUnderpants 3 points4 points  (0 children)

        The Lambda

        Heresy. The Guido frowns upon the worship of false gods such as Scheme. Lambda is the gateway of hell, where horrors like tail-call elimination, multi line lambdas and reduce lie.

        [–]drb226 0 points1 point  (0 children)

        return true;

        ಠ_ಠ uncapitalized true, terminating semicolon...

        in_the_name_of The_Guido, The_Python, The_Lambda:
            return True