Skip to content

Instantly share code, notes, and snippets.

@deepakshrma
Last active March 5, 2021 16:42
Show Gist options
  • Save deepakshrma/09e4b31d80c34499a4e9ebbce4f8af66 to your computer and use it in GitHub Desktop.
Save deepakshrma/09e4b31d80c34499a4e9ebbce4f8af66 to your computer and use it in GitHub Desktop.
1. How you should not write code

How you should not write code

This is an article from mini-series of How you should not write code. If you know how to write code and want to write better code. Please follow someone like Robert Martin.

const Service = require("./Service");
class ClientWithoutInjection {
constructor() {
this.service = new Service();
}
greet() {
return "Hello " + this.service.getName();
}
}
class ClientWithInjection {
constructor(service) {
this.service = service;
}
greet() {
console.log(this.service.getName);
return "Hello " + this.service.getName();
}
}
exports.ClientWithInjection = ClientWithInjection;
exports.ClientWithoutInjection = ClientWithoutInjection;
const {
ClientWithInjection,
ClientWithoutInjection
} = require('./Client')
const Service = require('./Service')
describe('DI', function () {
describe('Client', function () {
it('check it', function () {
const cw = new ClientWithInjection(new Service())
const co = new ClientWithoutInjection()
expect(cw.greet()).toBe(co.greet())
});
});
});
jest.mock('./Service', () => (function () {
this.getName = () => "something else"
}));
const {
ClientWithoutInjection
} = require('./Client')
describe('DI', function () {
describe('Client', function () {
it('check it', function () {
const co = new ClientWithoutInjection()
expect(co.greet()).toBe("Hello something else")
});
});
});
// Service.js
class Service {
getName() {
return "This is service";
}
}
module.exports = Service;
// nest/nest/nest/nest/nest/nest/nest.js
const Service = require('../../../../../../dependency_injection/client/Service')console.log(new Service().getName())
// package.json
"scripts": {
"nestWithout": "node nest/nest/nest/nest/nest/nest/nest.js",
"test": "jest"
},
// package.json
"scripts": {
"nest": "NODE_PATH=./ node nest/nest/nest/nest/nest/nest/nest.js",
"nestWithout": "node nest/nest/nest/nest/nest/nest/nest.js",
"test": "jest"
},
// nest/nest/nest/nest/nest/nest/nest.js
const Service2 = require('dependency_injection/client/Service')
console.log(new Service2().getName())
// Program:: remove all odds and multiply 5, so that u get all numbers divisible by 10
//Data set
let data = [1, 2, 3, 4, 5];
const MULTIPLIER = 5;
//Data set
//Example 1
let mapped = [];
for (let index = 0, len = data.length; index < len; index++) {
if (data[index] % 2 == 0) mapped.push(data[index] * MULTIPLIER);
}
console.log(mapped);
//Example 2
mapped = data.reduce((curr, next) => {
if (next % 2 == 0) curr = [...curr, next * MULTIPLIER];
return curr;
}, []);
console.log(mapped);
//Break down
mapped = [];
const isEven = num => num % 2 == 0; // Function returns if even
// Accumulator fun to collect all even number
const accumulator = (records, num) => {
// push the current numb at last and create new array
if (isEven(num)) {
records = [...records, num * MULTIPLIER];
}
return records;
};
mapped = data.reduce(accumulator, []);
console.log(mapped);
class Person {
constructor(name) {
this.name = name || null;
}
}
var deepak = new Person();
// how they check
if (deepak.name == null) {
console.log("null")
}
console.log(deepak.name && true); // null
deepak = new Person("");
console.log(deepak.name && true); // null
deepak = new Person(false);
console.log(deepak.name && true); // null
deepak = new Person(0);
console.log(deepak.name && true); // null
// Better option
const NO_NAME = Symbol();
class BetterPerson {
constructor(name) {
this.name = name || NO_NAME;
}
hasName() {
return this.name !== NO_NAME;
}
}
deepak = new BetterPerson(0);
console.log(deepak.hasName()); // false
deepak = new BetterPerson("deepak");
console.log(deepak.hasName()); // true
const delay = () =>
new Promise(r => {
setTimeout(r, 1000);
});
const fetchData = async function(url) {
await delay();
if (url == "fail") throw "CODE_FAIL";
if (url == "fatal") throw "CODE_FATAL";
if (url == "") return "OK";
};
async function main() {
try {
const data = await fetchData("fail");
} catch (e) {
if (e == "CODE_FAIL") {
// Show toast message
console.log(e); // CODE_FAIL
}
if (e == "CODE_FATAL") {
// Show popup message
console.log(e);
}
}
}
main();
// catch function
const onCatchError = e => {
if (e == "CODE_FAIL") {
// Show toast message
console.log(e);
}
if (e == "CODE_FATAL") {
// Show popup message
console.log(e);
}
};
const onSomeOtherCatchError = () => {};
async function mainNew() {
try {
const data = await fetchData("fail")
.catch(onCatchError) // CODE_FAIL
.catch(onSomeOtherCatchError);
} catch (e) {
console.log("Something else handle here");
}
}
mainNew();
const property = Symbol();
class Something {
constructor() {
this[property] = "test";
}
}
var instance = new Something();
console.log(instance["property"]); // undefined
console.log(instance); // Something { [Symbol()]: 'test' }
class Person {
constructor(nm) {
this.setName = function (name) {
nm = name;
};
this.getName = function () {
return nm;
};
}
}
const person = new Person("deepak");
console.log(person.getName()); // "deepak"
person.setName("kapeed");
console.log(person.getName()); // "kapeed"
function PersonF(nm) {
this.setName = function (name) {
nm = name;
};
this.getName = function () {
return nm;
};
}
const personf = new PersonF("deepak");
console.log(personf.getName()); // "deepak"
personf.setName("kapeed");
console.log(personf.getName()); // "kapeed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment