View Full Version : quick question
ajfmrf
04-04-2011, 12:51 AM
I hope I found the right place to ask this.
I see lots of coding where it is indented like this
<div>
</div>
blog {
margin-left: 8px;
Doesn't that bloat the script with all those blank spaces being unused?Or does it matter in that regard.
I think it can make things more confusing but it is done a lot.
Bud
bluewalrus
04-04-2011, 03:10 AM
Why this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
#blog {
margin:8px;
}
</style>
</head>
<body>
<div id="blog">
<p>Content</p>
<ul>
<li>1</li>
<li>2</li>
</ul>
</div>
</body>
</html>
as opposed to
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
#blog { margin:8px;}
</style>
</head>
<body>
<div id="blog">
<p>Content</p>
<ul><li>1</li><li>2</li>
</ul>
</div>
</body>
</html>
The reason is the latter is harder to read and only saves a few bytes at most depending on how big your file is. Almost all connections today will not even not the difference in size it is so small (unless working with an absurdly large file, at that point though you should break the file down).
Schmoopy
04-04-2011, 06:45 AM
As bluewalrus said, it's all about being readable.
Take the following example, first formatted:
<div id="content">
<div id="posts">
<div class="post">
<div class="title"></div>
<div class="post_content">
</div>
</div>
</div>
</div>
Unformatted:
<div id="content">
<div id="posts">
<div class="post">
<div class="title"></div>
<div class="post_content">
</div>
</div>
</div>
</div>
You can see, as the number of nested divs grow, they become much more difficult to track, that's why the first example is a much preferred way of doing things. In the first example, you know exactly which div you're in straight away.
james438
04-04-2011, 07:59 AM
While these ways are the preferred ways to code I find that it is best to code in a way that makes sense to you. Make sure that when you do write a script to do so in such a way that when you go back to it you can easily make sense of it.
I have my own style of indentation that I use, which is somewhat my own and I tend to use some form of notation at times and I also use dividers to separate one element of my script with another.
How do you mean it makes things more confusing?
ajfmrf
04-04-2011, 11:30 PM
I just noticed that my post does not show up correctly.
But most of you figured out what I was talking about anyway.Thats cool!
I guess that due to the fact that I don't do much scripting it makes a difference.
Though I always wondered if there was a reason or concept I was unaware of that most people were applying.
As far as confusing, so far I need to just start from the left and seperate each section from each other when possible.
I do think putting comments in as I work is a great idea that can help when editing is done if a web page is large.
I have been working on a rather large page and editing is becoming more difficult but thankfully I am just putting the finishing touches on it -lol
Bud
djr33
04-05-2011, 12:05 AM
HTML, at least for an experienced designer, is generally easy to read. Using some sort of tabbing is always a good idea, at least not just typing everything on one line or something like that. Using line breaks and tabs to group things visually makes it faster to understand the code and helps to keep everything organized. Beyond that, I pretty much agree with james that you should do what makes sense to you. In HTML, I'm not sure comments are really needed. Marking sections with them (like <!--images section-->) might make make, but beyond that it seems extraneous at least for a fairly experienced designer, as I said. (Of course if you are still learning some of this stuff and adding comments helps, go ahead. There's nothing wrong with that, except, in my opinion, slightly messy course code with extra comments-- I don't like the way comments are formatted in HTML for one thing.)
Applying this more generally to other languages like PHP (I spend most of my time coding PHP) the same applies: tabs are useful, and in a language like PHP (really anything aside from HTML, which is just markup, not really programming) consistent tabs are crucial.
Regarding James's comment about using your own style: that's fine. BUT... make sure that if you ever plan to work with someone, or sell your code to someone who might later need to edit it, that it's close enough to standard that someone can work with it.
I tend to code in a slightly unconventional way, so I need to watch out for that when I'm working with others. A great way to make it easier for them is to use comments. I like comments in PHP because they don't get in the way too much and they're hidden in the final output. And in PHP is can be difficult to remember what something is doing, unlike HTML that is generally surface-oriented.
A quick summary:
1. Always use tabs and line breaks to organize code. (In any way you want as long as it is clear.)
2. Use comments when needed. Use lots of comments. But don't use comments when it is clear from the code itself.
I hope that helps some.
ajfmrf
04-06-2011, 12:16 AM
I try and use as little notes/comments as possible.
The question mostly pertains to some pages that go back and forth from left to right numerous times and I just wondered why.
I understand each post as a valid answer-you all imply it is as each individual needs/wants or prefers.And as usual that makes sense.
I don't write much stuff ( html and css at most) mostly due to the fact I have never learned php,cgi or any other scripting.They look daunting to me and I usually learn better/faster from example.Reading does not do it for me with this type of thing.
I have had issues just with what I am doing to date sometimes coming accross times when I am completely baffled why something won't work.As in a post in the dd scripts about a page with three rss display boxes where only one shows-the other two don't ( I have lost sleep due to this problem and still can't figure out what is wrong.
Anyway thanks for the opinions and help
Bud
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.