DEV Community

Cover image for Refactoring?!
jsong89
jsong89

Posted on

2 2

Refactoring?!

My Github project repo: Repo
Commit: Commit

Overview

In this time's lab, 5 purpose is for students to do refactoring themselves opensource project. When I saw this lab 5, actually I was being happy because honestly after 3 times working with my collaborators the API's code is getting un-efficient and long. It doesn't mean my classmate's work is useless it was enough helpful, but just need to remodeling :)

What is changed..?

So, after reading this I quickly started to work for it. I separated 3part of to-do things for refactoring each refactoring are..

added start function to handle promise error

I realized that the try and catch function has a promise error, so I changed it like this

async function start() {
    try {
        const { argv } = getParams();
        const {
            input: fileOrDirectory,
            output: outputDir,
            stylesheet: cssUrl,
            config: config,
            lang
        } = argv;
        if(!fileOrDirectory && !config) {
            throw new Error("Please include an input filename or folder");
          }

        await convertFilesToHTML(fileOrDirectory, cssUrl, lang, outputDir, config);
    } catch (err) {
        console.error(err);
        console.log(chalk.red(err.message));
        process.exit(-1);
    }   
}

start();
Enter fullscreen mode Exit fullscreen mode

organized files

I separated some functions into another file.
Image description

reduced duplicated part

const convertToHTML part was duplicated, so I divided them as await getParamsData and await getFileData

exports.convertFilesToHTML = async (filename, cssUrl, lang = "en", outputDir = "dist", config) => {
    const paramsData = await getParamsData(filename, cssUrl, lang = "en", outputDir, config);

    const fileInfos = await getFileData(paramsData.input);

    //function part for generating an index file to go to sample pages.
    await createIndex(paramsData, fileInfos);    
};
Enter fullscreen mode Exit fullscreen mode

Combine all commit to one..

After finishing all refactoring part(currently) I merged all of the commits to one commit from lab 5's instruction(used squash)

commit 10b5254e98ca6dbbfd4982dc671ec14487164821 (HEAD -> master, refactoring)
Author: jsong89 <jsong89@myseneca.ca>
Date:   Thu Oct 14 20:14:47 2021 -0400

    Refactoring ssg to improve code maintainability:
      * added start function to handle promise error
      * organized files
      * reduced duplicated part
Enter fullscreen mode Exit fullscreen mode

Conclusion

After this lab, I realized how much important that before we are going to commit something should not commit every each of the small things. It will make other developers confuse what is the point of the commit and distracting them because too many lists are there. So, I will not commit every moment will carefully committing the essential part.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

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

Okay