A
A
Alexander Buki2019-07-03 23:47:04
JavaScript
Alexander Buki, 2019-07-03 23:47:04

What is meant by js code refactoring and how well did I do it for the proposed code?

There is a test task.
You need to refactor the code:

function Journal(date) {
  this.date = date;

  this.formatDate = function(date) {
    return date.getDate() + '.' + (date.getMonth() + 1) + '.' + date.getFullYear();
  };

  this.getTitle = function() {
    return "Выпуск от " + this.formatDate(this.date);
  };

}

Journal.compare = function(journalA, journalB) {
  return journalA.date - journalB.date;
};

// использование:
var journals = [
  new Journal(new Date(2012, 1, 1)),
  new Journal(new Date(2012, 0, 1)),
  new Journal(new Date(2011, 11, 1))
];

function findMin(journals) {
  var min = 0;
  for (var i = 0; i < journals.length; i++) {
    if (Journal.compare(journals[min], journals[i]) > 0) min = i;
  }
  return journals[min];
}

alert( findMin(journals).getTitle() );

If I understand correctly, refactoring is optimization, so I translated everything to es6 to the maximum and applied the sort method for the array, the code was halved:
function Journal(date) {
  this.date = date;
  this.formatDate = date => `${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()}`;
  this.getTitle = () => `Выпуск от ${this.formatDate(this.date)}`;
}
const journals = [
  new Journal(new Date(2012, 1, 1)),
  new Journal(new Date(2012, 0, 1)),
  new Journal(new Date(2011, 11, 1)),
];
journals.sort((a, b) => a.date - b.date);
alert(journals[0].getTitle());

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2019-07-03
@alexbuki

1. Well, where are the ES6 classes, or at least prototypes?
2. String values ​​of dates should have been added to the array and worked with them. This way you get rid of duplication: 3. The Journal class has a compare method, which means that this.date is an implementation detail that can be changed, so instead of sorting by date, you should use the compare method. Or implement the Journal.min method, which will already use sorting in the implementation. 4. Refactoring is not necessarily code reduction. For example, for readability, it was worth putting the minimum date into a variable: or: 5. For title, you can use a getter. This will allow, for example, to use destructuring:new Journal(new Date(/* ... */)),
const { title } = minJournal;

A
Alexander Buki, 2019-07-04
@alexbuki

Subject to comments

class Journal {
  constructor(date) {
    this.date = date;
  }

  formatDate(date) {
    return `${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()}`;
  }

  get title() {
    return `Выпуск от ${this.formatDate(this.date)}`;
  }
}
Journal.compare = (journalA, journalB) => journalA.date - journalB.date;
Journal.min = (journals) => {
  let min = 0;
  for (let i = 0; i < journals.length; i++) {
    if (Journal.compare(journals[min], journals[i]) > 0) min = i;
  }
  return journals[min];
};

const journals = [
  new Journal(new Date(2012, 1, 1)),
  new Journal(new Date(2012, 0, 1)),
  new Journal(new Date(2011, 11, 1)),
];

const minJournal = Journal.min(journals);
const { title } = minJournal;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question