1. Лучший говнокод

    В номинации:
    За время:
  2. Java / Говнокод #11767

    +75

    1. 1
    2. 2
    3. 3
    4. 4
    Process process = Runtime.getRuntime().exec("su");
    OutputStream outputStream = process.getOutputStream();
    String cmd = "keycode " + KeyEvent.KEYCODE_BACK;
    outputStream.write((cmd + "\n").getBytes("ASCII"));

    Андроид. Вот такой вот прекрасный способ программно нажать кнопочку "Back".

    http://stackoverflow.com/questions/5832861/android-back-key-by-software

    bormand, 13 Сентября 2012

    Комментарии (8)
  3. Ruby / Говнокод #11763

    −166

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    class User < ActiveRecord::Base
        
      def full_name
      	user_info=User.find(self.id)
      	[user_info.name,user_info.surname].join(" ")
      end
    end

    Получаем полное имя себя

    1101_debian, 13 Сентября 2012

    Комментарии (8)
  4. PHP / Говнокод #11758

    +49

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    wordpress:
    function __( $text, $domain = 'default' ) {
    	        return translate( $text, $domain );
    }
    
    function translate( $text, $domain = 'default' ) {
    	        $translations = &get_translations_for_domain( $domain );
    	        return apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
    }
    
    PHP-manual:
    PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality. 
    
    wordpress:
    /*
    * Don't use translate() directly, use __()
    */

    xcont, 12 Сентября 2012

    Комментарии (8)
  5. PHP / Говнокод #11749

    +52

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    <?php
    function zvlad_gen($min, $max, $anz) {
    $array = range($min, $max);
    for($x=0; $x<$anz; $x++) {
    $i = rand(1, count($array))-1;
    $erg[] = $array[$i];
    //array_splice($array, $i, 1);
    }
    return $erg;
    }
    // Получить 6 случайных чисел в диапазоне от 121 до 155
    $rand_array = zvlad_gen(121, 155, 6);
    echo join(", ", $rand_array);
    ?>

    Небольшая функция, которая поможет вывести диапазон чисел. Так же можно вывести несколько вариантов сразу, т.е. что бы не запускать скрипт несколько раз.
    Данная функция подойдет сайтам, которые разыгрывают призы, например) Ну, или лотерея какая нибудь.

    ahref, 11 Сентября 2012

    Комментарии (8)
  6. Куча / Говнокод #11731

    +117

    1. 1
    main = print x where x = x + 0

    http://ideone.com/9caQE

    result: Runtime error     time: 0.01s    memory: 3536 kB     signal: -1 
    input: no
    output: no
    stderr:
    prog: <<loop>>

    LispGovno, 09 Сентября 2012

    Комментарии (8)
  7. PHP / Говнокод #11723

    +62

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    $user = User::model()->findByAttributes(array("email" => $this->username, "is_deleted"=>0));
    
    if (!$user)
    {
        $user = User::model()->findByAttributes(array("email" => $this->username, "is_deleted"=>0));
    }

    Видимо так, на всякий случай, ещё раз попробовать решил.

    dizballanze, 07 Сентября 2012

    Комментарии (8)
  8. C++ / Говнокод #11657

    +31

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    #include <iostream>
    #include <tr1/functional>
    
    using namespace std::tr1::placeholders;
    
    struct I
    {
      int i;
    };
    
    struct S
    {
      int i;
      I ii;
    };
    
    int main()
    {
      std::tr1::function<I& (S&)> pi = std::tr1::bind(&S::ii, _1);
      std::tr1::function<std::tr1::reference_wrapper<I> (S&)> pri = std::tr1::bind(static_cast<std::tr1::reference_wrapper<I> (*)(I&)>(&std::tr1::ref<I>), std::tr1::bind(pi, _1));
      std::tr1::function<int& (S&)> psi = std::tr1::bind(&I::i, std::tr1::bind(&std::tr1::reference_wrapper<I>::get, std::tr1::bind(pri, _1)));
      S s = {1, {2}};
      std::cout << psi(s) << std::endl;
      psi(s) = 3;
      std::cout << psi(s) << std::endl;
      return 0;
    }

    Нашёл на RSDN, в теме о том, как получить указатель на член члена.

    suc-daniil, 28 Августа 2012

    Комментарии (8)
  9. Java / Говнокод #11626

    +75

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    @Override
    public boolean onTouchEvent(MotionEvent e) {
    	x=(int)e.getX(); y=(int)e.getY();
    	/* ... */
    	synchronized(this) {
    		try {this.wait(1000);}
    		catch (InterruptedException ex) {}
    	}
    	return true;
    }
    
    /* Gets (screen/pixel) x,y coordinates of last touch event*/
    public boolean GetCoordinates(MutablePoint coordinates) {
    	if (x==-1) return false;
    	coordinates.init(x,y);
    	return true;
    }

    https://github.com/acl33/AndroidDasher/blob/master/src/dasher/android/DasherCanvas.java

    rat4, 20 Августа 2012

    Комментарии (8)
  10. PHP / Говнокод #11612

    +60

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    list($width, $height, $type, $attr) = getimagesize($file1);
         
    if($width == $height)
    {    
        set_time_limit(270);
        if(filesize($file1) > 102400)  return array(false, "Размер больше 100Кб!");
        if($type == 1)      {$file2 = $file1.'.gif'; $out = $tizer_id.'.gif'; $type = 'gif'; $ext = 'gif';}
        elseif($type == 2)  {$file2 = $file1.'.jpg'; $out = $tizer_id.'.jpg'; $type = 'jpg'; $ext = 'jpg';}
        elseif($type == 3)  {$file2 = $file1.'.png'; $out = $tizer_id.'.png'; $type = 'png'; $ext = 'png';}
        else return array(false, "Это не картинка!");
        /* ... */
    }
    // no square
    else
    {
        return array(array('img',false, "Изображение не квадратное!"));
    }

    Зачем жить, если no square

    roman-kashitsyn, 19 Августа 2012

    Комментарии (8)
  11. PHP / Говнокод #11610

    +52

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    <?
    ....
    //башкортостан
    $city_select_arr = "<OPTGROUP LABEL=\"Республика Башкортостан\">";
    foreach($city_select_data as $k=>$v) {
    {
    $global_city = ($_GET['city'] == '' ? $_POST['city_select']: $_GET['city']);
    $city_select_arr .= "<option value=\"".$v."".( $v == $global_city ? " selected" : "" ) . "\">".$v."</option>";
    }
    }
    $city_select_arr .= "</OPTGROUP>";
    //пермский край
    $city_select_arr .= "<OPTGROUP LABEL=\"Пермский край\">";
    foreach($city_select2_data as $k=>$v) {
    {
    $global_city = ($_GET['city'] == '' ? $_POST['city_select']: $_GET['city']);
    $city_select_arr .= "<option value=\"".$v."".( $v == $global_city ? "" : "" ) . "\">".$v."</option>";
    }
    }
    $city_select_arr .= "</OPTGROUP>";
    
    .... и так еще циклов 10-15

    обратите внимание на переменную $global_city - ей в каждой итерации каждого цикла присваивают условное значение, которое в пределах этого файла точно не меняется :)
    ну и сам набор циклов можно было бы конечно систематизировать.

    scorcher, 19 Августа 2012

    Комментарии (8)