A horrifying piece of C code

Discussion in 'Off Topic' started by Zhan, Mar 2, 2011.

  1. Zhan

    Zhan L5: Dapper Member

    Messages:
    208
    Positive Ratings:
    114
    This is something a close friend of mine wrote the other day:

    Code:
    [FONT="Courier New"]char p,q,r,w[151];
    int main(int c,char**v){
      int _=151;
      r=2^c?0:atoi(v[1]);
      while(--_>0)
        w[_]=_==75;
      while(_++<40&&puts(""))
        for(p=c=0;c<150|(q=w[c]);putchar((p=q)?35:32))
          w[c]=r&(p?240:15)&(w[c]?204:51)&(w[++c]?170:85);
    }[/FONT]
    Believe it or not, it actually prints out 1D elementary cellular automata, and takes the cellular automata rule as the first command line argument. If any of you have a C compiler, you can run it with 90 as the first argument and it will print out a nice Sierpinski triangle.

    Those of you with programming experience: I apologize for any loss of sanity.
     
  2. fubarFX

    aa fubarFX The "raw" in "nodraw"

    Messages:
    1,597
    Positive Ratings:
    1,677
    fractal geometry gives me a boner. But I can't code for my own life ;_;
     
    • Thanks Thanks x 1
  3. DaBeatzProject

    aa DaBeatzProject

    Messages:
    1,276
    Positive Ratings:
    1,091
    phah! I can!

    Code:
    dflwjshd kgfljfhg kjwfhg klwjhgl kjfgh<jhg $G HJKFGH WLKJGHL lfjkh 
    gw jfghlkfjgh kljhaz a jhg aekjhga emgjh oifhj klfhjflmk jh
     
    • Thanks Thanks x 2
  4. Zeewier

    Zeewier L9: Fashionable Member

    Messages:
    619
    Positive Ratings:
    130
    That "code" could indeed be turned into a fractal :p

    Ontopic:

    You should tell him to comment any code that he writes.
     
  5. Loc_n_lol

    Loc_n_lol L10: Glamorous Member

    Messages:
    726
    Positive Ratings:
    273
    I am horrified that this even compiles, let alone do something as intended...
     
    • Thanks Thanks x 1
  6. Zhan

    Zhan L5: Dapper Member

    Messages:
    208
    Positive Ratings:
    114
    Oh, he does when he writing normal code, he's a very competent programmer and writes good code. This was really just an experiment to see how obfuscated and abusive of C constructs he could make the program.
     
  7. TMP

    aa TMP Abuser of Site Rules

    Messages:
    948
    Positive Ratings:
    551
    Huh?

    This code isn't THAT hard to understand.

    Now if he had made it in the shape of a horse.....
     
  8. Zeewier

    Zeewier L9: Fashionable Member

    Messages:
    619
    Positive Ratings:
    130
    It's about habits, also when you look back to your code weeks later it's easier to see how you have done things.

    Also: The new trend is that more and more people within a company have to understand your code, even if they are not the programmers.
     
  9. Zhan

    Zhan L5: Dapper Member

    Messages:
    208
    Positive Ratings:
    114
    Of course, of course. Both of us have interned for a tech company writing code, and we're both familiar with the marks of good programming practice.

    This piece of code is a sort of joke, and is not to be taken seriously.
     
  10. Randdalf

    aa Randdalf

    Messages:
    1,054
    Positive Ratings:
    462
    It looks harder than it is because he's obfuscated all the if statements using that annoying shorthand I don't completely understand. It's nice and compact, if entirely unreadable, and not good practice in general. :(

    I've been advised you should never comment inside a method, it just makes things more confusing and messy. Comment at the top of every method and the top of every class - no more is necessary unless your code is absolutely terribly complex (in which case you should make it less so). I have to say, this works rather well.
     
    Last edited: Mar 2, 2011
  11. Numerous

    Numerous L4: Comfortable Member

    Messages:
    150
    Positive Ratings:
    72
    And people wonder why we don't have laser-guns already.
     
  12. cyked

    cyked L3: Member

    Messages:
    132
    Positive Ratings:
    87
    Rand's right. It's mostly just obfuscated code. the pre/post increments arent too hard to follow. I still dont know why he chose "_" as a variable other than to be a dick and then that only leaves the shorthanded if statements which like rand said are just confusing for most people (i dont really mind them) although his code would've made more sense if it started like

    Code:
    int _=151;
    char p,q,r,w[_];
    int main(int c,char**v){
    //REST OF PROGRAM BLARGHHH
    }
    edit: I dont remember if you can use a variable to initialize an array in c++! either way, it makes more sense to define the int outside of main with the rest of the variables anyways
     
  13. Numerous

    Numerous L4: Comfortable Member

    Messages:
    150
    Positive Ratings:
    72
    It just occurred to me:
    Why didn't he try anything like "if(a<b<c)" or MuaHaHaHa[-1]?
     
  14. Terr

    aa Terr Cranky Coder

    Messages:
    1,591
    Positive Ratings:
    405
    I disagree: The comments that explain how code implementation works need to be as near as possible to the lines that actually do whatever-it-is. Comments on methods should describe what they do to the level-of-detail required by the the caller, while comments inside the method describe everything else.