DEV Community

jjung99
jjung99

Posted on • Edited on

1 1

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.

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay