Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / ASP

Cleaning Up That Ugly Client-side ASPX Code

3.00/5 (1 vote)
6 Mar 2012CPOL2 min read 9.5K  
An easy way to clean up client-side ASPX code

When managing a big web project, you might find yourself ending up with a big master page, that has a bunch of centralized code client side logic, like includes. What I mean by 'includes', is the part of the code where you spit out all the declarations for JS and CSS files. I got to this stage many times by myself, and I've seen it happen at my various work places as well.

This usually ends up to some big nasty chunk of code that looks like this (and this is a relatively small example of what I mean...):

HTML
<link rel="stylesheet" href="../../Content/bootstrap.css" />
<link rel="stylesheet" href="../../Content/common.css" />
<link rel="stylesheet" href="../../Content/widgets.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" 
type="text/javascript">
</script>
<script src="@Url.Content("~/Scripts/bootstrap/bootstrap-dropdown.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/common.js")" 
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/homepage.js")" 
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/add-form.js")" 
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/search-form.js")" 
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/bookmark-list.js")" 
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/register.js")" 
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/openid.js")" 
type="text/javascript"></script>

Why Is This Bad?

For starters, there's so much similar text here, that typing it all up should've rung a huge bell for us, screaming "THIS IS WRONG!!". The clutter of text here bothers us from seeing what's really important here, and all that is, is the files being included here.

Why Does This Happen?

The main reason for this is because we don't think of client side code as 'actual' code. This leads to us forgetting about DRYing up our code. The equivalent of this in server side code would be to copy the whole body of a certain method a dozen times, and just changing one parameter between different versions. If this were server code, we would've easily spotted the code duplication, and extracted this to a method that receives a parameter and saved a lot of typing, and kept the maintainability of the code.

How Do We Fix This?

All we need to do is introduce a small helper method that will print this again and again for us. We can do this on the client and on the server. In this case, I would go with doing this on the client, just because this piece of code will probably only be useful for us in this specific context.

Here is my clean solution to this problem:

JavaScript
@{ Func<string, string> 
JsRequire = s => "<script src=\"" + Url.Content(s) + 
"\" type=\"text/javascript\"></script>"; }
@{ Func<string, string> CssRequire = s => 
"<script rel=\"stylesheet\" href=\"" + s + "\" />"; }

@CssRequire("../../Content/bootstrap.css")
@CssRequire("../../Content/common.css")
@CssRequire("../../Content/widgets.css")

@JsRequire("https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")
@JsRequire("~/Scripts/bootstrap/bootstrap-dropdown.js")
@JsRequire("~/Scripts/common.js")
@JsRequire("~/Scripts/homepage.js")
@JsRequire("~/Scripts/add-form.js")
@JsRequire("~/Scripts/search-form.js")
@JsRequire("~/Scripts/bookmark-list.js")
@JsRequire("~/Scripts/register.js")
@JsRequire("~/Scripts/openid.js")

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)