Skip navigation.

Broken Windows of BlogsAll recent postsMinimalist Code-Behind Template

Code Samples: Inline or Code-Behind?

When you read ASP.NET–related books, do you prefer ASPX pages with inlined server-side scripts, or ASPX and code-behind separate?

For the sake of this discussion, let’s not delve into such technical matters as missing IntelliSense in ASPX pages, compiler inability to catch errors in inlined code, etc. Let’s focus on the format of samples as they are printed in books. Which format is easier to comprehend? How would you go about making samples concise yet complete? Maybe you have a favorite typeface that would work great for code listings.

I will not offer my opinion at this point, otherwise it may set the overall tone of this discussion as a dictum (and I don’t want that.) I’m interested in your ideas.

Comments

Comment permalink 1 Klaus |
Either way is OK, I can follow both. However, what really bugs me is when books mix the metaphors and do both...be consistent. In most situations the value and clarity of code examples is not in how they are presented, rather how complete and clear a job the author does in dissecting the sample and explaining what is happening.

All being said, I do like it when books present code the way that I work :-) and that's using the code behind model. I find this easier to conceptualise and all to often the value is in the scripting not the html markup.

p.s. My personal peeve, off topic sorry, are markup (X)HTML samples in .NET books that still show inline styles or MS bloated presentation markup. Come on, just because MS products create bloated markup doesn't mean we should encourage it.
Comment permalink 2 Milan Negovan |
Klaus, thank you for feedback. Actually, your comment is very on topic, so to say. I do feel authors need to clean up their act... and their ASPX samples. Those hideous proprietary notions should go.
Comment permalink 3 David O'Hara |
Hands down I prefer the code behind model. While inline does keep things short and sweet, it's not what most of us are used to. As for typeface, please DO NOT use Terminal - it's an awful typeface. I would recommend Monospace or Lucida Console but that's just me. :)
Comment permalink 4 Scott Allen |
I'm leaning towards inline code these days - at least for 2.0.
Comment permalink 5 Shane Shepherd |
I definitely prefer the code-behind method. However, like Klaus said, consistency is key. When I was first learning .NET, one of my key frustrations was translating the examples I was seeing in a book into something that worked.

It seems that very often there is not an adequate discussion of code-behind and how it works. Also, referencing of namespaces in code-behind (i.e. - "using System.Data.SqlClient;") is often sadly overlooked. I end up wasting time researching the correct namespace for the class and methods I'm trying to use.
Comment permalink 6 Sean Chase |
Separate code-behind example boxed and a boxed example of the markup if necessary.
Comment permalink 7 Alex |
I fully agree with David O'Hara.
Comment permalink 8 tom |
I agree with the folks who want it in the code behind. I've never actually used inline C# in an aspx page.

Can I ask what you meant by "inline styles?" Is that when you use style= in your html? Do I need to be using css classes for everything?

Am I being lazy by still using style=? I mean... do I really need to create a class for something I might use once or twice?
Comment permalink 9 Milan Negovan |
Server controls don't make rational use of CSS inline styles. Too often they take control attributes and write out long inline styles.

It's ok to inline if you have a one-off need here and there, but if you style controls in a consistent manner, you need to move these styles into a CSS file.

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

TrackBacks

Sorry, TrackBacks are not allowed.

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):