views:

144

answers:

7

Forgive me if this is a slight mis-use of the system, but I'd like fellow StackOverflow users to settle a debate a colleague and I are having.

As a general rule, I prefer each separate item of functionality to be encapsulated within a UserControl even if I know it's only going to be used once.

My colleage will eschew creating a UserControl in this circumstance and use a WebForm, arguing that if it's only going to be used once, what's the point of creating a UserControl - it just adds an additional, unnecessary overhead.

I argue that the additional overhead is negligible and it makes for better-organised code, and who knows, you may re-use the functionality at some point in the future.

So who's right?

Edit

It looks like I may get quite a few responses - my colleague and I have agreed that we'll count up the replies in a day and award the correct answer to whichever argument gets the most votes.

Further edit

Looks like I'm the winner :D

I've ignored posts that suggest we're both right, and counted an answer in support of each side as +1, and each up-vote on that answer as a +1 too. I win 11-6.

+7  A: 

If it makes the code better organized, I'll always try to create separate user controls.

This really makes communication easier to follow. Think SRP.

Lennaert
This can also make the code easier to read.
decasteljau
Amen brother. Amen...
Salamander2007
+4  A: 

Why not just leave it in the WebForm and refactor to a UserControl when you do reuse that functionality? Remember: YAGNI.

Sure, Code gets better organized, but there is also a balance to be struck with the amount of classes/files your project has - if you go overboard, it can get daunting to find stuff.

Daren Thomas
Right, see http://stackoverflow.com/questions/437/what-is-your-solution-to-the-fizzbuzz-problem/1902#1902 Enterprise Fizzbuzz for intentional over-engineering!
Davy8
+3  A: 

I'd not encapsulate it in a UserControl, so sorry but I share your colleague's opinion. UserControls are specifically meant for reusable content (it's a control, right?). Keep it simple, don't over-engineer, don't create more work for yourself, because "you may re-use the functionality at some point in the future". You may not. You'll make it a control when you actually need it.

Pawel Krakowiak
So, you're one of those purists who never use table for design because "they are meant for tabular data"?
User
Yes, I am. :) I use DIVs for layout.
Pawel Krakowiak
+1 for the purist!!!!!
Treb
+2  A: 

I also prefer putting everything in User Controls. For once, it is better organized, then you can not be sure you will not want to put that piece of UI somewhere else on your site.

Simple answer: You're right, your colleague is wrong. Now please vote up my answer. :)

User
+1  A: 

Consider the case where there are dozens of such pieces of code on a page: Does it make sense for this all to live in one form or should each form be its own control?

Not to say that you'll be 100% right on this but this may be a way to poke a hole in your co-worker's view on this.

JB King
A: 

I argue that the additional overhead is negligible

Agreed - trying too micro optimize performance by not using UserControls should be frowned upon.

and it makes for better-organised code,

It can, and if it does in your particular context - then, yeah, it's a good idea.

and who knows, you may re-use the functionality at some point in the future.

Yeah, you may. Or you may not. This is a wash, and is best refuted by Y(P)AGNI (You (probably) ain't going to need it).

So - to summarize, it depends on whether it really does make for better organized code.

Mark Brackett
+1  A: 

Even even you use the code only once - putting the code into a usercontrol gives you the option of fragment caching. If your app is performance critical then I would favour user controls. By the time you get to performance testing your app - you are unlikely to be able to go back and refractor your code into user controls to get the benefit of fragment caching.

Tim Brown