-1

i made a login system with JavaScript for a game idea i had, but apparently my ide says it is too complex, do i need to split one function in more pieces? Do it reduces computer processing time? I just don't know if it's critical or not.

Anyway this is the code:

class Log {
  constructor() {
    this.list = {
      username: ["admin", "helper"],
      password: ["admin", "h24"]
    };
    this.user = document.getElementById('username');
    this.psw = document.getElementById('password');
    this.posUser = null;
    this.posPsw = null;
    this.t = true;
  }

  login() {
    if (this.user.value != '' && this.user.value != null) {
      if (!this.list.username.includes(this.user.value.toLowerCase())) {
        errors.innerHTML = 'This user does not exist.';
      } else {
        for (let i = 0; i < this.list.username.length; i++) { //user[pos]
          let j = this.user.value.toLowerCase();
          if (j === this.list.username[i]) {
            this.posUser = i;
          }
        }
        for (let k = 0; k < this.list.password.length; k++) { //psw[pos]
          let l = this.psw.value;
          if (l === this.list.password[k]) {
            this.posPsw = k;
          }
        }
        if (this.posUser === this.posPsw) {
          //access
          console.log('access');
        } else { // user[pos] != psw[pos] then show error
          errors.innerHTML = 'Incorrect password.';
        }
      }
    }
  }
}
let errors = document.querySelector('.error');
let invite = new Log();

document.querySelector('.btnLog').addEventListener('click', function() {
  invite.login();
});
* {
  margin: 5px;
}
<div class="form">
  <div class="inline">
    <label>user</label><input type="text" id="username" autocomplete="off" />
  </div>
  <div class="inline">
    <label>psw</label><input type="password" id="password" autocomplete="off" />
    <div class="eye"></div>
  </div>
  <div class="flex-start">
    <button class="btn btnLog">login</button>
  </div>
  <div class="inline none -error">
    <div class="err_img"></div>
    <div class="error"></div>
  </div>
</div>
David Makogon
  • 69,407
  • 21
  • 141
  • 189
Zambon
  • 37
  • 1
  • 8
  • 3
    Cognitive complexity is about readability of the code, not performance. You can reduce most of your code to `const pwdIndex = this.list.password.findIndex(pwd => pwd === this.psw.value)` and the same for your user. – Roberto Zvjerković Aug 16 '21 at 13:02
  • 2
    Please don't edit your question to add things like 'CLOSED' in a title. If it has an answer, it has an answer. If you've resolved it and the answer isn't here, then either add the proper answer or delete the question. But answers (or proclamations of resolution) don't belong in questions. I rolled back your edit, accordingly. – David Makogon Aug 16 '21 at 13:15
  • it's not a question of cognitive complexity, it's a mini spaghetti code – Mister Jojo Aug 16 '21 at 14:46
  • @MisterJojo and spaghetti code increases cognitive complexity. The metric works. It's not totally accurate all the time but when it's high *chances are* that the code is badly written. – VLAZ Aug 16 '21 at 15:11
  • @VLAZ No, cognition concern knowledge. This is not the case with the spaghetti code, which stems from distraction and a lack of intellectual rigor – Mister Jojo Aug 16 '21 at 15:21

1 Answers1

1

If your IDE uses Sonar to compute the cognitive complexity i suggest you to break up your code in multiple method calls

read this blog post to find out more https://blog.sonarsource.com/cognitive-complexity-because-testability-understandability

Gregy8
  • 94
  • 8