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

    +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
    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
    public static string Handle(System.Exception exception)
            {
                try
                {
                    throw exception;
                }
                catch (System.Net.WebException ex)
                {
                    ...
                }
                catch (System.Web.Services.Protocols.SoapHeaderException ex)
                {
                    ...
                }
                catch (System.Web.Services.Protocols.SoapException ex)
                {
                    ...
                }
                catch (ArgumentNullException ex)
                {
                    ...
                }
                catch (NullReferenceException ex)
                {
                    ...
                }
                catch (Exception ex)
                {
                    ...
                }
            }

    кусок кода в чужом проекте, который сейчас допиливаю :(

    shtaff, 19 Января 2012

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

    +969

    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
    public static string ConvertNumberToString(double tmpStr)
            {
                string ret = "";
                try
                {
                    if (((long)tmpStr).ToString().Length > 3)
                    {
                        string len = ((long)tmpStr).ToString();
                        string[] strSplit = tmpStr.ToString().Split(',');
    
                        long tmpM = 0;
                        if (strSplit.Length > 1)
                            tmpM = Convert.ToInt64(strSplit[1]);
    
                        int count = (int)len.Length / 3;
                        ret = len.Substring(0, (len.Length - 3 * count));
                        for (int i = 0; i < count; i++)
                        {
                            ret += " " + len.Substring((ret.Length - i), 3);
                        }
                        if (tmpM > 0)
                        {
                            ret += "," + strSplit[1];
                        }
                    }
                    else
                        ret = tmpStr.ToString();
                }
                catch
                {
                }
                return ret.Trim();
            }

    Из той же оперы...

    yorikim, 17 Января 2012

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

    +126

    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
    public static bool IsLong(string tmpStr)
    {
    	bool blRetVal = true;
    	for (int i = 0; i < tmpStr.Length; i++)
    	{
    		if (tmpStr[i] != '0' && tmpStr[i] != '1' && tmpStr[i] != '2' &&
    			tmpStr[i] != '3' && tmpStr[i] != '4' && tmpStr[i] != '5' &&
    			tmpStr[i] != '6' && tmpStr[i] != '7' && tmpStr[i] != '8' &&
    			tmpStr[i] != '9')
    			blRetVal = false;
    	}
    	return blRetVal;
    }
    
    static public string ConvertDateTimeForSQL(DateTime tmpDateTime)
    {
    	return (
    		tmpDateTime.Year.ToString() + "-" +
    		(tmpDateTime.Month < 10 ? "0" : "") + tmpDateTime.Month.ToString() + "-" +
    		(tmpDateTime.Day < 10 ? "0" : "") + tmpDateTime.Day.ToString() + " " +
    		(tmpDateTime.Hour < 10 ? "0" : "") + tmpDateTime.Hour.ToString() + ":" +
    		(tmpDateTime.Minute < 10 ? "0" : "") + tmpDateTime.Minute.ToString() + ":" +
    		(tmpDateTime.Second < 10 ? "0" : "") + tmpDateTime.Second.ToString());
    }
    
    static public string ConvertDateTimeShortForSQL(DateTime tmpDateTime)
    {
    	return (tmpDateTime.Year.ToString() + "-" +
    		(tmpDateTime.Month < 10 ? "0" : "") + tmpDateTime.Month.ToString() + "-" +
    		(tmpDateTime.Day < 10 ? "0" : "") + tmpDateTime.Day.ToString());
    }
    
    -----------------------------------
    P.S. Версия .NET 3.5

    yorikim, 17 Января 2012

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

    +123

    1. 1
    internal static string TryingDownloadAgainDotDotDot

    Индусы суровы.

    anycolor, 17 Января 2012

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

    +142

    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
    class SMSSender
        {
            const string API_URL = "http://api.sms****.ru/?";
            string base_URL = "";
            private string _email;
            private string _password;
            XmlDocument doc = new XmlDocument();
            Dictionary<string,string> parameters;
    
            public SMSSender(string email, string password)
            {
                _email = email;
                _password = password;
                base_URL = API_URL + "email=" + _email + "&password=" + _password + "&";
            }
    
            public bool LoginAttempt()
            {
                parameters = new Dictionary<string, string>();
                parameters.Add("method", "login");
                return APIRequest(parameters);
            }
    
            public KeyValuePair<int,object> GetCreditsLeft()
            {
                parameters = new Dictionary<string, string>();
                parameters.Add("method", "get_profie");
                APIRequest(parameters);
                return new KeyValuePair<int, object>(0, int.Parse(GetValueByName("credits")));
            }
    
            public int SendSMS(string senderName, string internationalNumber, string text)
            {
                parameters = new Dictionary<string, string>();
                parameters.Add("method", "push_msg");
                parameters.Add("text", text);
                parameters.Add("phone", internationalNumber);
                parameters.Add("sender_name", senderName);
                APIRequest(parameters);
                return int.Parse(GetValueByName("n_raw_sms"));
            }
    
            public KeyValuePair<int, object> GetLastError()
            {
                return new KeyValuePair<int, object>(int.Parse(doc.GetElementsByTagName("err_code")[0].InnerText), doc.GetElementsByTagName("text")[0].InnerText);
            }
    
            private string GetValueByName(string keyToReturn)
            {
                return doc.GetElementsByTagName(keyToReturn)[0].InnerText;
            }
    
            private bool APIRequest(Dictionary<string, string> param)
            {
                string URL = base_URL;
                foreach (KeyValuePair<string, string> p in param)
                    URL = URL + p.Key + "=" + p.Value + "&";
                doc.Load(URL);
                if (GetLastError().Key == 0) return true;
                else throw new SMSSenderException(GetLastError().Key, GetLastError().Value.ToString());
            }
        }
    
        class SMSSenderException : Exception
        {
            int _errorCode;
            string _Message;
            public SMSSenderException(int errorCode, string Message)
            {
                _errorCode = errorCode;
                _Message = Message;
            }
    
            public int ErrorCode
            {
                get { return _errorCode; }
            }
    
            override public string Message
            {
                get { return _Message; }
            }
        }
    }

    API сервер отправки принимает запросы вида http://api.****sms.ru?method=send_msg&phone=+79 123456789&text=abcdef, возвращает простейший XML с err_code и результатом выполнения запроса.
    Казалось бы, 20 строчек кода и проблема решена? Нифига, без специального класса для этого не обойтись. Совсем никак. И уж совсем ничего нельзя делать без специального Exception для этого дела.

    A1mighty, 16 Января 2012

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

    +136

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    bool    isInsideString = false;
    
    ...
    
    isInsideString = (isInsideString == true)? false:true;

    Alx, 16 Января 2012

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

    +110

    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
    public partial class ProductForm : Form
        {
            private delegate bool ProductManipulation();
    
            private static ProductManipulation pmd;
    
            public ProductForm()
            {
                InitializeComponent();
                this.FormClosing += this.ProductForm_Closing;
                ProductsLB.DoubleClick += this.ChangeBtn_Click;
                ProductsLB.Click += this.LoadProductKey;
    
                pmd = LoadDataToLB;
                pmd();
            }
    
    ...
    
            private void AddBtn_Click(object sender, EventArgs e)
            {
                pmd -= LoadDataToLB;
                pmd += AddProduct + pmd;
                pmd += LoadDataToLB;
                pmd(); 
                pmd -= AddProduct;
            }
    
    ...

    Обильное делегирование

    Ekstrem, 13 Января 2012

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

    +131

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    Note that async is a contextual keyword. In all syntactic contexts other than the ones above it is considered an identifier. 
    Thus, the following is allowed (though strongly discouraged!):
    
    using async = System.Threading.Tasks.Task;
    …
    async async async(async async) { }

    Из C# Specifications к Visual Studio Async CTP.

    Em1ss1oN, 12 Января 2012

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

    +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
    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
    string res = "";
                try
                {
                    if (org_id == NewId.ToString())
                    {
                        string query = "delete from ARMVZ_CONFIG where org_id = " + org_id;
                         OdbcCommand cmd = new OdbcCommand(query, getConnect());
                        cmd.ExecuteNonQuery();
                        cmd.CommandText = "delete from organisations where id = " + org_id;
                         cmd.ExecuteNonQuery();
                        cmd.CommandText = "delete from services where orgid = " + org_id;
                        cmd.ExecuteNonQuery();
                        NewId = 0;
                         res = "";
                    }
                    else
                    {
                        try
                        {
                            string query = "select * from tmp_organisations where id = " + org_id;
                             OdbcCommand cmd = new OdbcCommand(query, getConnect());
                            DataTable dt = new DataTable();
                            dt.Load(cmd.ExecuteReader());
                            if (dt.Rows.Count > 0)
                             {
                                query = "update organisations set ";
                                foreach (DataColumn col in dt.Columns)
                                {
                                    if (col.ColumnName != "id")
                                     {
                                        query = query + col.ColumnName +
                                                                 " = (select " + col.ColumnName +
                                                                    @" from tmp_organisations where tmp_organisations.id = " + org_id + " ),";
                                     }
                                }
                                query = query.Remove(query.Length - 1);
                                query = query + " where id = " + org_id;
                                 cmd.CommandText = query;
                                dt.Dispose();
                                cmd.ExecuteNonQuery();
    
                            }
                        }
                        catch
                         {
                            //
                        }
                        try
                        {
                            string query = "select * from tmp_armvz_config where org_id = " + org_id;
                             OdbcCommand cmd = new OdbcCommand(query, getConnect());
                            DataTable dt = new DataTable();
                            dt.Load(cmd.ExecuteReader());
                            if (dt.Rows.Count > 0)
                             {
                                query = "update armvz_config set ";
                                foreach (DataColumn col in dt.Columns)
                                {
                                    if (col.ColumnName != "org_id")
                                     {
                                        query = query + col.ColumnName +
                                                                 " = (select " + col.ColumnName +
                                                                    @" from tmp_armvz_config where tmp_armvz_config.org_id = " + org_id + " ),";
                                     }
                                }
                                query = query.Remove(query.Length - 1);
                                query = query + " where org_id = " + org_id;
                                 cmd.CommandText = query;
                                dt.Dispose();
                                cmd.ExecuteNonQuery();
    
                            }
                        }
                        catch
                         {
                            //
                        }

    метод называется "rollback_transaction". весь метод просто не влез

    bercerker, 11 Января 2012

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

    +119

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    static int getCheckNumber(int n)
    {
    return Average(n, 0);                //Сабж
    }
    
    static int Average(int x, int y)   //Функция вычисления среднего арифметического
    {
    return ((x + y) / 2);
    }

    Среднее арифметическое от произвольной переменной и нуля - эквивалентно делению на 2 :)

    vistefan, 09 Января 2012

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