1. PHP / Говнокод #12238

    +141

    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
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    <?php
    
    function createOperator($a = [])
    {
    	$IOperator = [
    		'index' => 1,
    		'iteration' => "",
    		'f' => 'print_r($a);if(++$a["index"] < $a["count"])
    				return eval($a["iteration"]);
    			else
    				return $a["result"];',
    		'run' => 'return eval',
    	];
    	if(!empty($a))
    	{
    		$IOperator['run'] = '$a = $'.$a['title'] . '; ' . $IOperator['run'] . '($a["iteration"]);';
    		$IOperator['iteration'] = '$a = array_merge($a, ' . $a['operation'] . ');' . "\n" . $IOperator['f'];
    		$IOperator = array_merge($a, $IOperator);
    	}
    	return $IOperator;
    }
    
    $fib = createOperator([
    	'first' => 1,
    	'result' => 1,
    	'count' => 5,
    	'title' => 'fib',
    	'operation' => '["first" => $a["result"], "result" => $a["first"]+$a["result"]]'
    ]);
    echo eval($fib['run']);
    
    
    $fac = createOperator([
    	'result' => 1,
    	'count' => 5,
    	'title' => 'fac',
    	'operation' => '["result" => $a["result"]*$a["index"]]',
    ]);
    echo eval($fac['run']);

    Меня потянуло сделать какую-то фигню. Встречайте, рефлексивное программирование.

    mkusher, 02 Декабря 2012

    Комментарии (12)
  2. PHP / Говнокод #12237

    +141

    1. 1
    1/-0 == 42

    Внезапно.

    serpinski, 02 Декабря 2012

    Комментарии (1)
  3. PHP / Говнокод #12236

    +121

    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
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    This awesome yet simple and pragmatic PHP library performs an addition of two numbers.
    
    In early stages of Internet, developers were forced to work with poor, dry, functional, horrific languages. Everything had to be done through austere functions and operators. There was no objects. No interfaces. No dependency injection.
    
    For example, to make something as simple as an addition, our dads had to write: 1+1. Yeah, really.
    
    Hopefuly now, we have PHP 5.3 and its solid OOP implementation. SimplePHPEasyPlus lets you make this addition in a more fashionable way, using real OOP. It is fast, simple, flexible and tested. To add 1 to 1, all you have to do is:
    
    use SimplePHPEasyPlus\Number\NumberCollection;
    use SimplePHPEasyPlus\Number\SimpleNumber;
    use SimplePHPEasyPlus\Number\CollectionItemNumberProxy;
    use SimplePHPEasyPlus\Parser\SimpleNumberStringParser;
    use SimplePHPEasyPlus\Iterator\CallbackIterator;
    use SimplePHPEasyPlus\Operator\AdditionOperator;
    use SimplePHPEasyPlus\Operation\ArithmeticOperation;
    use SimplePHPEasyPlus\Operation\OperationStream;
    use SimplePHPEasyPlus\Engine;
    use SimplePHPEasyPlus\Calcul\Calcul;
    use SimplePHPEasyPlus\Calcul\CalculRunner;
    
    
    $numberCollection = new NumberCollection();
    
    $numberParser = new SimpleNumberStringParser();
    
    $firstParsedNumber = $numberParser->parse('1');
    $firstNumber = new SimpleNumber($firstParsedNumber);
    $firstNumberProxy = new CollectionItemNumberProxy($firstNumber);
    
    $numberCollection->add($firstNumberProxy);
    
    $secondParsedNumber = $numberParser->parse('1');
    $secondNumber = new SimpleNumber($secondParsedNumber);
    $secondNumberProxy = new CollectionItemNumberProxy($secondNumber);
    
    $numberCollection->add($secondNumberProxy);
    
    $addition = new AdditionOperator('SimplePHPEasyPlus\Number\SimpleNumber');
    
    $operation = new ArithmeticOperation($addition);
    
    $engine = new Engine($operation);
    
    $calcul = new Calcul($engine, $numberCollection);
    
    $runner = new CalculRunner();
    
    $runner->run($calcul);
    
    $result = $calcul->getResult();
    $numericResult = $result->getValue(); // 2
    This library is now available for production purposes. Enjoy!

    [КО]Складывает 2 числа[/КО]

    Vasiliy, 01 Декабря 2012

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

    +127

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    val arr = new Array[Int](3)
    val arr2 = arr
    
    arr(0) = 100
    arr(1) = 200
    arr(2) = 300
    
    //arr2 == Array(100, 200, 300)

    Не говнокод конечно, хотя как посмотреть.

    Это нормально, учитывая, что val предполагает неизменяемость значения, или в данном случае считается, что только присвоить новое значение нельзя, а изменять внутреннюю структуру массива можно как захочешь?

    Ведь наже в C++ нельзя изменить значения const std::vector.

    Fai, 01 Декабря 2012

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

    +51

    1. 1
    2. 2
    #101 Check PHP configuration in console
    $ php -r "phpinfo\(\);"

    Вот такой скрипт нашёл в дебрях локального битрикса под NDA.

    serpinski, 01 Декабря 2012

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

    +50

    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
    public function getFilename($filename) {
            $string = ereg_replace("ж", "zh", $filename);
            $string = ereg_replace("ё","yo",$string);
            $string = ereg_replace("и", "i",$string);
            ......
    
            $string = ereg_replace("Т","T",$string);
            $string = ereg_replace("Б","B",$string);
            
            $string = ereg_replace(" ","_",$string);
            $filename = preg_replace("/[^\w\.\-_]/","",$string);
            
            return strtolower($filename);
        }

    UnnamedUser, 01 Декабря 2012

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

    +6

    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
    template <typename T, size_t rows, size_t cols>
    class Matrix {
    public:
        Matrix() :
            m_matrix(reinterpret_cast<T*>(new char[sizeof(T) * rows * cols]))
        {
            memset(m_matrix, 0, sizeof(T) * rows * cols);
            new (m_matrix) T[rows * cols];
    
            if ( rows == cols ) {
                for ( size_t i = 0; i < rows; i++ )
                    m_matrix[i * cols + i] = 1; // FIXME: this is hack
            }
    
        }
    
        // ...
    private:
        T *m_matrix;
    };

    Из прошлого куска.

    Инициализируем память нулями. А вдруг тип скалярный? :)

    Elvenfighter, 01 Декабря 2012

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

    +10

    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
    28. 28
    29. 29
    30. 30
    template <typename T, size_t rows, size_t cols>
    class Matrix {
    public:
        Matrix() :
            m_matrix(new T[rows * cols])
        {
            // Make identity matrix, if possible
            if ( rows == cols ) {
                for ( size_t i = 0; i < rows; i++ )
                    m_matrix[i * cols + i] = 1; // FIXME: this is hack
            }
    
        }
    
        // ...
    
        Matrix<T, rows, cols>& operator =(Matrix<T, rows, cols> &&other) {
            if ( this != &other ) {
                delete [] m_matrix;
                m_matrix = other.m_matrix;
    
                other.m_matrix = new T[cols * rows];
                other = static_cast<const Matrix&>(Matrix());
            }
    
            return *this;
        }
    
        // ...
    };

    Издержки move construtor :)

    Прошу внимания к строчкам 19-23

    Elvenfighter, 30 Ноября 2012

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

    +14

    1. 1
    int main(){(([](){})());}

    preview.tinyurl.com/blrtfuo
    ideone.com/BXrXDR
    Или еще чуть веселее:
    ideone.com/C425yo

    Xom94ok, 30 Ноября 2012

    Комментарии (10)
  10. bash / Говнокод #12229

    −127

    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
    28. 28
    29. 29
    30. 30
    #!/bin/bash
    set -e
    
    KEHU_FILE=kehu_ruikemei
    
    echo "*************"
    echo "rm logo_linux_clut224"
    echo "*************"
    find drivers/video/logo/ -name logo_linux_clut224.o -exec rm -f {} \;
    find drivers/video/logo/ -name logo_linux_clut224.c -exec rm -f {} \;
    find drivers/video/logo/ -name logo_linux_clut224.ppm -exec rm -f {} \;
    
    echo "***********************"
    echo "copy logo_linux_clut224.ppm"
    echo "***********************"
    cp $KEHU_FILE/logo_linux_clut224.ppm drivers/video/logo/
    
    echo "***********************"
    echo "make kernel.img"
    echo "***********************"
    make kernel.img -j4
    
    echo "***********************"
    echo "return origin files"
    echo "***********************"
    find drivers/video/logo/ -name logo_linux_clut224.o -exec rm -f {} \;
    find drivers/video/logo/ -name logo_linux_clut224.c -exec rm -f {} \;
    git checkout -- drivers/video/logo/logo_linux_clut224.ppm
    
    echo "return origin files success!!!"

    Скрипт в корне исходников ядра linux для Rockchip RK3066.

    Necromant, 30 Ноября 2012

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