Skip to content

Instantly share code, notes, and snippets.

View MauricioRobayo's full-sized avatar
🧰
Refactoring

Mauricio Robayo MauricioRobayo

🧰
Refactoring
View GitHub Profile
@MauricioRobayo
MauricioRobayo / http-exceptions.txt
Last active June 26, 2022 04:24
http-exceptions.txt
Do not throw HTTP exceptions from non-HTTP flow control code.
It can be tempting when working on an API to throw HTTP exceptions everywhere.
The only application layer that can accurately determine what response to send to the client is the HTTP control flow layer.
BigInt is a built-in object whose constructor returns a bigint primitive — also called a BigInt value, or sometimes just a BigInt — to represent whole numbers larger than 2^53 - 1 (Number.MAX_SAFE_INTEGER), which is the largest number JavaScript can represent with a number primitive (or Number value). BigInt values can be used for arbitrarily large integers.
The term “render prop” refers to a technique for sharing code between React components using a prop whose value is a function.
A component with a render prop takes a function that returns a React element and calls it instead of implementing its own render logic.
All in all, careful design of short methods with low complexity that focus on a single fine-grained concern and stay at a single level of abstraction combined with proper expressive naming will definitely help to better convey the intention of your code to other developers – and to yourself.
When critical resources are not available on startup, the application should print an error message and stop immediately.
The first step towards getting error handling right is to stop the application when its environment is broken. Start agreeing on the environment spec in the first iteration, and make sure that failure to comply with the environment during startup leads to immediate and sudden failure of the application with a sensible error message.
Duplication is far cheaper than the wrong abstraction.
@MauricioRobayo
MauricioRobayo / 0-parallel.js
Last active January 31, 2023 16:19
Parallel async await
const p1Promise = p1();
const p2Promise = p2();
const r1 = await a1;
const r2 = await a2;
@MauricioRobayo
MauricioRobayo / 0-arrow-functions.txt
Last active July 22, 2021 10:44
Methods in ES6 objects: using arrow functions https://stackoverflow.com/a/31095976/2002514 #gogofast
Arrow functions are not designed to be used in every situation merely as a shorter version of old-fashioned functions. The most common use case for arrow functions is as short "lambdas" which do not redefine this, often used when passing a function as a callback to some function.
Arrow functions cannot be used to write object methods because, as you have found, since arrow functions close over the this of the lexically enclosing context, the this within the arrow is the one that was current where you defined the object.
@MauricioRobayo
MauricioRobayo / 0-reduce.txt
Last active June 24, 2021 14:41
All code using array.reduce should be rewritten without array.reduce so it's readable by humans https://twitter.com/jaffathecake/status/1213408744362692608 #gogofast
Object transformations should not use reduce.
Creating an object once, then passing the same object back through reduce is using reduce for decoration only. The only part of reduce it's using is the looping.
The applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly.
@MauricioRobayo
MauricioRobayo / testingAsynchronousCode.js
Last active August 15, 2022 18:26
Testing Asynchronous Code
test('the data is peanut butter', async () => {
const data = await fetchData();
expect(data).toBe('peanut butter');
});
test('the fetch fails with an error', async () => {
expect.assertions(1);
try {
await fetchData();
} catch (e) {