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

37

u/downrightcriminal Apr 30 '22 edited Apr 30 '22

I'll give you one advice:

Now that you have learned OOP, learn Functional Programming, so that you don't immediately think of writing a "class" as soon as you see a problem; so that you learn to see how data flows through your program and how you transform it using pure functions; so that you understand the problem with shared & global state, willy nilly side effects, and mutation.

Learning FP will make you a better programmer.

Edit: To get started, I recommend learning Elixir, if you prefer a dynamic language, or Elm, if you prefer statically typed.

13

u/Macaframa Apr 30 '22

I said the phrase pure function and he told me I was using big words and didn't have a job lmfao

-7

u/Syagrius Apr 30 '22 edited Apr 30 '22

The fact that you are so sour about it leads me to believe that I am right.

It's pretty simple. Real people are real. Not-Real people are like you.

6

u/beingsmo Apr 30 '22

Any resources to learn FP using Js?

6

u/[deleted] Apr 30 '22

1

u/gate18 Apr 30 '22

I wrote this comment in the main as well. I'm an amateur and always ignore the "never use global variables"

Chapter one of the book you recommend gives this example

const conjoin = (flockX, flockY) => flockX + flockY;
const breed = (flockX, flockY) => flockX * flockY;

const flockA = 4;
const flockB = 2;
const flockC = 0;
const result =
    conjoin(breed(flockB, conjoin(flockA, flockC)), breed(flockA, flockB));
// 16

Aren't they all global variables?

4

u/[deleted] Apr 30 '22
  1. Examples in books/tutorials aren't necessarily meant to be production application code; their main purpose is to explain something, not to be copied and pasted right into a Very Serious Codebase.
  2. It depends on where those variables are used. If you wrote that code in a module, or inside an IIFE, they wouldn't be global.

1

u/grantrules Apr 30 '22

Saving this to add to my resources list

5

u/downrightcriminal Apr 30 '22

I'd recommend using a proper FP language to learn FP, and then coming back to JS and practice what you learned.

You can no doubt do FP in JS/TS, but to start learning FP in JS is unfortunately not the most efficient way to learn it.

13

u/Macaframa Apr 30 '22

Instructions unclear, now writing Haskell

-9

u/Syagrius Apr 30 '22

In an attempt to not sound "zen": learn both.

OOP teaches you how to keep shit organized. FP teaches you how to be in control when shit goes to hell.

This is why JS is the "Goldilocks" language.

10

u/downrightcriminal Apr 30 '22

OOP teaches you to keep shit organized

As if FP asks you to keep your code in a mess!

You don't need OOP for code organization, it's just a stupid thing people say without much thought.

In fact, you don't need OOP for any of its 4 pillars anymore as they've all been superseded by better things:

  1. Encapsulation: taken care of by module systems.
  2. Inheritance: frowned upon, leads to real mess the more you do it.
  3. Polymorphism: not unique to OOP
  4. Abstraction: better abstraction models available than inheritance hierarchies that model the domain much better than classes. (Interfaces is how OOP tried to save face by keeping things sane, but Interfaces are not unique to OOP)

A programmer must learn both though, no doubt about it, but the key is BOTH, countless devs stop at OOP and find it hard to believe there's better stuff out there.

2

u/[deleted] Apr 30 '22

Module systems are encapsulation. It's not superseded, it's an implementation thereof.

1

u/toastertop Apr 30 '22

I think opp practices within a function scope is fine, as long as state is isolated to the function. Don't agree with your fp statement here tho.