Should functionality that is used only once in UserControl?

Forgive me if this is a slight misuse of the system, but I would like other StackOverflow users to resolve the discussion to a colleague, and I have one.

As a rule, I prefer that each individual element of functionality be encapsulated in UserControl, even if I know that it will be used only once.

My colleague will avoid creating a UserControl in this case and use WebForm, arguing that if it will be used only once, then that the point of creating UserControl is simply adding extra unnecessary overhead.

I argue that the extra overhead is negligible, and this makes more organized code, and who knows, you can reuse the functionality at some point in the future.

So who is right?

Edit

It seems that I can get a lot of answers - my colleague and I agreed that we will calculate the answers for the day and attach the correct answer to any argument that has the most votes.

Further editing

Looks like I'm a winner: D

I ignored the messages, which suggest that we are both right, and counted the answer in support of each side as +1, and everyone voting for this answer as well as +1. I beat 11-6.

+4
source share
7 answers

If the code is better organized, I will always try to create separate user controls.

It really makes communication easier. Think about SRP.

+7
source

Why not just leave it in WebForm and reorganize in UserControl when reusing this function? Remember: YAGNI.

Of course, the code becomes more organized, but there is also a balance that needs to be struck by the number of classes / files that are in your project - if you go overboard, it can be intimidating to find material.

+4
source

I did not encapsulate it in UserControl, so sorry, but I share the opinion of a colleague. UserControls are specifically designed for reusable content (this is a control, right?). Keep it simple, do not retrain, do not create more work for yourself, because "you can reuse functionality at some point in the future." You can not. You will make it control when you really need it.

+3
source

I also prefer to invest everything in User Controls. Firstly, it’s better organized, and you cannot be sure that you don’t want to place this fragment of the user interface somewhere else on your site.

The simple answer is: you're right, your colleague is wrong.

+2
source

Consider the case when there are dozens of such code fragments on the page: does it make sense that all this lives in one form or each form is its own control?

This is not to say that you will be 100% right on this, but it may be a way to punch a hole in your colleague on this issue.

+1
source

Even if you use the code only once - including code in the usercontrol gives you the option of caching fragments. If your application has critical performance, I would prefer to use custom controls. By the time you get to testing the performance of your application, you are unlikely to be able to go back and exaggerate your code in user controls to take advantage of fragment caching.

+1
source

I submit that the additional overhead is negligible

Agreed - trying to micro-optimize performance without using UserControls should be underestimated.

and this makes for more organized code,

He can, and if this happens in your specific context, then yes, that’s a good idea.

and who knows, you can reuse the functionality at some point in the future.

Yes, you can. Or you can’t. This is a wash, and is best refuted by Y (P) AGNI (you (probably) will not need it).

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

0
source

All Articles