Professional Documents
Culture Documents
md 12/3/2019
// bad
var a = 1;
var b = 2;
// good
// Javascript
const a = 1;
let b = 2;
// Typescript
const a: number = 1;
let b: string = 'Hello';
// bad
function getValue(basicValue) {
const ratioA = 1.2;
let newValue = basicValue * ratioA;
const varRatio = 0.85;
newValue *= varRatio;
return newValue;
}
// good
function getValue(basicValue) {
const ratioA = 1.2;
const varRatio = 0.85;
let newValue = basicValue * ratioA;
newValue *= varRatio;
return newValue;
}
// bad
const a = 1;
1/9
highlight-node.md 12/3/2019
// good
const a: number = 1;
let b: string = 'Hello';
4. The variable do not have many data types in the life cycle
// bad
let age = 20;
age = '26';
// good
let age = 20;
age = 26;
// bad
let a = null;
// good
let a = undefined;
// bad
const user: User = {
userId: '0001',
email: 'tunguyenq@gmail.com',
}
const tempUser: User = user;
// good
const user: User = {
userId: '0001',
email: 'tunguyenq@gmail.com',
}
const tempUser: User = {...user};
// bad
2/9
highlight-node.md 12/3/2019
// good
const userIds: string[] = ['0001', '0002', '0003'];
const [ first, second ] = userIds;
// bad
const user: User = {
userId: '0001',
email: 'tunguyenq@gmail.com',
}
const userId = user.userId;
const userName = user.email;
// good
const user: User = {
userId: '0001',
email: 'tunguyenq@gmail.com',
}
const { userId, email } = user;
// bad
for(...){
for(...){
for(...){
for(...){
for(){
}
}
}
}
}
// good
for(...){
for(...){
}
}
3/9
highlight-node.md 12/3/2019
2. Using high order function like: map, reduce, find, forEach instead of for statement.
// bad
const products: Product[] = await getProducts();
for(let i = 0; i < products.length; i ++){
// doing something
}
// good
const products: Product[] = await getProducts();
products.forEach(x=> (...));
products.map(x=> (...));
products.some(x=> (...));
products.every(x=> (...));
products.reduce(x=> (...));
products.reduce(x=> (...));
products.find(x=> (...));
3. If else statement must have bracket although just have only one statement.
// bad
if (test)
return false;
// good
if (test) {
return false;
}
// bad
if(condtionA){
if(conditionB){
if(conditionC){
if(conditionD){
if(conditionHell){
// Holy...
}
}
}
}
}
// good
if(conditionA){
4/9
highlight-node.md 12/3/2019
if(conditionB){
if(conditionC){
if(conditionFinal){
// That's enough, Do not use "if" anymore :(
}
}
}
}
// bad
switch (foo) {
case 1:
const x = 1;
break;
case 2:
const y = 2;
break;
}
// good
switch (foo) {
case 1:
const x = 1;
break;
case 2:
const y = 2;
break;
default:
const x = 3;
break;
}
III. FUNCTION
1. Avoid to override arguments in function.
// bad
function getValue(basicValue) {
const ratioA = 1.5;
basicValue *= ratioA;
return basicValue;
}
// good
function getValue(basicValue) {
5/9
highlight-node.md 12/3/2019
// bad
const userIds = ['0001', '0002', '0003'];
userIds.map(function(x){
// ...
})
// good
const userIds = ['0001', '0002', '0003'];
userIds.map((x) => {
// ...
})
// bad
function getUserStatus(userId) {
// do something
if (status) {
return 'true';
}
return false;
}
// good
function getUserStatus(userId) {
// do something
if (status) {
return true;
}
return false;
}
// bad
function getUserName(userId){
// ...
return {
usersAge,
6/9
highlight-node.md 12/3/2019
usersName
}
}
// good
function getUsersByAge(){
// ...
return usersByAge;
}
function getUsersByGender(){
return usersByGender;
}
5. Function has arguments with default values which are always at the end of list.
// bad
function getUsers(age = 18, gender){
const users = ...;
return users;
}
// good
function getUsers(gender, age = 18){
const users = ...;
return users;
}
IV. OTHERS
1. Using === and !== operators instead of == and !=
// bad
if(a == b){
// TODO:
}
if(a =!){
// TODO:
}
// good
if(a === b){
// TODO:
}
if(a !==){
7/9
highlight-node.md 12/3/2019
// TODO:
}
// bad
function getName(userId){
let deferred = $.Deferred();
// doing something
return deferred.promise();
}
// good
function getName(userId) {
let promise = new Promise((resolve, reject)=>{
if(userId){
resolve();
}else{
reject();
}
})
return promise;
}
async function getName(userId){
const userName = await queryDb(userId);
return userName;
}
// bad
new Promise((resolve, reject) => {
if (ok) {
resolve(result);
}
})
// good
new Promise((resolve, reject) => {
if (ok) {
resolve(result);
}
else {
reject(error)
}
})
8/9
highlight-node.md 12/3/2019
// bad
const Calendar = React.memo(props => {
const [count, setCount] = useState(0);
return <div style={{height : '100px'}}>{count}</div>
})
// good
const Calendar = React.memo(props => {
const [count, setCount] = useState(0);
let timerCount = null;
useEffect(()=>{
timerCount = setInterval(()=>{
setCount(count++)
},1000)
return () => {
clearInterval(timerCount);
}
}, [])
return <div>{count}</div>
})
9/9