Code Etiquette Course


#1

I’m new to code and self-taught. One thing that I’ve noticed is that there are some inconsistencies with the code format, which I find confusing. Sometimes a semi-colon is used after a function or property, but then at times it’s not. Then sometimes the next line is indented by a few spaces and others it’s tabbed. Also, where do spaces have to be? Sometimes there are spaces between characters and at other times, there aren’t.

What’s the proper format? Does it make a difference which language we’re using? For instance, in CSS we have to put a semi-colon after each property/value to show that it’s the end, but in JavaScript after a while, for or if/else loop there’s no semi-colon? I’ve noticed sometimes the lessons have loops end with a semi-colon and at other times it doesn't.

A course with clear and consistent standards would be helpful.


#2

When to use semi-colons is always a tricky one, and depends on the language you are using, css you should always use semi-colon. There is a old question the codecademy forum (link) which tells you when to use semi-colons in JS.

Spaces is in most cases for better readability, but you can take it for, certain things you can't split:

height: 50 px;

50px is one argument, this can't have a space. Using tabs/spacing in the beginning is so far i know in most cases a programmers preference


#3

Thanks for the link. It was helpful.

I still have questions about the formatting and believe a course explaining the formatting differences between languages and why they exist would help. There are some formatting specifics that are unclear. Moreover, knowing when and why something is formatted a certain way and in a
particular language is useful info.

As an alternative to a new course, maybe add a section to each language course about any aspects of formatting that are must-knows and/or best practices.

Can you more thoroughly explain spacing? How do you know when you need a space or can go without it? For instance, I've seen an HTML tag and accompanying text written without spacing. The same goes for a CSS attribute. Can I add a space?

CSS Example:
color: red; vs. color:red;


#4

@msasia278

Most people use the Tab key, even when they set it to use spaces when they press it, a single space isn't much. A lot of people (me included) use two spaces, though.

To answer your question, every time you open something (<element> or {, usually), everything inside there should be indented one level until you close it (</element> or }, usually).

Do you mean something like this:

<p>Text</p>

versus this:

<p>
  Text
</p>

? It's personal preference, since HTML doesn't depend on whitespace for stuff*. I like the readability of the second one, but I always feel like the whitespace in it will show up, even though it doesn't. So I usually end up using the first option.

It's personal preference since CSS isn't a whitespace dependent language, but most people I know prefer a space between the property and the value (so, color: red;).

Does this help you understand it better?


* Unless the elements are set to display: inline-block;. It's weird and I'm not sure if anyone knows why it's like this.
See here for more info.


#5

Thanks for the link. I understand the indentation and personal preference for spacing before a line. I was more concerned about understanding times I need to have or not have space between characters.

ex: console.log()
(No space between the "g" in log and the parenthesis)

I also wanted to know if I could have space before a property value, which you explained that I could.
ex: color: red

With the <p>Text<p> example, can there be space after the opening tag and before the closing tag?

<p>Text</p> vs. <p> Text </p>


#6

@msasia278

That's correct. You could put a space there, but I think it's general practice not to.

Either should be fine, the browser ignored the extra spaces in the second one when I tried them :slightly_smiling:


#7

Thanks for your help!