Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is inline code in your aspx pages a good practice?

If I use the following code I lose the ability to right click on variables in the code behind and refactor (rename in this case) them

<a href='<%# "/Admin/Content/EditResource.aspx?ResourceId=" + Eval("Id").ToString() %>'>Edit</a>

I see this practice everywhere but it seems weird to me as I no longer am able to get compile time errors if I change the property name. My preferred approach is to do something like this

<a runat="server" id="MyLink">Edit</a>

and then in the code behind

MyLink.Href= "/Admin/Content/EditResource.aspx?ResourceId=" + myObject.Id;

I'm really interested to hear if people think the above approach is better since that's what I always see on popular coding sites and blogs (e.g. Scott Guthrie) and it's smaller code, but I tend to use ASP.NET because it is compiled and prefer to know if something is broken at compile time, not run time.

like image 624
mjallday Avatar asked Sep 16 '08 01:09

mjallday


1 Answers

I wouldnt call it bad practice (some would disagree, but why did they give us that option in the first place?), but I would say that you'll improve overall readability and maintainability if you do not submit to this practice. You already conveyed out a good point, and that is IDE feature limitation (i.e., design time inspection, compile time warning, etc.).

I could go on and on about how many principles it violates (code reuse, separation of concerns, etc.), but I can think of many applications out there that break nearly every principle, but still work after several years. I for one, prefer to make my code as modular and maintainable as possible.

like image 147
Kilhoffer Avatar answered Oct 02 '22 20:10

Kilhoffer