Comment On Using Design Patterns...or not...

Recently, the anonymous submitter of the following code had been mentoring novice developers in the use of design patterns.  I'm sure one of the things he stressed was creating a reusable template for how to solve a problem that can be used in many many situations. All good stuff. [expand full text]
« PrevPage 1 | Page 2Next »

Re: Using Design Patterns...or not...

2009-03-11 08:03 • by Sandor (unregistered)
case 1
first;
case 2
first;
case 3
first;
case else
WTF; //This should never occur


Re: Using Design Patterns...or not...

2009-03-11 08:03 • by mitschke (unregistered)
Who stole my code?

Re: Using Design Patterns...or not...

2009-03-11 08:14 • by Code Dependent
I see a pattern here.

Re: Using Design Patterns...or not...

2009-03-11 08:16 • by DaveK
248616 in reply to 248615
Code Dependent:
I see a pattern here.
Obviously, the switch needs to be wrapped up in a for loop!

signed, The For Loop Re-Imprisonment Front.

P.s. We have your for loops. Send ten thousand quatloos or you'll never see them again!

Re: Using Design Patterns...or not...

2009-03-11 08:18 • by Anon (unregistered)
Well, the code's being reused. What more do you want?

Re: Using Design Patterns...or not...

2009-03-11 08:18 • by Litote. (unregistered)
What's this got to do with design patterns? This is just tat code.

Re: Using Design Patterns...or not...

2009-03-11 08:21 • by Teh (unregistered)
I think I have to change my glasses, I'm now seeing like quadruple

I found the pattern... right-click and click "View source"

Re: Using Design Patterns...or not...

2009-03-11 08:21 • by seconddevil
There is a name for that pattern, The Copy and Paste Pattern

Re: Using Design Patterns...or not...

2009-03-11 08:33 • by Osno (unregistered)
This should have been solved with an if, like this:

if (v == SOME_CONST1)
{
_panelInstance = DetailPanelFactory.getInstance(type);
_panelInstance.parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance);
}
if (v == SOME_CONST2)
{
_panelInstance = DetailPanelFactory.getInstance(type);
_panelInstance.parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance);
}
if (v == SOME_CONST3)
{
_panelInstance = DetailPanelFactory.getInstance(type);
_panelInstance.parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance);
}
if (v == SOME_CONST4)
{
_panelInstance = DetailPanelFactory.getInstance(type);
_panelInstance.parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance);
}

Re: Using Design Patterns...or not...

2009-03-11 08:34 • by Pim
Yeah, nothing wrong with copy and paste. If you've written something and it works, there's no need to keep writing it again and again.

No, the real WTF is the use of non-descriptive names like SOME_CONST1. That should never happen!

Re: Using Design Patterns...or not...

2009-03-11 08:39 • by Mr^B (unregistered)
248625 in reply to 248618
Litote.:
What's this got to do with design patterns? This is just tat code.


_panelInstance = DetailPanelFactory.getInstance(type);

It's clearly using design patterns!

Re: Using Design Patterns...or not...

2009-03-11 08:49 • by Tom (unregistered)
This allows you to have at least 4 unit tests instead of just 1.

Re: Using Design Patterns...or not...

2009-03-11 08:50 • by whoever (unregistered)
It's really a WTF, it should use a reusable class to do this, like this:

PanelInstanceManager _panelInstance = new PanelInstanceManager();
case SOME_CONST1:
_panelInstance.setInstanceItem(DetailPanelFactory.getInstance(type));
_panelInstance.getInstanceItem().parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance.getInstanceItem());
break;

case SOME_CONST2:
_panelInstance.setInstanceItem(DetailPanelFactory.getInstance(type));
_panelInstance.getInstanceItem().parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance.getInstanceItem());
break;

case SOME_CONST3:
_panelInstance.setInstanceItem(DetailPanelFactory.getInstance(type));
_panelInstance.getInstanceItem().parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance.getInstanceItem());
break;

case SOME_CONST4:
_panelInstance.setInstanceItem(DetailPanelFactory.getInstance(type));
_panelInstance.getInstanceItem().parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance.getInstanceItem());
break;

Re: Using Design Patterns...or not...

2009-03-11 08:56 • by Code Dependent
248631 in reply to 248620
seconddevil:
There is a name for that pattern, The Copy and Paste Pattern
A common derivative of copy-and-paste programming is the "Copy, Paste and Mis-edit" style. I ran across this textbook example when I was tasked with implementing a customer-requested embellishment to an application.

<asp:DropDownList ID="DdlMonth" runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="11">11</asp:ListItem>
<asp:ListItem Value="12">12</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DdlYear" runat="server">
<asp:ListItem Value="4">2007</asp:ListItem>
<asp:ListItem Value="8">2008</asp:ListItem>
<asp:ListItem Value="9">2009</asp:ListItem>
<asp:ListItem Value="10">2010</asp:ListItem>
<asp:ListItem Value="11">2012</asp:ListItem>
<asp:ListItem Value="12">2013</asp:ListItem>
<asp:ListItem Value="13">2014</asp:ListItem>
<asp:ListItem Value="14">2015</asp:ListItem>
</asp:DropDownList>

Note: in DdlYear, the value of 2007 is 4. 2008 through 2010 values are their respective last one or two digits. 2011 is missing, and 2012 through 2015 are off by one in their values. Obviously what happened is the (pseudo-)developer entered the code for DdlMonth, then copy-and-pasted from it to get DdlYear. And then, true to the programming style, mis-edited it.

This application is over three years old, and this has never been caught until now.

Side note: why in hell would anybody hard-code Year values into a DDL?

Re: Using Design Patterns...or not...

2009-03-11 08:57 • by A Nonny Mouse
since when do we poke fun at novices? :( i remember writing garbage like this when i first started out too

Re: Using Design Patterns...or not...

2009-03-11 09:01 • by Teh (unregistered)
Reuse! with a while

while (v == SOME_CONST1 | SOME_CONST2 | SOME_CONST3 | SOME_CONST4 )
{
_panelInstance = DetailPanelFactory.getInstance(type);
_panelInstance.parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance);
break;
}

Re: Using Design Patterns...or not...

2009-03-11 09:01 • by Not THAT Alex (unregistered)
248634 in reply to 248623
Pim:
Yeah, nothing wrong with copy and paste. If you've written something and it works, there's no need to keep writing it again and again.

No, the real WTF is the use of non-descriptive names like SOME_CONST1. That should never happen!


I might be wrong, but I suppose that's because they intend to use this same piece of code again...

I don't know about Design, but I can certainly see a pattern here.

Re: Using Design Patterns...or not...

2009-03-11 09:02 • by Skip (unregistered)
I'm pretty sure I worked with this guy...we had an intern who would dutifully churn out 200 lines of code like this a day.

Re: Using Design Patterns...or not...

2009-03-11 09:09 • by Not THAT Alex (unregistered)
248636 in reply to 248631
Code Dependent:
seconddevil:
There is a name for that pattern, The Copy and Paste Pattern
A common derivative of copy-and-paste programming is the "Copy, Paste and Mis-edit" style. I ran across this textbook example when I was tasked with implementing a customer-requested embellishment to an application.

<asp:DropDownList ID="DdlMonth" runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="11">11</asp:ListItem>
<asp:ListItem Value="12">12</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DdlYear" runat="server">
<asp:ListItem Value="4">2007</asp:ListItem>
<asp:ListItem Value="8">2008</asp:ListItem>
<asp:ListItem Value="9">2009</asp:ListItem>
<asp:ListItem Value="10">2010</asp:ListItem>
<asp:ListItem Value="11">2012</asp:ListItem>
<asp:ListItem Value="12">2013</asp:ListItem>
<asp:ListItem Value="13">2014</asp:ListItem>
<asp:ListItem Value="14">2015</asp:ListItem>
</asp:DropDownList>

Note: in DdlYear, the value of 2007 is 4. 2008 through 2010 values are their respective last one or two digits. 2011 is missing, and 2012 through 2015 are off by one in their values. Obviously what happened is the (pseudo-)developer entered the code for DdlMonth, then copy-and-pasted from it to get DdlYear. And then, true to the programming style, mis-edited it.

This application is over three years old, and this has never been caught until now.

Side note: why in hell would anybody hard-code Year values into a DDL?


In a textbook?!
OK, now that explains a lot.

Re: Using Design Patterns...or not...

2009-03-11 09:12 • by Tuxie (unregistered)
Clearly the developer should had used a DetailPanelCollectionFactoryObserverManagerFactoryInterface instead.

Re: Using Design Patterns...or not...

2009-03-11 09:17 • by Don't get it (unregistered)
It reminds me of a simple web page a consultant implemented. About 15 check boxes on it. About 38,000 lines of code to handle it. Turned out to be a lot of gloppy code, cut and pasted 15 times.

Re: Using Design Patterns...or not...

2009-03-11 09:28 • by m0ffx (unregistered)
248640 in reply to 248616
DaveK:
Code Dependent:
I see a pattern here.
Obviously, the switch needs to be wrapped up in a for loop!

signed, The For Loop Re-Imprisonment Front.

P.s. We have your for loops. Send ten thousand quatloos or you'll never see them again!


'Quatloo'? Is that a squat toilet?

Re: Using Design Patterns...or not...

2009-03-11 09:31 • by Anonymous (unregistered)
248641 in reply to 248638
Don't get it:
It reminds me of a simple web page a consultant implemented. About 15 check boxes on it. About 38,000 lines of code to handle it. Turned out to be a lot of gloppy code, cut and pasted 15 times.

And now it's on his resume as "- Designed and coded a complex web survey application".

Re: Using Design Patterns...or not...

2009-03-11 09:44 • by Your Name (unregistered)
248642 in reply to 248638
Don't get it:
It reminds me of a simple web page a consultant implemented. About 15 check boxes on it. About 38,000 lines of code to handle it. Turned out to be a lot of gloppy code, cut and pasted 15 times.


The Real WTF is it took 2533 and a third lines to handle 15 check boxes.

Re: Using Design Patterns...or not...

2009-03-11 09:44 • by Code Dependent
248643 in reply to 248641
Anonymous:
And now it's on his resume as "- Designed and coded a complex web survey application".
We had a craptastic "programmer" working here several years ago, who was only kept on so the boss wouldn't look like he'd made the same mistake twice (his predecessor had lasted two weeks). In the year that he was here he only produced one application -- a single web page which accepted user input and sent an email. He used to come around to various ones of us asking elementary questions. One conversation I remember:

"Hey, Code, do you know how to call a routine after a user makes a selection in a listbox?"

"Use the onchanged event."

"Onchanged event?"

"Here, I'll show you." (show him)

"Cool. Yeah, that's how I was going to do it, but I was wondering if there might be a better way."

Out of curiosity, I looked this guy up on LinkedIn recently. He has his position with us listed as "Senior Software Developer".

Re: Using Design Patterns...or not...

2009-03-11 09:46 • by Les (unregistered)
S/he got paid $ per line of code.

Re: Using Design Patterns...or not...

2009-03-11 09:55 • by St Mary's Hospital for the Tedious Tedium (unregistered)
Well, this code was optimized to make small ZIP files out of the source code.

He got one thing right, at least.

Re: Using Design Patterns...or not...

2009-03-11 10:15 • by DaveK
248647 in reply to 248640
m0ffx:
DaveK:
Code Dependent:
I see a pattern here.
Obviously, the switch needs to be wrapped up in a for loop!

signed, The For Loop Re-Imprisonment Front.

P.s. We have your for loops. Send ten thousand quatloos or you'll never see them again!


'Quatloo'? Is that a squat toilet?
What on earth would I want ten thousand toilets for when I've only got one arse to sit on them with? I'd wear it through to the bone before I got half-way through done with them!

Re: Using Design Patterns...or not...

2009-03-11 10:20 • by T-Biscuit (unregistered)
248649 in reply to 248632
A Nonny Mouse:
since when do we poke fun at novices? :( i remember writing garbage like this when i first started out too


Yeah, leave the n00b alone, fellas.

Re: Using Design Patterns...or not...

2009-03-11 10:26 • by Dr. F (unregistered)
Ah, I love the smell of <a href="http://en.wikipedia.org/wiki/Cargo_cult_programming">Cargo Cult Programming</a> in the morning!

Re: Using Design Patterns...or not...

2009-03-11 10:26 • by Dr. F (unregistered)
248651 in reply to 248650
D'oh!

Re: Using Design Patterns...or not...

2009-03-11 10:37 • by diaphanein (unregistered)
If at first you don't succeed, copy, paste and fail again.

Re: Using Design Patterns...or not...

2009-03-11 10:39 • by Code Dependent
248653 in reply to 248651
Dr. F:
D'oh!
When you are in edit mode, you'll see a link above the text input box that says BBCode Okay. Click on it.

Re: Using Design Patterns...or not...

2009-03-11 10:57 • by Bobbo (unregistered)
248656 in reply to 248647
DaveK:
What on earth would I want ten thousand toilets for when I've only got one arse to sit on them with? I'd wear it through to the bone before I got half-way through done with them!


What, your arse has a 14 year life-span?

And shouldn't you be in school?? Thinking about it, I shouldn't really be talking to you about your arse should I...

Re: Using Design Patterns...or not...

2009-03-11 11:03 • by Anonymous (unregistered)
Yeh, you may laugh... but thats how Command and Singleton patterns were born.... just give him a couple of weeks...

Re: Using Design Patterns...or not...

2009-03-11 11:08 • by Charles400
Maybe in some bizzaro world programmers aren't allowed to create their own methods.

Re: Using Design Patterns...or not...

2009-03-11 11:11 • by C.L.I.T. Commander (unregistered)
248663 in reply to 248616
DaveK:
The For Loop Re-Imprisonment Front.


Is this related at all to the Coalition for the Liberation of Itinerate Tree-dwellers?

Or the Liberate Apes Before Imprisoning Apes movement?

Re: Using Design Patterns...or not...

2009-03-11 11:25 • by fistposter (unregistered)
If it switches on type, it would be straight-forward to avoid copy and paste programming.

case SOME_CONST1:
_panelInstance = DetailPanelFactory.getInstance(CONST1);
_panelInstance.parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance);
break;

case SOME_CONST2:
_panelInstance = DetailPanelFactory.getInstance(CONST2);
_panelInstance.parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance);
break;

case SOME_CONST3:
_panelInstance = DetailPanelFactory.getInstance(CONST3);
_panelInstance.parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance);
break;

case SOME_CONST4:
_panelInstance = DetailPanelFactory.getInstance(CONST4);
_panelInstance.parseXML(panelList);
// add the new panelItem to the panelCollection
_panelCollection.addItem(_panelInstance);
break;


Copy and paste this!

Re: Using Design Patterns...or not...

2009-03-11 11:27 • by Code Dependent
248671 in reply to 248663
C.L.I.T. Commander:
Is this related at all to the Coalition for the Liberation of Itinerate Tree-dwellers?

Or the Liberate Apes Before Imprisoning Apes movement?
Personal Emulation of Nubile Isis Society

Re: Using Design Patterns...or not...

2009-03-11 11:29 • by baldywilson (unregistered)
The real WTF is the fact that the anonymous submitter decided to send this code to the Daily WTF when the subby was supposed to be mentoring this guy. It's not the novice programmer that's the problem here, its his mentor who clearly can't do his job.

Re: Using Design Patterns...or not...

2009-03-11 11:56 • by Anonymous (unregistered)
Novice or not, some coder looked at this and thought to himself "this is an appropriate way to implement this logic". This, to me, highlights a fundamental flaw in the entire mindset of the coder. Find a different career.

Re: Using Design Patterns...or not...

2009-03-11 11:59 • by Cro (unregistered)
248699 in reply to 248671
Or the Commitee for the Liberation and Integration of Terrifting Lifeforms and their Rehablibiliation into Society.

CAPTCHA: vulputate

Re: Using Design Patterns...or not...

2009-03-11 12:01 • by Bobbo (unregistered)
248703 in reply to 248699
Cro:
Or the Commitee for the Liberation and Integration of Terrifting Lifeforms and their Rehablibiliation into Society.


Genius.

Cro:

CAPTCHA: vulputate


QED.

Re: Using Design Patterns...or not...

2009-03-11 12:02 • by Cro (unregistered)
Or, if I could actually spell (and capitalise correctly);

the Committee for the Liberation and Integration of Terrifying life fOrms and their Rehabilitation Into Society.

Re: Using Design Patterns...or not...

2009-03-11 12:22 • by DaveK
248720 in reply to 248703
Bobbo:
Cro:
Or the Commitee for the Liberation and Integration of Terrifting Lifeforms and their Rehablibiliation into Society.


Genius.
Well yes, but Grant Naylor's genius, not Cro's.

Re: Using Design Patterns...or not...

2009-03-11 12:23 • by Anonymous (unregistered)
248722 in reply to 248705
Cro:
Or, if I could actually spell (and capitalise correctly);

the Committee for the Liberation and Integration of Terrifying life fOrms and their Rehabilitation Into Society.
It's organisms, not lifeforms. It's supposed to be making a humourous acronym, remember? But, FYI, even if correct it wouldn't have been funny. Just so you know for future reference.

Re: Using Design Patterns...or not...

2009-03-11 12:23 • by Cro (unregistered)
248723 in reply to 248720
DaveK:
Bobbo:
Cro:
Or the Commitee for the Liberation and Integration of Terrifting Lifeforms and their Rehablibiliation into Society.


Genius.
Well yes, but Grant Naylor's genius, not Cro's.


Never claimed originality, mate...! :)

Re: Using Design Patterns...or not...

2009-03-11 12:24 • by DaveK
248724 in reply to 248663
C.L.I.T. Commander:
DaveK:
The For Loop Re-Imprisonment Front.


Is this related at all to the Coalition for the Liberation of Itinerate Tree-dwellers?

Or the Liberate Apes Before Imprisoning Apes movement?
Well, let's see:
me:
How would you like a kick in the flrif, then?
Nahh, it's just not the same.

Re: Using Design Patterns...or not...

2009-03-11 12:25 • by DaveK
248725 in reply to 248723
Cro:
DaveK:
Bobbo:
Cro:
Or the Commitee for the Liberation and Integration of Terrifting Lifeforms and their Rehablibiliation into Society.


Genius.
Well yes, but Grant Naylor's genius, not Cro's.


Never claimed originality, mate...! :)
Sorry, wasn't meant to sound accusatory, just giving the original source. Boys from the Dwarf! *wrist-snap*

Re: Using Design Patterns...or not...

2009-03-11 12:27 • by SenTree
Just curious - is there an equivalent to Muphry's law for humourous corrections to novices' code ? (Some of the offerings don't exhibit the same behaviour as the original...).
« PrevPage 1 | Page 2Next »

Add Comment