1. C# / Говнокод #17687

    +133

    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
    public string GenerateWinCode(String PrefixWinCode, String Name, String LastName, String NameCompany, bool IsCompany = false)
            {
                string _NormalWinCode = "";
                if (!IsCompany)
                    _NormalWinCode = (PrefixWinCode + Name[0] + LastName[0]).ToUpper();
                else
                {
                    var i = 1;
                    try
                    {
                        while (String.IsNullOrWhiteSpace(NameCompany[i].ToString()))
                        {
                            i++;
                        }
                        _NormalWinCode = (PrefixWinCode + NameCompany[0] + NameCompany[i]).ToUpper();
                    }
                    catch (Exception)
                    {
                        _NormalWinCode = (PrefixWinCode + NameCompany[0] + NameCompany[0]).ToUpper();
                    }
                }
                if (PrefixWinCode == "IN")
                    throw new RuleException("ErrorWincode", Resources.Accounts.Account.WincodeInvalid);
                try
                {
                    using (var context = db)
                    {
                        var _WincodesSim =
                            (from q in context.UserPartners.Where(m => m.WinCode.ToUpper().StartsWith(_NormalWinCode))
                             where q.WinCode.Length > 4
                             select q.WinCode).ToList();
                        var _Sufix = _WincodesSim.Select(m => Convert.ToInt32(m.Substring(4))).Max();
                        return _NormalWinCode + (_Sufix + 1);
                    }
                }
                catch (InvalidOperationException)
                {
                    return _NormalWinCode + 1;
                }
            }

    Генерация уникальных ключей

    AndrewTakker, 24 Февраля 2015

    Комментарии (2)
  2. Java / Говнокод #17686

    +77

    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
    public static void main(String[] args) {
            testIndiaLazy();
        }
    
        private static void testIndiaLazy() {
            LazyInstantiator lazyInstantiator = new LazyInstantiator();
            lazyInstantiator.getInstance();
            lazyInstantiator.getInstance();
        }
    
        public static class LazyInstantiator {
            private Object instance;
    
            public Object getInstance() {
                System.out.println("getInstance");
                if (instance != null || create());
                return instance;
            }
    
            private boolean create() {
                System.out.println("create");
                instance = new Object();
                return true;
            }
        }

    Out:
    getInstance
    create
    getInstance

    dmli, 24 Февраля 2015

    Комментарии (22)
  3. Си / Говнокод #17685

    +137

    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
    #include <time.h>
    
    
    int rrand(int start, int end)
    {
    	int range=end-start+1;
    	int speed=1;
    	int base=0;
    	int rez=start;
    	if(range>200) speed=range/100;
    	while(range>=0)
    	{
    		srand(clock());
    		if(rand()%2) base=base+speed+1;
    		else base--;
    		rez=rez+base;
    		rez=(rez < start)? end-rez : rez;
    		rez=(rez > end)? (rez%end)+start : rez;
    		range=range-speed;
    	}
    	return rez;
    }

    случайные числа в определенном диапазоне...

    pl7ofit, 24 Февраля 2015

    Комментарии (12)
  4. C# / Говнокод #17684

    +137

    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 bool StartListener()
    		{
    			bool flag;
    			try
    			{
    				flag = (this.m_Listener.BeginAccept() ? true : false);
    			}
    			catch (Exception exception)
    			{
    				CAssert.ReportAssert(exception);
    				flag = false;
    			}
    			return flag;
    		}

    Из реального корпоративного проекта.

    Danmer, 23 Февраля 2015

    Комментарии (2)
  5. JavaScript / Говнокод #17683

    +156

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    if(!item.contact.middleInitial)
                  {
                    fullAddress = item.contact.firstName+' '+item.contact.lastName+' '
                    +item.contact.companyName+' '+item.streetLine1 +' '+item.city +', '+item.state +' '+item.postalCode;
                  }
                  else
                  {
                    fullAddress = item.contact.firstName+' '+item.contact.middleInitial+' '+item.contact.lastName+' '
                    +item.contact.companyName+' '+item.streetLine1 +' '+item.city +', '+item.state +' '+item.postalCode;
                  }

    Сабж))

    rainerg, 23 Февраля 2015

    Комментарии (0)
  6. Си / Говнокод #17682

    +136

    1. 1
    2. 2
    for (j = 0; j < NUM_DMA_BUFFERS; j++)
            *(int *)dev->channel[0].virtDma[j] = 0x1235+j;

    ЯННП

    codemonkey, 23 Февраля 2015

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

    +51

    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
    template<class T, size_t N>
    constexpr size_t sa(T (&)[N])
    {
    	return N;
    };
    
    static std::memory_order mmo[] = 
    {
        memory_order_relaxed,
        memory_order_consume,
        memory_order_acquire,
        memory_order_release,
        memory_order_acq_rel,
        memory_order_seq_cst
    };
    
    std::memory_order current_program_memory_order()
    {
    	return mmo[rand()%sa(mmo)];
    }
    
    void current_program_memory_barier()
    {
    	std::atomic_thread_fence(current_program_memory_order());
    }

    LispGovno, 23 Февраля 2015

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

    +102

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    public class Permuter                                {
        private static void permute(int n, char[] a)     {
            if (n == 0)                                  {
                System.out.println(String.valueOf(a))    ;}
            else                                         {
                for (int i = 0; i <= n; i++)             {
                    permute(n-1, a)                      ;
                    swap(a, n % 2 == 0 ? i : 0, n)       ;}}}
        private static void swap(char[] a, int i, int j) {
            char saved = a[i]                            ;
            a[i] = a[j]                                  ;
            a[j] = saved                                 ;}}

    "I finally figured out how to get those pesky semicolons and curly braces out of my Java code"

    Xom94ok, 22 Февраля 2015

    Комментарии (36)
  9. PHP / Говнокод #17679

    +161

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    there is a reason why opencart is the no.1 most used ecommerce solution in places like china and india, its the easiest code base to understand!
    
    --
    
    many apps servers! what does that mean? you mean different applications running from the same framework?
    you build each application starting from the index.php file and include what ever library classes you require.
    
    --
    
    "I agree with you that it's harder to write simple code, because REPEATING CODE IS HARD TO DEBUG HARD TO READ AND TO CORRECT. so it makes you waste a lot of time."
    
    this is what search and replace is for!

    Создатель опенкарта (Daniel Kerr) исходит на говно, много мякотки
    http://www.techchattr.com/never-use-opencart#comment-1151857248

    Fike, 22 Февраля 2015

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

    +160

    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
    foreach (scandir(DIR.'app'.SLASH.'lib') as $filename) {
        $path = DIR.'app'.SLASH.'lib'.SLASH.$filename;
        if (is_file($path) && substr($filename,strlen($filename)-4,4)=='.php') {
            require_once($path);
        }
    }
    foreach (scandir(DIR.'app'.SLASH.'models') as $filename) {
        $path = DIR.'app'.SLASH.'models'.SLASH.$filename;
        if (is_file($path) && substr($filename,strlen($filename)-4,4)=='.php') {
            require_once($path);
        }
    }
    foreach (scandir(DIR.'app'.SLASH.'ext') as $filename) {
        $path = DIR.'app'.SLASH.'ext'.SLASH.$filename;
        if (is_file($path) && substr($filename,strlen($filename)-4,4)=='.php') {
            require_once($path);
        }
    }

    Начался разбор проблемы со слов заказчика - "сайт долго грузится"

    Crawdaunt, 22 Февраля 2015

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