HopeSeekr's Bad Code Collection
--------------------------------

Hi!  Those of you who have even partially perused xMule's code will at one point
or another find *really* horrible code.  In fact, the level of one's C++ 
efficiency can be determined largely by how horrid the codebase appears to them 
:-).  This is my attempt to catalogue some of the worst pieces of code I have 
found.

File:      SearchList.cpp
================================================================================
First Rev: 1.1
Found Rev: 1.40  
Line(s):   50 - 51
Author:    Unknown eMule dev

Bad Code:
 50    taglist.RemoveAll();
 51    taglist.SetSize(0);

Good Code:
 50    taglist.RemoveAll();

Dissertation:
When a vector is emptied its size is already 0.

File:      FileDetailDialog.cpp
================================================================================
First Rev: 1.7
Found Rev: 1.48
Line(s):   130
Author:    Originally by eMule, made bad by Tiku

Bad Code:
130    bufferS.Printf(wxString(wxT("Unknown")).MakeLower());

Good Code:
130    bufferS = _("unknown");

Dissertation:
Instead of 5 operations this does two.  Clear example of operation-creep.

File:      SearchDlg.cpp
================================================================================
First Rev: 1.32.4.1.2.1
Found Rev: 1.93
Line(s):   1052
Author:    Upload2010

Bad Code:
1050   for (i = 0 ; i < 32768 ; i++)

Good Code:
1050    int count;
1051    count = lsresults->GetItemCount();
1052    for (i = 0; i < count; ++i)

Dissertation:
a) ++i vs i++
* ++i equals: i += 1
* i++ equals: int *tmp = new int[sizeof(i)]; *tmp = i; *tmp++; return i; i = *tmp; 
              delete tmp;

b) With i < GetItemCount(), GetCount would be called at every iteration.
c) 32768 is far smaller than the < 10 searches most people use.

File:      otherfunctions.cpp
================================================================================
First Rev: 1.1
Found Rev: 1.55
Line(s):   41 - 55
Author:    Tiku

Bad Code:
42    CString GetResString(UINT uStringID)
43    {
44        int i;
45        for (i = 0 ; i < itemsof(_resStrings) ; i++)
46        {
47            if (_resStrings[i].id == uStringID)
48            {
49                return CString(wxGetTranslation(_resStrings[i].resstr));
50            }
51        }
52        return "This String is Needen ";
53    }

Good Code:
42    #include <map>                              // Needed for std::map
43
44    void LoadStrings()
45    {
46        for (int a = 0; a < itemsof(_resStrings); ++a)
47        {
48            theApp.msgs[_resStrings[a].id] = _resStrings[a].resstr;
49        }
50    }
51
52    wxString GetResString(unsigned int uStringID)
53    {
54        return theApp.msgs[uStringID];
55    }

Dissertation:
The old GetResString would loop until it found the string.  This would mean at 
most 939 iterations *per string literal*.  There are *many* string literals in
xMule.  GetResString() is the most frequently called function(!).  This new one
exponentially reduces CPU calls.

File:       N/A - Occurs many places
================================================================================
Discovered: 22 July 2004
Author:     Mostly Madcat

Bad Code:
void SomeFunction(CString in);
SomeFunction(CString("asdf").c_str());

Good Code:
void SomeFunction(CString in);
SomeFunction(CString("asdf"));

Dissertation:
If you want a CString, then why pass it a char*? ::boggles::
