How NOT to reverse a list [modified]
-
Does anyone see anything stupid in this code? Here's a hint: it isn't the most efficient logic I've seen.
void CrapClass::reverse_points()
{
CPoint *pt, *npt;
CList <CPoint *, CPoint *> list;
POSITION next, prev;// copy the list
next = m_point_list.GetHeadPosition();
while (next != NULL)
{
pt = m_point_list.GetNext(next);
npt = new CPoint(*pt);
list.AddTail(npt);
}// delete the old list
delete_points();// create the new list in reverse order
prev = list.GetTailPosition();
while (prev != NULL)
{
pt = list.GetPrev(prev);
npt = new CPoint(*pt);
m_point_list.AddTail(npt);
}// delete the points from the temp list
while (!list.IsEmpty())
delete list.RemoveHead();
}modified on Thursday, December 13, 2007 6:52:30 PM
-
Does anyone see anything stupid in this code? Here's a hint: it isn't the most efficient logic I've seen.
void CrapClass::reverse_points()
{
CPoint *pt, *npt;
CList <CPoint *, CPoint *> list;
POSITION next, prev;// copy the list
next = m_point_list.GetHeadPosition();
while (next != NULL)
{
pt = m_point_list.GetNext(next);
npt = new CPoint(*pt);
list.AddTail(npt);
}// delete the old list
delete_points();// create the new list in reverse order
prev = list.GetTailPosition();
while (prev != NULL)
{
pt = list.GetPrev(prev);
npt = new CPoint(*pt);
m_point_list.AddTail(npt);
}// delete the points from the temp list
while (!list.IsEmpty())
delete list.RemoveHead();
}modified on Thursday, December 13, 2007 6:52:30 PM
That code is bad. I love the use of a completely pointless copy the first time round.
Deja View - the feeling that you've seen this post before.
-
Does anyone see anything stupid in this code? Here's a hint: it isn't the most efficient logic I've seen.
void CrapClass::reverse_points()
{
CPoint *pt, *npt;
CList <CPoint *, CPoint *> list;
POSITION next, prev;// copy the list
next = m_point_list.GetHeadPosition();
while (next != NULL)
{
pt = m_point_list.GetNext(next);
npt = new CPoint(*pt);
list.AddTail(npt);
}// delete the old list
delete_points();// create the new list in reverse order
prev = list.GetTailPosition();
while (prev != NULL)
{
pt = list.GetPrev(prev);
npt = new CPoint(*pt);
m_point_list.AddTail(npt);
}// delete the points from the temp list
while (!list.IsEmpty())
delete list.RemoveHead();
}modified on Thursday, December 13, 2007 6:52:30 PM
well the class has been named CrapClass...was the developer in question trying to write bad code?? but i guess u must have renamed it urself... :doh:
modified on Friday, December 14, 2007 9:57:28 AM
-
well the class has been named CrapClass...was the developer in question trying to write bad code?? but i guess u must have renamed it urself... :doh:
modified on Friday, December 14, 2007 9:57:28 AM
-
That code is bad. I love the use of a completely pointless copy the first time round.
Deja View - the feeling that you've seen this post before.
Pete O'Hanlon wrote:
Forum:Coding Horrors Subject:Re: How NOT to reverse a list Sender:Pete O'Hanlon Date:Friday, December 14, 2007 4:49:00 PM That code is bad. I love the use of a completely pointless copy the first time round.
I'm thinking he wanted a really fresh copy -- nothing like data where the bits are still warm. Yummy.
-
Pete O'Hanlon wrote:
Forum:Coding Horrors Subject:Re: How NOT to reverse a list Sender:Pete O'Hanlon Date:Friday, December 14, 2007 4:49:00 PM That code is bad. I love the use of a completely pointless copy the first time round.
I'm thinking he wanted a really fresh copy -- nothing like data where the bits are still warm. Yummy.
CurtD wrote:
nothing like data where the bits are still warm
Souldn't he have done it twice then - just in case the first wasn't heated through warmly enough.
Deja View - the feeling that you've seen this post before.