DEV Community

Pavel Belokon
Pavel Belokon

Posted on

Refactoring

This week, I have been refactoring my CLI tool, BukuRain. I used git rebase to consolidate all the commits I made and pushed them.

I focused on making my code more modular and, as a best practice, renamed functions so that they clearly define their purpose.

A good example of what I did is shown below. I split one function, parseText, into two different functions: one to build HTML and the other to actually construct the HTML content:

// Parse text to title and body
function parseText(content, filename) {
  let title;
  let body;

  if (content.length >= 2 && content[1].startsWith("\r\n")) {
    title = content[0];
    body = `<h1>${content[0]}</h1>`;
  } else {
    title = parseFileName(filename);
    body = `<p>${content[0]}</p>`;
  }

  body += content
    .slice(1)
    .map((line) => `<p>${line}</p>`)
    .join("\n");

  return { title, body };
}

// Build html content from text
function buildFromText(content, filename, lang) {
  const { title, body } = parseText(content, filename);
  return buildHtml(title, body, lang);
}

Enter fullscreen mode Exit fullscreen mode

That was the general approach I followed for the refactoring of the rest of the code.

There was one bug I did not notice while testing a pull request from my classmate, which caused all commands except -c to crash because the config file was not provided. This issue was easily resolved with an if statement.

Overall, I believe I did my best, but there is always room for improvement. I am continuously working on enhancing my skills to return later and make further improvements.

Rebasing was straightforward. Git provided clear instructions that I followed. I did encounter some merge conflicts, but they were relatively simple to resolve at this point.

I did make one beginner mistake again, which was writing on the main branch instead of the branch I was supposed to work on. However, I resolved this by cherry-picking the commit to my branch.

Top comments (0)