1

I get the feeling that this function can be shortened, but I'm not sure what would be the best way to do it. I would like to make the following function more efficient. Your help will be greatly appreciated.

My Function

onStudentActionSelect: function () {
      if (this.selectedRows.length === 1) {
        this.$store.commit(SET_MODAL_OPTIONS, {
          modalContent: MODAL_CONTENT.DELETE_STUDENT(this.selectedRows[0].name),
          modalSettings: MODAL_SETTINGS.CANCEL_DELETE,
          modalCallback: this.deleteStudent
        })
      } else if (this.selectedRows.length > 1) {
        this.$store.commit(SET_MODAL_OPTIONS, {
          modalContent: MODAL_CONTENT.DELETE_STUDENTS(this.selectedRows.length),
          modalSettings: MODAL_SETTINGS.CANCEL_DELETE,
          modalCallback: this.deleteStudents
        })
      }
    }
1
  • You could use ternary expressions to turn this into a single commit call, but readability will suffer. Imo, leave it as it is. Commented Jan 15, 2021 at 9:14

3 Answers 3

3

The only thing that changes is the content of your modal, so you can determine that before doing a commit to the store. Simplifying code like this usually involves determining the parts that stay the same, and preparing the rest in variables/parameters.

onStudentActionSelect: function () {
  // Do nothing if we have nothing selected
  if (!this.selectedRows.length) {
    return;
  }

  const modalContent = this.selectedRows.length === 1 ? MODAL_CONTENT.DELETE_STUDENT(this.selectedRows[0].name) : MODAL_CONTENT.DELETE_STUDENTS(this.selectedRows.length);

  this.$store.commit(SET_MODAL_OPTIONS, {
    modalContent,
    modalSettings: MODAL_SETTINGS.CANCEL_DELETE,
    modalCallback: this.deleteStudent
  })
}
Sign up to request clarification or add additional context in comments.

Comments

2

Move the condition to the place where there's a difference between the two condition blocks :

onStudentActionSelect: function () {
   let value;
   if (this.selectedRows.length === 1) {
         value=this.selectedRows[0].name
       }else if(this.selectedRows.length > 1){
          value=this.selectedRows.length
      }
        this.$store.commit(SET_MODAL_OPTIONS, {
          modalContent: MODAL_CONTENT.DELETE_STUDENT(value),
          modalSettings: MODAL_SETTINGS.CANCEL_DELETE,
          modalCallback: this.deleteStudent
        })
      
    }

using the ternary operator doesn't exclude the case when the length===0

Comments

1

Deal with the if/else with ? operator for shortened purpose, here is the code:

onStudentActionSelect: function () {
      this.$store.commit(SET_MODAL_OPTIONS, {
          modalContent: MODAL_CONTENT.DELETE_STUDENT(this.selectedRows.length === 1 ? this.selectedRows[0].name : this.selectedRows.length),
          modalSettings: MODAL_SETTINGS.CANCEL_DELETE,
          modalCallback: this.deleteStudent
        })
    }

1 Comment

You condition will set this.selectedRows.length even when Array.length is 0. But original conditions does not. So it is bad solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.