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

    +162

    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
    class StupidClass
    {
    	public function __toString()
    	{
    		return 'this';
    	}
    	public function hax()
    	{
    		return $this === $$$$$$this;
    	}
    }
    
    var_dump((new StupidClass)->hax()); // true

    Уииии

    Fike, 06 Января 2015

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

    +129

    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
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    <macrodef name="foreach">
      <attribute name="target"/>
      <attribute name="file-property"/>
      <element name="files"/>
      <element name="args"/>
      <sequential>
        <local name="foreach.files"/>
        <local name="foreach.target"/>
        <local name="foreach.file-property"/>
        <local name="foreach.args"/>
        <property name="foreach.target" value="@{target}"/>
        <property name="foreach.file-property" value="@{file-property}"/>
        <pathconvert property="foreach.files">
          <files/>
        </pathconvert>
        <propertyset id="foreach.args">
          <args/>
        </propertyset>
        <property name="foreach.args" refid="foreach.args"/>
        <property name="foreach.target" value="@{target}"/>
        <!-- there is no better way to do this at the moment
             property names and values should not contain comma-space and equals signs
        -->
        <script language="javascript"><![CDATA[
           var files = project.getProperty("foreach.files").split(":"),
           args = project.getProperty("foreach.args").split(", "),
           task = project.createTask("antcall"), arg;
    
           task.target = project.getProperty("foreach.target");
           for (var a in args) {
             arg = task.createParam();
             arg.setName(a.split("=")[0]);
             arg.setValue(String(a.split("=")[1]));
           }
    
           for (var f in files) {
             arg = task.createParam();
             arg.setName(project.getProperty("foreach.file-property"));
             arg.setValue(String(files[f]));
             task.perform();
           }
         ]]></script>
      </sequential>
    </macrodef>
    <!-- пример использования: -->
    
    <target name="transcode-font-helper">
      <property name="font.face.local" value="${font.face}"/>
      <foreach target="transcode-font" file-property="font.raw.source">
        <files>
          <fileset dir="${basedir}/fonts">
            <include name="*/${font.face.local}/*.otf"/>
            <include name="*/${font.face.local}/*.ttf"/>
          </fileset>
        </files>
        <args>
          <propertyref name="font.face.local"/>
        </args>
      </foreach>
    </target>

    А ведь если подумать: собрали все самое лучше, что есть в современном программировании - Ява, ХМЛ и ж.скрипт. Потом выбросили условные операторы, итерацию и операции со строкам - потому что не нужны. И получилась замечательная система для сборки проектов.

    wvxvw, 06 Января 2015

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

    +155

    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
    <?php
    
    function load ($file) {
    	static $files = null;
    	$files or $files = [];
    	
    	if (!isset($files[$file])) {
    		require($file);
    		$files[$file] = true;
    	}
    }
    
    $times = 1000000;
    $time1 = microtime(true);
    
    for ($i = 0; $i < $times; $i ++) {
    	require_once('inc.php');
    }
    
    $end1 = microtime(true) - $time1;
    
    $time2 = microtime(true);
    
    for ($i = 0; $i < $times; $i ++) {
    	load('inc.php');
    }
    
    $end2 = microtime(true) - $time2;
    
    echo "require_once = $end1 vs \nload = $end2";

    Вот это да... require_once работает в ~8 раз медленнее чем функция load. Вывод:
    require_once = 16.962311029434 vs
    load = 2.6861710548401

    Содежания файла inc.php:

    <?php echo 'Hello, world!';

    volter9, 06 Января 2015

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

    +151

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    $friends = mysql_query("(SELECT * FROM  `friends` WHERE `from` LIKE  '{$act}' AND  `to` LIKE  '{$act}' AND  `isaccepted` =  '1') UNION (SELECT  `id` ,  `name` ,  `surname` FROM  `users_info`);");
    		$friend = array();$loop=0;
    		while($fetch = mysql_fetch_assoc($friends)) {
    			if ($fetch['from'] == $act) $search=$fetch['to']; else $search=$fetch['from'];
    			$friend[$loop]=mysql_fetch_assoc(mysql_query("SELECT `id`,`name`,`surname` FROM `users_info` where `id`='{$search}'"));
    			$loop++;
    		}

    Почему не работает?

    yanislavb, 05 Января 2015

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

    +156

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    $query = "INSERT INTO news (title, img_url, subtitle, content, date, time, id) 
    	VALUES (\"" .
    	$_POST["title"] . "\", \"" .
    	$_POST["img_url"] . "\", \"" .
    	$_POST["subtitle"] . "\", \"" .
    	$_POST["content"] . "\", \"" .
    	date("Y-m-d") . "\", \"" .
    	time() . "\", " .
    	DEFAULT . ");";

    только начал php. уверень, есть решение поэлегантнее этого

    artembegood, 05 Января 2015

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

    +56

    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
    #include <iostream>
    #include <thread>
    #include <list>
    #include <functional>
    #include <chrono>
    using namespace std;
    
    void outputToSomeContainer(int val, list<int>& result){
       result.push_back(val);
    }
    
    class async{
      list<thread> a;
    public:
      async(){}
      async(async&& a): a(move(a.a))
      {}
      void addTask(function<void()>&&f){
        a.emplace_back(move(f));
      }
      void wait(){
        for(auto&& i: a)
          i.join();
      }
    };
    
    async async_O_n_Sort(const list<char>& unsorted, function<void(int)> outputToContainer){
      async a;
      for(int i: unsorted)//O(n)
        a.addTask([i, outputToContainer](){this_thread::sleep_for(chrono::milliseconds(5+i*10));outputToContainer(i);});
      return a;
    }
    
    int main() {
      list<char> unsorted {1, 0, 6, 3, 4};
      list<int> sorted;
      auto a = async_O_n_Sort(unsorted, bind(outputToSomeContainer, placeholders::_1, ref(sorted)));
      cout<<"А мы веселые пельменья, мы похоже на варенья"<<endl;
      a.wait();
      for(int i: sorted)
          cout<<i<<endl;
      return 0;
    }

    Тред:

    http://www.gamedev.ru/flame/forum/?id=196521
    http://coliru.stacked-crooked.com/a/c317bee4dbe183ab

    laMer007, 05 Января 2015

    Комментарии (91)
  7. JavaScript / Говнокод #17409

    +156

    1. 1
    document.write('Well, I broke your script');

    Я знаю что document.write это плохо, но использование document.write ломает jsfiddle :D
    http://jsfiddle.net/volter9/x29Lzvu6/

    volter9, 05 Января 2015

    Комментарии (1)
  8. Си / Говнокод #17408

    +143

    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
    void ** __attribute__((noinline)) findVoidSortMap(void ** list,void *key)
    {
        if (!list) return 0;
        if (!*list) return 0;
        unsigned int count= **(unsigned int**)list;
        char *p=(char*)*list;
        p+=4;
        Element *b=(Element *)p;
     
        long long skey=(long long)key;
     
        while (count>0) {
            void** kt=(void**)&b[count>>1];
            long long rkey=(long long)kt[0];
            if (skey==rkey) return (void**)&kt[1];
            if (skey>rkey) {b+=(count>>1)+1;count--;}
            count=count>>1;
        }
     
        return (void**)-1;
    }

    гуру осемблира на этом коде доказывал, что Эльбрус сосёт
    http://www.gamedev.ru/flame/forum/?id=196722&page=33#m481

    TarasB, 05 Января 2015

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

    +136

    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
    GetDate(string dateTimeText){
                    DateTime date = new DateTime();
                    String[] parsedDate = dateTimeText.Split('/');
                    bool allNumbers = true;
    
                    foreach (string s in parsedDate)
                    {
                        int value;
                        if (!int.TryParse(s, out value) && allNumbers)
                        {
                            allNumbers = false;
                        }
                    }
                    if (parsedDate.Length == 3 && allNumbers)
                    {
                        String newDateText = parsedDate[1] + "/" + parsedDate[0] + "/" + parsedDate[2];
    
                        DateTime.TryParse(newDateText, out date);
                    }
     return date;
    }

    парсинг юзеринпута в датетайм пикере

    zxxc, 05 Января 2015

    Комментарии (3)
  10. Куча / Говнокод #17406

    +126

    1. 1
    2. 2
    3. 3
    <fileset dir="${basedir}" includes="**/*">
          <type type="dir"/>
    </fileset>

    Печаль заключается в том, что <type type="dir"/> никогда ничего не даст выбрать. fileset не может технически содержать папки.

    wvxvw, 05 Января 2015

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