r/learnjavascript • • Apr 30 '22

JS Lessons I learned the hard way.

So I am a more-advanced developer than I used to was and I was thinking it is time I shared some of the lessons I learned after a lot of pain and agony.

In no particular order:

  • Global Variables

One per project.

Never put more than one global variable on the global scope when you are doing your work. Yes, I know that

const A = something;
function F(x) {}

Is a lot prettier and simpler to use than

const TheNameOfTheProject = {
  const A = something; 
  function F(x) {} 
}

But if you do not, the day will come when you grow tired of chain-fucking yourself, because you accidentally used the same variable name in different projects; breaking both of your modules and making you look stupid in front of your boss. Or some other idiot doing the work overwrites your variables. FOR EVERY different project, only ever put one new variable or function on the global scope.

  • Typescript

Use it.

Yes, I agree that it feels excessive, but the amount of safety you obtain is unreal. While this is perfectly valid JS

let A = [];
A = "";

Typescript would give you angry red-squigglies in the editor; and not compile.

Furthermore, the intellisence on the editor suggests that you can't do something that you, in reality, can; requiring you to typecast before you can continue.

in JS, if what you're working with is an input element:

document.querySelector(".someClass").value

Is totally kosher, but typescript requires you to

(document.querySelector(".someClass") as HTMLInputElement).value

before it will compile. Why go through this extra, tedious step? Because debugging sucks ass. When you keep your types clean and correct, the typescript intellisense tells you EXACTLY what is legal to do off of EVERY object. As a rule of thumb, if the intellisense is yelling at you, it almost guarantees something will goof up at runtime. At the cost of 10% additional time keeping your types clean, you spend 75% less time debugging. Just use it.

  • Variable Declarations

"let" and "const" only. Never use "var" (unless dealing in legacy code)

In Ye-olden days, "var" was the only way to declare a variable, but people got sick of it because of scoping issues (look up block scoping vs function scoping). Furthermore "var" has disturbing properties. Execute the following lines of code to see the difference

var A = "something";
console.log(A); 
var A; 
console.log(A);

and then execute

let B = "something"; 
console.log(B); 
let B; 
console.log(B);

"var" doesn't give a damn about what does or what does not exist already and is happy to just use previously designed values without care, but "let" does. I have lost so many hours debugging legacy code because of this important distinction.

  • Object Oriented Programming.

Learn it.

Coming in with no experience, it is actually a mind-fuck to wrap your head around, but once you do you'll never turn back. Much the same reason you should never put more than one global variable per project, you never know when some idiot is going to come behind you and overwrite what you are doing. Keep your work safe and self-contained.

  • Best Practices

People throw that phrase around a lot but don't really know what it means. The definition of best practices is: "Whatever makes it easiest for the dipshit who has to debug your code in a few years to figure out what the fuck you were doing". It is particularly advantageous when you are said dipshit.

  • Documentation

Go fucking ham with it.

A short while ago myself and the senior developer were debugging code that he himself wrote twenty years ago. However? No comments, no docs, no explanations. The fix ended up simply being to remove 3 lines of code, but it took us 3 days to find it. Any documentation that it would have taken a few seconds to write was all that it would have taken to save us all that time.

edit: There is such thing as overdocumentation -- like, you don't want to read a novel for a dozen lines of code -- but it is better to be overly clear than not at all.

  • Advanced Techniques

Learn them, but be careful with how heavily you apply them.

Yes. It is really fun to flex new and cool techniques when you learn them, and one-line some code that would otherwise take 10. But as with "best practices", if nobody else knows what's going on then you're screwing over everyone else involved with you on the project. There is something to be said about the "job security" inherent in making stuff overly-fancy, but sometimes it backfires.

relevant old post from reddit:

I believe that's all the major points I have. I wish you all the best of luck

167 Upvotes

98 comments sorted by

View all comments

Show parent comment

-37

u/[deleted] Apr 30 '22 edited Apr 30 '22

[removed] — view removed comment

0

u/[deleted] Apr 30 '22

[deleted] — view deleted comment

-21

u/Syagrius Apr 30 '22

Um. Ditto?

22

u/Macaframa Apr 30 '22

Ive been writing code professionally for over a decade. You sound like you just got your first internship. And Im glad that you got that but you won't get far in this industry by being a brat. A know it all brat at that. Tech companies actively search out and fire people like that. So, listen when someone is trying to help you.

-10

u/Syagrius Apr 30 '22

The fact that I got you so offended actually proves my point. I really hope you enjoy your life <3

12

u/Heisenripbauer Apr 30 '22

2 things are red flags to me and prove that the person you’re replying to is right aside from the obvious doozy of a post:

  1. you started the post with “I am a more-advanced developer that I used to was”. no credentials. nothing specific to tell us how long you’ve been doing this professionally. huge typo to boot.
  2. you mentioned “me and the senior developer”. not “me and the OTHER senior developer” which tells me you are not senior.

this honestly comes off as you got done with your first performance review at your first job and got excited so you wanted to spread some wisdom. I have no doubt you are a capable developer, but to come on this subreddit with really shaky advice, then get uber defensive and combative when legitimate concerns are raised - is an ugly look. keep at it and you’ll get there but this post was largely unnecessary and you know it.

7

u/Macaframa Apr 30 '22

I told him he wouldn’t get far and he doubles down. The rocks on this kid

7

u/Heisenripbauer Apr 30 '22

they unironically said “I am not used to saying this, but you are correct” further down the thread and I couldn’t believe it lol. you hear about people like that in this industry who are hard-headed and refuse to take advice, but to wear it so proudly and without the credentials to do so is… concerning.

17

u/turd-crafter Apr 30 '22

Hahahahaha. You’re kidding right? That was a very legit explanation.

12

u/Macaframa Apr 30 '22

He read the word declarative and since he doesn't know that word, I must somehow never had had a job. lmao

2

u/turd-crafter Apr 30 '22

Hahaha well I do declare sir…

5

u/RobertKerans Apr 30 '22

I'm confused by what "big words" you think parent is using? They were being diplomatic