DEV Community

jjung99
jjung99

Posted on • Updated on

Improving Code through Refactoring

For code improvements, I focused on the following points.

1. Separate files according to functional criteria.

I divided the code related to HTML generation and the code related to the command line, split it into two files, and put it in a util folder. There was one error in this part, but it was removed by adding the following codes.

command.js

module.exports = {
    getArgs
}
Enter fullscreen mode Exit fullscreen mode

html.js

module.exports = {
    isKeyInObject,
    HTMLgenerator
}
Enter fullscreen mode Exit fullscreen mode

index.js

const { getArgs } = require('./util/command');
const { isKeyInObject, HTMLgenerator } = require('./util/html');
Enter fullscreen mode Exit fullscreen mode

2. In each divided file, unnecessary code is removed and structurally modified to make it easier to read.

In my judgment, there were many unnecessary if statements, so I removed them. I removed them because I think it would be better if it showed the error message. Through the first and second processes, I increased the readability of my project.

The part I removed in index.js

if(flags['inputFlag'] == false){
                  argv['input'] = "test/The Naval Treaty.txt"
                  argv['i'] = "test/The Naval Treaty.txt"
                  }
                  if(flags['langFlag'] == false){
                      argv['lang'] = "english"
                      argv['l'] = "english"
                      }
                  if(flags['outputFlag'] == false){
                          argv['output'] = "dist"
                          argv['o'] = "dist"
                          }
Enter fullscreen mode Exit fullscreen mode

3. Add the necessary parts due to the nature of the code function.

I have not previously added a -o, i.e. result value option. Since the output option was in the -config option, I decided that it was also necessary for the command line option and made it work by modifying the code. In addition, I also updated the README.md file according to the updates.

 const output = argv.output ?? 'dist';
        if (!fs.existsSync(output)) {
            fs.mkdirSync(output);
        }
        await fs.promises.writeFile(`${output}/${filename}.html`, getHTML(filename, html, lang));
Enter fullscreen mode Exit fullscreen mode

I committed to each of the above processes. And when I pushed it, there was a git error. Because among the codes I committed, there were overlapping code lines. So I made a manual correction after using fetch, pull git commands.

Discussion (0)