Coding etiquette?

Discuss website building & share your websites and web projects!
Post Reply
User avatar
Petite Plume
Newbie
Posts: 20
Joined: Mon Jun 06, 2022 7:00 pm
Location: France

Coding etiquette?

Post by Petite Plume »

Hi! I just started learning html and css so I can code my website, but there's something the tutorials I've read don't mention (yet?), and it's making me curious.

I understand that as long as my code works in the end and I'm able to read it again, I can write it pretty much how I want, but is there there some sort of etiquette among webdevs for displaying code? Like when skip a line, or when to add an indent, or when to give a div an id instead of a class... This sort of thing.
I feel like this must exist and it's giving me the nagging feeling that my code is "wrong" (even though I know I'm the only one reading it so far).
We're thankful for our Cradle's Graces, but we're not coming back.
User avatar
yequari
Netizen
Posts: 45
Joined: Tue May 10, 2022 7:36 pm
Location: Cyberspace
Website: https://yequari.com

Re: Coding etiquette?

Post by yequari »

Regarding HTML, usually you want all nested elements to be indented from their parent. For example

Code: Select all

<body>
	<div>
		<p>
			Your text here. Maybe a <a href="#">link</a>.
		</p>
	</div>
</body>
As for ids vs classes, the main difference is that ids must be unique. In other words, you can only give one element any given id, but you can give many elements any given class.
User avatar
lavender
Newbie
Posts: 17
Joined: Wed May 11, 2022 2:34 pm
Location: samsara
Website: https://chrysocyon.xyz

Re: Coding etiquette?

Post by lavender »

Petite Plume wrote: Fri Jun 17, 2022 6:51 pm I feel like this must exist and it's giving me the nagging feeling that my code is "wrong" (even though I know I'm the only one reading it so far).
as yequari mentioned, there are some basic formatting strategies like indentation of nested elements, but further from that, there isn't exactly any required way you have to indent it.

formal coding practice in like industry and the like tends to be somewhat strict on formatting and formatting etiquette, but outside of that you're unlikely to really have any issues with anyone over basic formatting. as long as it fits the syntax, imo everything formatting wise beyond that is for readability.

many people recommend the 80 column/character average limit for a single line of code, but that comes out of IBM punch cards from the 20s, there isn't really any particular reason to adhere to it if you don't want to. keeping your lines relatively short can help display with limited screen space in a window, though, i.e. if you don't want to write your code full-screened all the time or have a smaller display.

basically, once you learn the syntax, it's up to you. maybe look at other peoples code, take other peoples suggestions if you find them helpful, but as far as pure formatting goes, i'd argue it's most important it makes sense to you, as you will be the one working on your code most often if you're working independently (usually).
Image
User avatar
ZinRicky
Newbie
Posts: 19
Joined: Tue May 10, 2022 4:09 pm
Location: Italy
Website: https://zinportal.neocities.org

Re: Coding etiquette?

Post by ZinRicky »

lavender wrote: Sat Jun 18, 2022 12:17 pm Many people recommend the 80 column/character average limit for a single line of code, but that comes out of IBM punch cards from the 20s, there isn't really any particular reason to adhere to it if you don't want to. keeping your lines relatively short can help display with limited screen space in a window, though, i.e. if you don't want to write your code full-screened all the time or have a smaller display.
I think the main ratio behind following the 80-char rule is knowing whether your code is meant to be read by humans or by machines. In the first case, you should maximise readability by coherently indenting and wrapping lines; in the second case, you should aim for minimising size.
Nam Sibyllam quidem Cumis ego ipse oculis meis
vidi in ampulla pendere, et cum illi pueri dicerent:
Σίβυλλα τί θέλεις; respondebat illa: ἀποθανεῖν θέλω.
marginalia
Newbie
Posts: 16
Joined: Sun Jul 03, 2022 10:37 am

Re: Coding etiquette?

Post by marginalia »

The Linux terminal emulator is 80x25 characters, and it's the same for many other Unixes as well, which is (probably) where the 80 character limit is from.

Makes sense to limit the width of the code. Maybe 80 is a bit narrow since virtually nobody uses the text mode terminal anymore, but terminal emulators instead, but even so, it's easier to read code that sticks to a narrow width. (For the same reason newspapers have columns, rather than page-wide lines).
There is a crack in everything
That's how the light gets in
Late_Settler
Newbie
Posts: 5
Joined: Sat Jul 09, 2022 2:33 pm

Re: Coding etiquette?

Post by Late_Settler »

Petite Plume wrote: Fri Jun 17, 2022 6:51 pm I understand that as long as my code works in the end and I'm able to read it again, I can write it pretty much how I want, but is there there some sort of etiquette among webdevs for displaying code? Like when skip a line, or when to add an indent, or when to give a div an id instead of a class... This sort of thing.

I feel like this must exist and it's giving me the nagging feeling that my code is "wrong" (even though I know I'm the only one reading it so far).
Some companies use internal style guides to ensure their code looks consistent and easily readable. But for the hobbyist it's entirely up to them. You'll develop your own preferences as you gain experience.
User avatar
sixeyes
Websurfer
Posts: 72
Joined: Wed May 11, 2022 7:39 pm
Website: https://sixey.es/

Re: Coding etiquette?

Post by sixeyes »

...and just wait until you get into the indent-with-tabs vs indent-with-characters debate ^^

It won't likely come up in html/css but there's also practices for naming your variables, like doing compound_word or compoundWord, for example. (I've read studies implying the camelCase variant is harder to get used to than snake_case, but easier to read once you're accustomed to it.) These things are usually "enforced" on a programming language-level.

A more "human" aspect of variable naming is how verbose to be. When coding professionally i've had - due to company policy - to stop naming variables with acronyms and stuff. So instead of calling something "memseg" i have to type out the whole "memorySegment" (and that would still fail review due to not being particular about what that segment would be used for). I found it annoying at first - who actually types temperatureDifference instead of just "delta"? - but i'm really thankful now because it increases readability soo much.

As an aside, as a kid writing PHP i would name my variables after my blorbos. You couldn't understand the code at all, but on the other hand you could easily know which book i was into at the time bc. it would all be $pantalaimon = $belacqua[$serafina] + $kaisa etc etc. Neat!
Image
marginalia
Newbie
Posts: 16
Joined: Sun Jul 03, 2022 10:37 am

Re: Coding etiquette?

Post by marginalia »

Yeah, I think (having worked professionally over a decade), the big thing to focus on should be on readability over rules, especially when there are multiple people involved in the project you spend a lot more time reading code than writing it. Even working alone it's still a good practice, because you're eventually (probably) going to have to figure out why the code isn't working.

Basically look at Arthur Whitney's code, and then do the opposite of that: https://code.jsoftware.com/wiki/Essays/Incunabulum
There is a crack in everything
That's how the light gets in
User avatar
yequari
Netizen
Posts: 45
Joined: Tue May 10, 2022 7:36 pm
Location: Cyberspace
Website: https://yequari.com

Re: Coding etiquette?

Post by yequari »

marginalia wrote: Wed Aug 03, 2022 12:24 pm Yeah, I think (having worked professionally over a decade), the big thing to focus on should be on readability over rules, especially when there are multiple people involved in the project you spend a lot more time reading code than writing it. Even working alone it's still a good practice, because you're eventually (probably) going to have to figure out why the code isn't working.

Basically look at Arthur Whitney's code, and then do the opposite of that: https://code.jsoftware.com/wiki/Essays/Incunabulum
Good god what is that
Post Reply