- Published on
How to make beautiful code
- Authors
- Name
- Tien Tran
- Mobile Developer at Use Inc.
Introduction
Yes! I understand a bit about TypeScript. I want to save this knowledge for my upcoming job. I can use that for Java and Swift…
This code is commented not “beautiful”. Here is the code:
function getSrcTokensAndDestTokens(network) {
const tokenInputs = this.srcTokens.concat(this.destTokens);
for (const token of tokenInputs) {
if (network.tokens[token].hidden || network.tokens[token].delisted) {
console.log(`GSI doesn't support ${token} yet!`);
return;
}
}
if (this.srcTokens.length === 0 && this.destTokens.length === 0) {
console.error("srcTokens and destTokens can't be both empty!");
return;
}
if (this.srcTokens.length === 0) {
for (const token in network.tokens) {
if (!network.tokens[token].hidden && !network.tokens[token].delisted) {
this.srcTokens.push(token);
}
}
}
if (this.destTokens.length === 0) {
for (const token in network.tokens) {
if (!network.tokens[token].hidden && !network.tokens[token].delisted) {
this.destTokens.push(token);
}
}
}
// ...
return {
// ...
}
}
Here is how to make it more beautiful.
1
This method implementation is long.
function getSrcTokensAndDestTokens(network)
What is src dest ? So, let’s change the function name
function getSourceTokensAndDestinationTokens(...)
or
function getSourceAndDestinationTokens(...)
… and
const tokenInputs = this.srcTokens.concat(this.destTokens);
=> const tokenInputs = this.sourceTokens.concat(this.destinationTokens);
2
We have a for-loop and an if for checking error here.
hidden
and delisted
are not a good name for boolean values.
for (const token of tokenInputs) {
if (network.tokens[token].hidden ||
network.tokens[token].delisted) {
console.log(`GSI doesn't support ${token} yet!`);
return;
}
}
Refactor
const token = network.tokens[tokenInput] // #1
if (token.isHidden || token.isDelisted) {
// ...
}
Next, This log is hard to search. The output log looks like:
GSI doesn't support ewqdfsfgmvnxdlmfnvmldmswldld yet!
GSI doesn't support ljdilhKHKJBJHJGVKUYIlJNKJNBH yet!
GSI doesn't support KJHJK yet!
Now, let refactor
const ERROR_LOG_NOT_SUPPORTED_TOKEN = "Token not supported"
function isValidTokenInputs(network, tokenInputs) {
for (const tokenInput in tokenInputs) {
const token = network.tokens[tokenInput]
if (token.isHidden || token.isDelisted) {
return [false, tokenInput] // We should create a type instead of using an array or
// a pair because it looses context information
}
}
return [true, ""]
}
function getSrcTokensAndDestTokens(network) {
// ...
const [isSourceTokenValid, wrongSourceTokenInput] =
isValidTokenInputs(network, tokenInputs)
if (!isSourceTokenValid) {
console.log(ERROR_LOG_NOT_SUPPORTED_TOKEN, wrongSourceTokenInput)
return
}
//...
}
3
We have depth nested block here.
if (this.srcTokens.length === 0) {
for (const token in network.tokens) {
if (!network.tokens[token].hidden &&
!network.tokens[token].delisted) {
this.srcTokens.push(token);
}
}
}
if (this.destTokens.length === 0) {
for (const token in network.tokens) {
if (!network.tokens[token].hidden &&
!network.tokens[token].delisted) {
this.destTokens.push(token);
}
}
}
We should define a method to check a token is valid or not
function isTokenValid(token) {
return !token.isHidden && !token.isDelisted
}
… define method for get valid
function getValidTokens(tokens) {
const result = []
for (const token in tokens) {
if (isTokenValid(token) {
result.push(token)
}
}
}
and the two if-s will be
const validNetworkTokens = getValidTokens(network.tokens) // #1
if (this.sourceTokens.length === 0) {
this.sourceTokens = validNetworkTokens // #2
}
if (this.destinationTokens.length === 0) {
this.destinationTokens = validNetworkTokens // #2
}
Updating …