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

    +129.9

    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 static void ClearUsers()
            {
                var toremove = new List<string>();
                foreach (var user in SignedUsers.Values)
                {
                    if (!user.IsActive)
                        toremove.Add(user.UserName);
                }
                foreach (var usr in toremove)
                {
                    SignedUsers.Remove(usr);
                }
    
            }

    Удаление юзера

    DavidM, 04 Сентября 2009

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

    +136.4

    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
    public class Xps2Slides
    {
        private int MakeCollection(List<string> data)
        {
            ...
            CallGC();
            ...
        }
    
        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            ...
            CallGC();
            ...
        }
    
        private void makeDeepZoomFiles(string png)
        {
            ...
            CallGC();
        }
    
        private string MakePNG(ref FrameworkElement fe, int pageNumber)
        {
            CallGC();
            ...
            CallGC();
            ...
        }
    
        private void doPNG(string outputPath, ref RenderTargetBitmap bmp)
        {
            ...
            CallGC();
            ...
        }
    
        private void CallGC()
        {
            GC.AddMemoryPressure(300000);// number was picked at random..
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.WaitForFullGCComplete();
        }
    }

    Кандидат на позицию программиста: "There are alot of samples on the internet of such similar code, but nothing that could be used for serially generating these collections on the fly without crashing with a memory overflow error or some other input output issue. I have resolved these problems in the file contained in the sample."

    OlgaWolga, 02 Сентября 2009

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

    +136

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    /// <summary>Read-Only property. Gets the Age.</summary>
    public Int32 Age {
    	get {
    		Int32 age = 0;
    		if(this.dateOfBirth != DateTime.MaxValue){
    			String temp = (DateTime.Now.Subtract(this.dateOfBirth).TotalDays / 365).ToString();
    			age = Convert.ToInt32(temp.Substring(0, temp.IndexOf(".")));
    		}
    		return (age);
    	}
    }

    вот только одно не понимаю -- мочему Int32?

    OlgaWolga, 02 Сентября 2009

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

    +132.9

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    if ("Recap".Equals(Request["post_back"]))
                {
                   <...>
                    if (Request["apply_coupon.x"] != null)
                    {
                        ValidateCoupon();
                    }
                    else if ("Recap".Equals(Request["post_back"]))
                    { <...> }
                   <...>
            }

    Из одного очень древнего проекта, с самопальным post back'ом

    Капитан Очевидность, 02 Сентября 2009

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

    +136

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    public bool IsPositiveNumber(String strNumber)
    {
        Regex objNotPositivePattern = new Regex("[^0-9.]");
        Regex objPositivePattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
        Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
        return !objNotPositivePattern.IsMatch(strNumber) &&
        objPositivePattern.IsMatch(strNumber) &&
        !objTwoDotPattern.IsMatch(strNumber);
    }

    Валидатор :)

    Coffeeholic, 01 Сентября 2009

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

    +127

    1. 1
    2. 2
    3. 3
    4. 4
    if (CProducts.UpdateProductByPartNumber(row.Cells[2].Text, row.Cells[3].Text, Convert.ToDecimal(row.Cells[4].Text), curId, pgId, vendId) != -1)
    {
            // ...
    }

    Эх... первый месяц работы программистом я думал что экономить место - это круто :о)

    Ordos, 31 Августа 2009

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

    +138.3

    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
    /*А вот так РНР программисты пишут код для ASP.NET.
    См проверку типов*/
    
    protected HttpContext CurrentContext
    {
     get {
      return _context;
     }
     set {
      if (typeof(HttpContext) == value.GetType())
       _context = value;
      else
       //...
     }
    }

    ArbuzOFF, 29 Августа 2009

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

    +139

    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
    public void Recalculate(List<DateTime> weekDates,List<HistoricalRateOccurence> historicalRates) {
                UnitsTotal = MonUnits + TuesUnits + WedsUnits + ThursUnits + FriUnits + SatUnits + SunUnits;
                if (historicalRates.Count == 0) {
                    PayAmount = UnitsTotal*PayRate;
                }else {
                    for (int i = 0; i < weekDates.Count; i++) {
                        switch (i) {
                            case 0:
                                PayAmount += MonUnits*GetPayRateForDay(PayRate, weekDates[i], historicalRates);
                                break;
                            case 1:
                                PayAmount += TuesUnits * GetPayRateForDay(PayRate, weekDates[i], historicalRates);
                                break;                            
                            case 2:
                                PayAmount += WedsUnits * GetPayRateForDay(PayRate, weekDates[i], historicalRates);
                                break;                            
                            case 3:
                                PayAmount += ThursUnits * GetPayRateForDay(PayRate, weekDates[i], historicalRates);
                                break;                            
                            case 4:
                                PayAmount += FriUnits * GetPayRateForDay(PayRate, weekDates[i], historicalRates);
                                break;
    
                        }
                    }
                }
                ChargeAmount = UnitsTotal * AmsBillRate;
            }

    Никогда не доверяй циклу!

    xrundelek, 28 Августа 2009

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

    +136.2

    1. 1
    2. 2
    3. 3
    var ids = form.Keys;
    
    if(ids.Length == 0 || ids.Length > 1) { throw Exception;}

    кидать исключение если ids.Length !=1

    xrundelek, 28 Августа 2009

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

    +127.7

    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
    ...
    
    // Импорт функций для работы с MailSlot
    [DllImport("kernel32.dll")]
    static extern int CreateMailslot(
    	string name,
    	int maxMessageSize,
    	int readTimeout,
    	int securityAttributes);
    [DllImport("kernel32.dll")]
    static extern int GetMailslotInfo(
    	int hFile,			// mailslot handle
    	int maxMsgSize,		// maximum message size
    	int* lpcbMessage,	// size of next message
    	int* lpcMessage,	// number of messages
    	int timeout);		// read time-out
    [DllImport("kernel32.dll")]
    static extern int ReadFile(
    	int hFile,
    	void* lpBuffer,
    	int nNumberOfBytesToRead,
    	int* lpNumberOfBytesRead,
    	int overlapped);
    
    ...
    
    // Чтение входящего пакета
    private void readMessage(int cbMessage)
    {
    	int bytesReaden, fResult;
    	byte[] buf = new byte [102400];
    
    	fixed (byte* data = buf)
    	{
    		fResult = ReadFile(
    			handleServer,
    			data,
    			cbMessage,
    			&bytesReaden,
    			0);
    	}
    
    	if (fResult == 0)
    	{
    		textBox_chat.AppendText("--< Невозможно прочесть данные >--\n");
    		return;
    	}
    
    	string str = "";
    	MsgType type = (MsgType)'e';
    	if (buf.Length > 0)
    	{
    		type = (MsgType)buf[0];
    		for (int i = 0; i < bytesReaden; i++)
    			str += BitConverter.ToChar(buf,i*2);
    		//str = buf.ToString();
    		str = str.Remove(0, 1);
    	}
    
    	switch (type)
    	{
    		...
    	}		
    }
    
    ...

    Учебная задача: чат на MailSlot.
    Битва с шарпом за указатели, за массивы и т.д.

    k06a, 26 Августа 2009

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