Govnokod: the enemy must be known in person



    All roughly represent what govnokod . On this remarkable site a whole collection assembled.

    What makes the code shitty - no one knows for sure. There is no exact definition. Often, what some consider the obvious govnokodom, others seem concise and effective solution.

    Examples


    Examples of govnokod range from redundant to outright hardcode . The most beloved examples are those whose shittyness is obvious to everyone:
    Boolean b = new Boolean( is_admin );
    if( b.toString().length() == 4 ) {
       // something...
    }
    // something
    


    But you know what I will say? It’s easy to condemn govnokod, but writing it is not so easy !
    Do not believe? Let's try!

    UPD The analysis of the received decisions is added below.


    The task


    Here is a simple task: Print the following on the screen:
    1
    2-1
    1-2-3
    4-3-2-1
    1-2-3-4-5
    6-5-4-3-2-1
    (Naturally, instead of 6 can be any number).

    Try to write two solutions in any language:
    1. The most concise / beautiful / readable
    2. The most govnokodisty (but not too much. Let's say the restriction is one screen. And of course, the solution should work!)

    You can write your own options in the comment (for long pieces recommended pastebin ). In addition to the entertaining component, this can turn out to be a useful exercise, because perhaps it will bring you closer to understanding how bad code is obtained and how to deal with it.

    Unleash your imagination!

    UPDATE : Decision Analysis


    Thanks to everyone who sent their options. So many comments I have not seen. Therefore, the topic is relevant.

    Let's try to analyze the decisions received.

    Good decisions


    "Good" solutions, as expected, are all of the same type, that is, approximately the following:
    $n = 6;
    for($i = 1; $i <= $n; $i++) {
        $array = $i % 2 ? range(1, $i) : range($i, 1);
        echo join('-', $array), PHP_EOL;
    }
    


    Two solutions stand out among them: Ruby and Haskell, which, in my opinion, are good because they are written in one line, but at the same time retain readability. This is achieved, obviously, due to the language. Arion

    Ruby :
    (6 + 1).times{|i| p (1..i).to_a.send((i%2 == 0)? :to_a: :reverse).join('-')}
    


    Haskell from pechlambda :
    main = mapM_ (putStrLn . foo) [1..6]
    foo n = concat $ intersperse "-" $ (if odd n then id else reverse) $ take n $ map show [1..]
    

    Which once again proves that Ruby rules !

    Seriously speaking, all these solutions have one drawback: they consume a lot of resources with large N (create many temporary arrays and strings). The following solution from rPman partly solves this problem (it is interesting that the author himself introduced it as govnokod):


    Bad decisions



    Of course, I was initially interested in the “bad” decisions. I must say that the Khabrovsk citizens were able to surprise me and came up with more govnovariants than I expected.

    Solid Trash

    The runcore solution tries to combine 1..N and N..1 cycle in one line, which reliably obfuscates the original purpose of this code:
    $is_reverse = false;
    for($i=1; $i<=6; ++$i) {
    	for($is_reverse?$j=$i:$j=1; $is_reverse?$j>=1:$j<=$i; $is_reverse?--$j:++$j) {
    		echo (($is_reverse)?($j.($j>1?'-':'')):($j.($j<$i?'-':'')));
    	}
    	$is_reverse = (($is_reverse) ? false : true);
    	if (in_array($is_reverse, array(true,false))) {
    		echo '
    '; } }


    Comments rule

    AHDPEu actively uses comments in its decision . It turned out great. Which once again proves that commenting on the code is from the evil one .

    Premature optimization

    Govnokod on Perl at youlose turns out due to attempts to optimize something there. Which once again proves that premature optimization is evil.

    Error processing

    JavaScript solution from azproduction , though somewhat exaggerated, shows the consequences of excessive error care and non-standard situations.

    Spoken languages

    • Delivered a sledopit solution in bash:
      #!/bin/bash
      i=1;m=7;while [ $i -lt $m ];do seq 1 $(($i%$m))|tr '\n' ' '|sed 's/ /-/g;s/-$//';((i++));echo;done|while read line; do [ $((${line/*-/}%2)) -eq 1 ]&&echo $line||echo $line|rev;done
      

      I think everyone will agree that it is never readable. It seems that this is the case when the language itself obliges the programmer to write govnokod. The same is confirmed by a whole series of decisions on brainfuck.
    • The pechlambda solution in Haskell: codepad.org/NQUSqcjc proves that no matter how good the language is, it can always be properly coded.
    • Thank you so much Next_Alex for reminding us of the good old goto.
    • But Levsha100 , completely in vain slandering his decision in assembler . In my opinion, I have not seen anything more readable here. :)
    • Decisions on Lisp , in my opinion, are both unreadable. However, later S2nek corrected .


    Govnocode Schools



    Classic

    But the most classic govnokod seems to have come out from SnakeSolid : pastebin.com/rdCtf2j5
    It seems to be nothing particularly superfluous: comments, and a couple of extra checks - yes, it's all a little redundant, but without crime. If you start to find fault, the author will probably be able to protect all this. But the code turned out on the whole page instead of a couple of lines. That is how it happens in life.

    To summarize


    I hope this simple exercise made you once again think about what is good and what is bad, or at least stretch old bones.

    Thanks to all!

    Write cleanly!


    Also popular now: