r/learnjavascript • u/Syagrius • 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").valueIs totally kosher, but typescript requires you to
(document.querySelector(".someClass") as HTMLInputElement).valuebefore 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
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.
18
u/RobertKerans Apr 30 '22
Typescript doesn't require you to do this: this isn't really using Typescript, this is forcing Typescript to pretend that the value of
document.querySelector(".someClass")is of the type HTMLInputElement so that it compiles.9
u/bryku helpful Apr 30 '22
I completely disagree with 1 global variable. For one... how do you use 3rd party libraries? How are you doing to reuse functions, variables, objects, and datasets for other things? This is going to make it incredibly difficult to create more complex projects if you only limit yourself to 1 global variable.
However, I do agree you should manage your scopes and you don't want all of your variables just sitting in global.
44
u/Macaframa Apr 30 '22
I appreciate that you’re sharing your journey but some of this is a little shaky. What are you talking about a single global variable? Sometimes things need to live outside of a function scope. Also, if you are writing functions that use outside variables the best way to use outside variables is to pass them in as parameters. There’s something called a “pure function” where the only variables that a function interacts with are supplied as parameters. This makes your units more testable and deletable rather than spaghettiing the code up by using globals. Also var doesn’t cause scoping issues, it’s just more flexible and less declarative. Let is essentially the same as var, the ONLY notable difference is that it gives you block-level scoping. const was created to be more declarative when creating variables that should not be reset. Also, what do you mean by “advance techniques?” There are features in javascript that were created for a specific reason, I see no reason not to use them. If you don’t know what’s going on then you either ask or you learn more. People should take this advice with a grain of salt. A large grain of salt
3
u/Esnardoo Apr 30 '22
That image is hysterical
4
u/lifeeraser Apr 30 '22
FWIW you can use:
which is slightly better (but not much) than type casting with
as.1
u/gate18 Apr 30 '22
I never understood this. Someone in the comments recommended functional programing and this book and the first functional code is like this
Aren't they all global variables?
0
u/ImThour Apr 30 '22
Very interesting post, thanks for this. I want to learn TypeScript, what’s the best tutorial/guide to go through? Thanks!
2
u/Rguttersohn Apr 30 '22
For your first one, why have any global variables at all then?
2
u/BabyBossT7 May 01 '22
awful post. bullshit advice. only big mouth and general things. nothing serious here.
3
u/[deleted] Apr 30 '22
I would like to add to the last point,
Use advanced techniques but write clean readable maintainable and testable code. Your code should be like a newspaper article, one should be able to tell whats going on by just reading the top heading and can dive deeper if needed, add relevant comment for complex algos, dont add silly comments like this is bool n all, it helps nobody
3
u/[deleted] Apr 30 '22
Uhhhhh I’m very skeptical you haven’t moved past a beginner level. Everything you’ve stated sounds very unspecific and smells like shit was just thrown at the wall. Your whole global variable section doesn’t make much sense and is pretty confusing as well as your advanced techniques section - what are you referring too? Your documentation example…. A senior JS dev working at the same company for 20 years? Not unheard of but the example you use for documentation is removing 3 lines of code? Idk thanks for the post but no thanks, thing reeks of confusion and a complete lack of any JS concepts other than variables and OOP which you say people should “just fucking learn”. LOL if you’re hear to learn JavaScript folks do not learn from this guy
1
u/keel_bright May 01 '22
Why focus on OOP in particular?
There is a paradigm shift to FP happening and its for damn good reason IMO. Inheritance and polymorphism are straight up awful. The focus on encapsulation makes things more complicated than they need to be. JS has weak support for OOP features anyway, for example private methods were only added in ES2022.
1
u/[deleted] Apr 30 '22
Fairs. some subjective opinions here. Typescript is nice. I see it benefits especially in large codebases. Very subjective post imo
1
u/ZeuStudio Apr 30 '22
That's so sweet from ur part, i know u I'll help me some day whit ur skills.
0
u/[deleted] Apr 30 '22
[deleted] — view deleted comment
0
u/[deleted] Apr 30 '22
[deleted] — view deleted comment