Coding etiquette?

Talk about your website ideas, web design, coding, software and hardware.
Post Reply
User avatar
Petite Plume
Newbie
Reactions:
Posts: 20
Joined: Mon Jun 06, 2022 7:00 pm
Pronouns: They/them

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.
yequari
Websurfer
Reactions:
Posts: 90
Joined: Tue May 10, 2022 7:36 pm

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
Reactions:
Posts: 17
Joined: Wed May 11, 2022 2:34 pm
Pronouns: it/its
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
Netizen
Reactions:
Posts: 31
Joined: Tue May 10, 2022 4:09 pm
Pronouns: He / him, Egli / lui
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
Netizen
Reactions:
Posts: 26
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
Reactions:
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
Webmaster
Reactions:
Posts: 146
Joined: Wed May 11, 2022 7:39 pm
Pronouns: they/them
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
Netizen
Reactions:
Posts: 26
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
yequari
Websurfer
Reactions:
Posts: 90
Joined: Tue May 10, 2022 7:36 pm

Re: Coding etiquette?

Post by yequari »

---
Last edited by yequari on Wed Apr 05, 2023 8:06 pm, edited 1 time in total.
marginalia
Netizen
Reactions:
Posts: 26
Joined: Sun Jul 03, 2022 10:37 am

Re: Coding etiquette?

Post by marginalia »

yequari wrote: Mon Aug 08, 2022 4:15 am
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
It is what happens when an APL programmer writes C code...
There is a crack in everything
That's how the light gets in
User avatar
rekanochi
Newbie
Reactions:
Posts: 22
Joined: Sun Jan 22, 2023 2:13 pm
Pronouns: he/him
Website: https://rekanochi.neocities.org/

Re: Coding etiquette?

Post by rekanochi »

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
i refuse to believe that this is code and not, in fact, just a series of elaborate keysmashes.

as an aside: leaving notes in your code always helps!
Image Image Image

Image
Image
User avatar
nikkiana
Netizen
Reactions:
Posts: 33
Joined: Mon May 16, 2022 5:31 pm
Pronouns: she/her/any
Website: http://nikkiana.neocities.org

Re: Coding etiquette?

Post by nikkiana »

As you might have already gathered from the replies to this.... the answer is yes, but also no, and there are not necessarily consistent rules across the board, and you'll likely find that certain projects, programmers, and employers will have incredibly passionate views over the most mundane of "code standards" or conversely, they won't care at all. It's really a crapshoot from my experience.

If you're just learning and you're mostly doing this for fun right now, I wouldn't worry too hard about it just yet. Just make it readable to you.

Things like indentation and whether to use spaces or tabs, when to skip a line in your code, naming conventions, etc... In HTML and CSS, that stuff doesn't matter with the rendering of your code in a browser, and you'll find about a million opinions on the "right" way to do it.

There are other things that do matter to the rendering, like when to use an id vs. a class. A given ID name should only be used once per page or else you might run into rendering problems, but a class can be used multiple times per page.

And yeah, you're right... this sort of thing can give you a nagging feeling your code is somehow "wrong" when it's not actually... I've been coding for 26 years and I STILL feel that way at times. Honestly, a lot of the time it's just one of those weird ways that a lot of coders from different backgrounds find to be egotistical jerks to one another.
Post Reply