Writing a good Javascript function

  Рет қаралды 4,128

Fredrik Christenson

Fredrik Christenson

Күн бұрын

Пікірлер: 11
@dejo095
@dejo095 5 жыл бұрын
Excellent stuff here. Typescript solves most of the issues you discussed here easily
@FredrikChristenson
@FredrikChristenson 5 жыл бұрын
That is true to a point but these issues are still true for the network calls. iirc I have a video like "The type is a lie" where I show some basic ideas on how to solve that problem, the short version is that you can trust TypeScript as long as you validate the data that comes over the network in a similar manner to how a strictly typed server would serialise data in to a object. Have a great day and thank you so much for watching!
@dejo095
@dejo095 5 жыл бұрын
@@FredrikChristenson thanks, I'll check that video then
@istvan-xyz
@istvan-xyz 6 жыл бұрын
Not sure if I would take advice from someone who claims that a good function should swallow errors.
@FredrikChristenson
@FredrikChristenson 6 жыл бұрын
I suppose the way I think about it is that we should ask how we want to communicate that something is wrong, perhaps throwing a error for a minor piece of logic isn't the way to go if it breaks the entire application but rather logging the error is an alternative, that being said it ofc depends on the nature of the error when we are deciding what to do when something goes wrong. Have a great day and thank you so much for watching!
@istvan-xyz
@istvan-xyz 6 жыл бұрын
Do you not think it's better for the application to crash than to continue on running in an deterministic way? Let's say you have the following: const totalPrice = add(itemPrice, taxes); If for some reason taxes would become some invalid value due to a coding error, I would rather have my application crash then to accidentally start selling everything at an accidental discount. It's also a massive pain to work with a codebase where the errors are swallowed, you never know if there is a genuine error in your code, nothing will tell you. It will be very hard to find the source of the error if every single function is written in a way that they swallow errors.
@FredrikChristenson
@FredrikChristenson 6 жыл бұрын
That is true but let me counter with, do you prefer that your checkout page crashes and stops sales if the failing logic is to correctly extract the display text for the newsletter signup link? I stand by that the nature of the error should determine the behaviour and throwing an error or crashing is not imo the best course of action for every issue. Have a great day and thank you so much for watching!
@istvan-xyz
@istvan-xyz 6 жыл бұрын
try { doMyLogicThatMightFail(); } catch (e) { logErrorAndFailGracefully(); } There is a difference between attempting to make decisions about errors on a functional level and doing it on a high level. There is a difference between rendering some random UI that happens to somewhat work and being explicit about what happens on failure.
@FredrikChristenson
@FredrikChristenson 6 жыл бұрын
There are plenty of functions even in the standards that have this behaviour an example would be indexOf. const index = [1, 2, 3].indexOf(4); Why isn't this throwing an error but rather returning -1? Even if we would rewrite indexOf to throw an error the difference is a try catch vs an if statement to check the return value and the code you provided could be rewritten: const value = doMyLogicThatMightFail(); if(value === -1) { logErrorAndFailGracefully(); } If we want to be picky and talk about how to make a poor version of the try catch: try { doMyLogicThatMightFail(); } catch (e) {} I prefer a try catch solution when the error could be caused by many different things and I need to express what was wrong personally but that is ofc just me. Have a great day and thank you so much for watching!
@i5artube
@i5artube 6 жыл бұрын
It is called JavaScript, not Javascript.
JavaScript Pro Tips - Code This, NOT That
12:37
Fireship
Рет қаралды 2,5 МЛН
JavaScript Best Practices and Coding Conventions - Write Clean Code
28:06
JavaScript Mastery
Рет қаралды 83 М.
UFC 287 : Перейра VS Адесанья 2
6:02
Setanta Sports UFC
Рет қаралды 486 М.
Непосредственно Каха: сумка
0:53
К-Media
Рет қаралды 12 МЛН
Why do senior devs use fancy code instead of simple code?
11:24
Fredrik Christenson
Рет қаралды 794
The Ultimate Guide to Writing Functions
24:31
ArjanCodes
Рет қаралды 187 М.
Clean Code: Functions (Part 1) - Beau teaches JavaScript
8:44
freeCodeCamp.org
Рет қаралды 53 М.
FASTER JavaScript In 2025 With Sets
13:13
Jack Herrington
Рет қаралды 22 М.
Fetch vs Axios
10:53
Fredrik Christenson
Рет қаралды 8 М.
Have you ever had to prove yourself?
11:15
Fredrik Christenson
Рет қаралды 224
This is the Only Right Way to Write React clean-code - SOLID
18:23
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 194 М.
Building a scalable web application in vanilla Javascript
23:56
Fredrik Christenson
Рет қаралды 28 М.