Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clean Code 1

Status
Not open for further replies.

gwillr

IS-IT--Management
Nov 4, 2003
267
CA
General question to gather a wider range of oppinion:

What do you look for when determining how "clean" a piece of code is?

What things are most important?

Gary
 
Simplicity is elegance. There are several hundred cat-skinning methods. The one that involves the fewest cuts gives the best pelt.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

AU.gif
Check out Tek-Tips Australia New Zealand forum1155
NZ.gif
 
GUJUm0deL, by that, you would mean something like the removal of all of the formatting tags, maybe to be replaaced by, say, a css sheet?


what would be the best way to accomplish this conversion quickly and easily? (or is there one?)

Gary
 
I would say something compatible with one of the w3c standards. My favorite is XHTML but even the others are ok. I like all my tags closed, my code to look indented and have as much as possible in the CSS. I don't neccessarily mean complete design but fonts, backgrounds etc.
 
Definately move styles into a style sheet and link it in (has the side effect of allowing this to be cached which may lead to some minor performance boosts if you reuse the style sheet).

I tend to move the javascript (from the <head> section of the document) into an external js file once I am in &quot;tidying up mode&quot;.

Usually I remove client-side html comments and move them server-side (so they are not seen by a view source). Of course this is not always possible (for instance you only have a simple HTML hosting solution available).

Ideally the HTML code left on the page should be just simple tags (I try to avoid tables as much as possible now) and I tend to do a little indenting to make it more obvious what &quot;blocks&quot; of code are related.

Regarding tidying up Javascript code - I put comments above every function (describing it's intended purpose), and simplify the control structures (adding redundant {} pairs to make it 100% obvious what sits where). I look at any javascript that might contain &quot;tight loops&quot; and see if there are changes I can make to reduce the amount of processing... here is an example:

This is not optimal:
Code:
for (var loop = 0; loop < 100; loop++)
{
  document.write(loop);
}

This is more optimal:
Code:
var _html = &quot;&quot;;
for (var loop = 0; loop < 100; loop++)
{
  html += loop.toString(10);
}
document.write(loop);

As you have already heard, there are many ways to do &quot;code cleanup&quot;. It's always good to have a corporate code standard to use as a baseline as well.

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top