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

    +121

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    public static T FirstOrDefault<T>(IEnumerable<T> it)
    {
       foreach (T v in it)
         return v;
       return default(T);
    }

    Самодельный FirstOrDefault.

    Боюсь даже представить, как у автора будет выглядеть Single

    bober_maniac, 31 Января 2011

    Комментарии (3)
  2. C# / Говнокод #5465

    +118

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    public static void Clear(string[] array)
            {
                int n = default(int);
                Array.ForEach(array, element => array[n++] = String.Empty);
            }

    Смешались в кучу кони, люди...

    bober_maniac, 31 Января 2011

    Комментарии (14)
  3. C# / Говнокод #5450

    +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
    public SqlTransaction GetSqlTransaction(string pMd5)
    {
    	if (_connection.State == ConnectionState.Closed)
    	{
    		try
    		{
    			_connection.Open();
    		}
    		catch (SqlException ex)
    		{
    			throw new ApplicationException("Unable to connect to database (" + _connection.DataSource + "/" + _connection.Database + "). Please contact your local IT administrator.", ex);
    		}
    	}
    	else
    	{
    		try
    		{
    			throw new ApplicationException("COUCOU");
    		}
    		catch (ApplicationException ex)
    		{
    			System.Diagnostics.Trace.WriteLine(ex.StackTrace);
    		}
    		sqlTransaction = _connection.BeginTransaction();
    	}
    	return sqlTransaction;
    }

    Код из очередного проекта. А надежда то на коннект все-равно остается! :)

    _Ru55_, 31 Января 2011

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

    +145

    1. 1
    2. 2
    3. 3
    4. 4
    if (!'8'=='=')
    {
          //код
    }

    kasthack, 29 Января 2011

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

    +146

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    //Мега-изобретательный, сцуко, флаг!
    public static bool DONT_UPDATE_INPUTMANAGER = FACEPALM;
    
    ...
    
    //Флаг выпилил, быстра блджад!!1
     if (DONT_UPDATE_INPUTMANAGER) DONT_UPDATE_INPUTMANAGER = false;

    Достаточно недавний мой высер, сделанный на обезумевшую от непоняток голову. Трабла была в том, что при перехода из одного в меню в другое второе меню так же воспринимало эту кнопку и шагало дальше, хотя цикл вроде прошёл и InputManager уже сбросился. Пришлось сделать вот таким вот флагом (правда до сих пор не могу понять КАК я умудрился такой if написать...).

    P.S: Похожая фигня и у MS, см. CurveEditor (create.msdn.com -> education catalog -> tools -> CurveEditor -> переменная disableUIEvents (причём int!!!)).

    RaZeR, 29 Января 2011

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

    +116

    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
    var sEmailRecipient = string.Empty;
    
                        string m_sPhysicalPath = "";
                        if (SaveType == "both" || SaveType == "email")
                        {
                            
                            List<User> recipients = null;
                            if (RecipentSelectMode == "auto")
                            {
                                if (!string.IsNullOrEmpty(AutoRecipient))
                                    recipients = Notification.ConvertToUsers(AutoRecipient, MethodologyId, CurrentObjectId, CurrentUserId, CurrentEntityName);
                            }
                            if(RecipentSelectMode == "manual"){
                                if (Recipient != null)
                                    sEmailRecipient = EvaluateExpression(CurrentUserId, MethodologyId, MainEntityName, MainObjectId, Recipient);
                            }
                            
                             m_sPhysicalPath = Document.AbsoluteApplicationPath + "/" + p_sReportPath.Substring(p_sReportPath.LastIndexOf("storage"));
    
                             if (RecipentSelectMode == "auto")
                             {
                                 foreach (User user in recipients)
                                 {
                                     if (Regex.IsMatch(user.Email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.None))
                                     {
                                         SendReportByEmail(m_sPhysicalPath, user);
                                     }
                                 }
                             }
                             if (RecipentSelectMode == "manual")
                             {
                                 if (Regex.IsMatch(Recipient, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.None))
                                 {
                                     SendReportByEmail(m_sPhysicalPath, new User { Email = sEmailRecipient });
    
                                 }
                                 else
                                 {
                                     return CreateResult(GetClientData(), "Email message sending failed - incorrect email address - " + Recipient, (int)ReportRenderingFailType.Success);
    
                                 }
                        
                             }
    
                        }
                        if (SaveType == "email")
                        {
                            FileInfo file = new FileInfo(m_sPhysicalPath);
                            if (file.Exists)
                                file.Delete();
                        }

    Отправляет отчет по email

    PetrLyapin, 27 Января 2011

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

    +130

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    private int FindIndexOfItemWithValue(object value)
    {
        for (int i = 0; i < base.Items.Count; i++)
        {
            object item = base.Items[i];
            if (InternalUtils.AreValuesEqual(this.SelectedValue, this.GetSelectedValue(item)))
            {
                return i;
            }
        }
        return -1;
    }

    Код комбобокса из System.Windows, Version=2.0.5.0 (Silverlight). Где тут ищется value - загадка природы.

    Unseen, 27 Января 2011

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

    +120

    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
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    96. 96
    97. 97
    98. 98
    static void processCmd(string command) {
    	string[] c_args = command.Split(" ".ToCharArray());
    	switch (c_args[0]) {
    		case "beep":
    			nbr.PlayTone(4096, 500);
    			break;
    		case "exit":
    			exit_op();
    			break;
    		case "info":
    			log_ca("Info:");
    			log_ca(Application.ProductName + " " + Application.ProductVersion);
    			log_ca("listener is " + ((services_running[0]) ? "running" : "down"));
    			log_ca("updater is " + ((services_running[1]) ? "running" : "down"));
    			break;
    		case "start":
    			try {
    				switch (c_args[1]) {
    					case "updater":
    						break;
    					case "listener":
    						if (services_running[0])
    							log_ca("listener is already running");
    						else
    							start_listener();
    						break;
    					default:
    						throw new ArgumentException();
    						break;
    				}
    			}
    			catch {
    				log_ca("Usage: start <service>. Available services: listener, updater.");
    			}
    			break;
    		case "help":
    			foreach (string hs in System.IO.File.ReadAllLines("help.txt")) {
    				log_ca(hs);
    			}
    			break;
    		default:
    			log_ca("\"help\" will display all available commands");
    			break;
    		case "stop":
    			try {
    				nbr.MotorA.Brake();
    				nbr.MotorB.Brake();
    				nbr.MotorC.Brake();
    			}
    			catch { }
    			break;
    		case "run":
    			try {
    				switch(c_args[1]) {
    					case "a":
    						if(arr_motor[0]=="none")
    							log_e("Motor not found or config error");
    						else {
    							if (c_args[4] == "false")
    								nbr.MotorA = new NxtMotor(false);
    							else
    								nbr.MotorA = new NxtMotor(true);
    							nbr.MotorA.Run(Convert.ToSByte(c_args[2]), Convert.ToUInt32(c_args[3]));
    						}
    						break;
    					case "b":
    						if(arr_motor[1]=="none")
    							log_e("Motor not found or config error");
    						else {
    							if (c_args[4] == "false")
    								nbr.MotorB = new NxtMotor(false);
    							else
    								nbr.MotorB = new NxtMotor(true);
    							nbr.MotorB.Run(Convert.ToSByte(c_args[2]), Convert.ToUInt32(c_args[3]));
    						}
    						break;
    					case "c":
    						if(arr_motor[2]=="none")
    							log_e("Motor not found or config error");
    						else {
    							if (c_args[4] == "false")
    								nbr.MotorC = new NxtMotor(false);
    							else
    								nbr.MotorC = new NxtMotor(true);
    							nbr.MotorC.Run(Convert.ToSByte(c_args[2]), Convert.ToUInt32(c_args[3]));
    						}
    						break;
    					default:
    						throw new Exception();
    						break;
    				}
    			}
    			catch (Exception ex) {
    				log_ca("Usage: run <motor> <speed> <tacho> <reverse>. Example: run a 100 0 false.");
    			}
    		break;
    	}
    }

    Мой код, написано 3 года назад.

    A1mighty, 27 Января 2011

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

    +125

    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
    // ****************************************************************************************
            // checkArguments
            // 
            // 
            // ****************************************************************************************
            private bool checkArguments()
            
            {
    
                bool boolSourceUserNameOK      = false;
                bool boolPathOK                = true;
    
                // Check the user name has been provided.
                if (this.UserName != null)
                {
                    if (this.UserName.Length > 0)
                    {
                        boolSourceUserNameOK = true;
                    }
                }
    
                // Check the source path has been provided.
                if (this.SourcePath == null)
                {
                    boolPathOK = false;
                }
                else
                {
                    if (this.SourcePath.Length == 0)
                    {
                        boolPathOK = false;
                    }
                }
    
                return boolSourceUserNameOK && boolPathOK;
    
            }

    Очень поэтичная проверка на две пустые строки =)

    victorko, 25 Января 2011

    Комментарии (30)
  10. C# / Говнокод #5371

    +131

    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
    string name = dr["name"].ToString().Trim();
    
                    //отступы
    
                    switch (lev)
                    {
                        case 0: tc.Text = "&nbsp;" + name; break;
                        case 1: tc.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + name; break;
                        case 2: tc.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + name; break;
                        case 3: tc.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + name; break;
                        case 4:
                        case 5:
                        case 6: tc.Text = "&nbsp;" + name; break;
                    }

    Создание отступов в ячейке таблицы...

    alex_donetsk, 24 Января 2011

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