Spacing in code?!

adamwdd

Honorable
Mar 30, 2013
2
0
10,510
I have been coding for a while and have never known how or when to space/tab on certain lines. Sorry if this has been asked before, I was searching for it for about an hour and a half.
For example: ( ) = Not in the HTML
<title>Create a new thread - Tom's Hardware</title>
(space) <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
(space) <meta name="description" content="Create a new thread" />
(tab) <meta name="robots" content="noindex, nofollow" />
(nospace) <meta name="viewport" content="width=device-width, initial-scale=1" />
(nospcae)<link rel="shortcut icon" href="http://m.bestofmedia.com/i/tomshardware/favicon.png" />

What is this called? Do you space the same for all languages? How do you do know when to put something on a certain line? Thanks for the help in advance.:)
 
Solution
The word you're looking for is 'indentation'. It makes reading the code and following the flow easier. Each language is different.

And if you don't, the person that comes behind you and has to maintain your code will curse you forever.

USAFRet

Illustrious
Moderator
The word you're looking for is 'indentation'. It makes reading the code and following the flow easier. Each language is different.

And if you don't, the person that comes behind you and has to maintain your code will curse you forever.
 
Solution

adamwdd

Honorable
Mar 30, 2013
2
0
10,510


Thank you.
 

randomizer

Distinguished
To expand on this a little, yes, the indendation conventions do differ between languages. This is particularly true for Python, where the indentation is a part of the language syntax and delineates blocks of code. Most languages use whitespace/indentation only for readability, but they usually have some convention on whether to use tabs or spaces (or a combination), and the number to use for each level of indentation.

In the case of HTML it makes no difference for the end user whether you have nice formatting or a single line of spaghetti, because the browser ignores any more than a single space between characters in text and all space around HTML tags. For the maintainer... well, just imagine reading a book with everything on one line.
 

hdeezie80

Honorable
Jul 18, 2012
14
0
10,560
depends I don't know much about html but there is some general guidelines to follow for each language if its for work you should try to get your code looking exactly the same as everyone elses code heres how I usually format

C++:
in c++ style is almost more important than the effectiveness of the code example of acceptable code:
//comment these are important in c++ if your codes going to be read by other developers

class Hello //classes should start with upper case you should have no inline methods
{
     private: // private and public methods should be in the proper section

          //you should always have a comment explaining the method
          string helloWorldStr; //variables should be camel cased
     
     public:
     
          Hello(); //constructor

          //prints hello world
          void printStr (); //functions or methods should be     
};

Hello::Hello() 
{
     helloWorldStr = "Hello world! \n";
}

void Hello::printStr()
{
     cout << helloWorldStr;
}

int main() 
{
     Hello *pH = new Hello; //pointers should have some kind of prefix p in this example most objects will be called as   pointers
     pH->printStr();
     delete pH;

    return 0;
}
java is similar to c/c++

python has indentation built into the language you use the tab character like brackets in c single letter variable names are ok and commenting isn't as important because the language itself is easier to read example:

#comment python version of same c++ code
class Hello:
def __init__(self):
self.h = "hello world!"
def printStr(self):
print self.h

if __name__ == "__main__":
hw = Hello();
hw.printStr()

Like I said style varies base on who you are writing for but