Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / ASP

Cleaning Up That Ugly Client-side ASPX Code

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
6 Mar 2012CPOL2 min read 9.2K   3   1
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)


Written By
Web Developer
Israel Israel
Started programming e-commerce sites with PHP & MySQL at the age of 14. Worked for me well for about 5 years.

Transfered to C# & asp.net, while serving in the IDF.
Worked on the 'Core Performance' Team at ShopYourWay.com (Sears Israel)
Currently working at Logz.io

Check out my blog!
or my twitter

Comments and Discussions

 
GeneralMy vote of 3 Pin
Jimzie from Fresno12-Mar-12 9:25
Jimzie from Fresno12-Mar-12 9:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.