Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expression is already of boolean type #5

Open
tshemsedinov opened this issue Mar 16, 2024 · 5 comments
Open

Expression is already of boolean type #5

tshemsedinov opened this issue Mar 16, 2024 · 5 comments

Comments

@tshemsedinov
Copy link

tshemsedinov commented Mar 16, 2024

static isItnFormatValid(itn: string): boolean {
if (/^0{1,20}$/.test(itn) || itn === 'null' || !/^\d{10}$/.test(itn)) {
return false
}
return true
}

static isItnFormatValid(itn: string): boolean {
  return !/^0{1,20}$/.test(itn) && itn !== 'null' && /^\d{10}$/.test(itn);
}

Also it will be better to remove null comparison and use string operations like .startsWith, .contains and so on in such simple cases at least we do not need regexp.

@vird
Copy link

vird commented Mar 17, 2024

Problem with this code is not only with "already boolean", it has redundant logic

According to tests this code can be rewritten in much simplier way

it('should return false for an ITN consisting of all zeroes', () => {
const itn = '0000000000'
const result = ApplicationUtils.isItnFormatValid(itn)
expect(result).toBe(false)
})

  • should return false for an invalid ITN format
    if (!/^\d{10}$/.test(itn)) return false
  • should return false for an ITN consisting of all zeroes
    Then if (!/^0*$/.test(itn)) return false
    This one also covers should return false for the string "null"

So

static isItnFormatValid(itn: string): boolean {
  // valid format should contain 10 digits, no letters allowed
  if (!/^\d{10}$/.test(itn)) return false
  // edge case. We don't allow all digits to be 0
  if (/^0*$/.test(itn)) return false
  return true
}

So check for null can be removed

@Deimos308
Copy link

@vird last condition can also be simplified to:

static isItnFormatValid(itn: string): boolean {
  // valid format should contain 10 digits, no letters allowed
  if (!/^\d{10}$/.test(itn)) return false
  // edge case. We don't allow all digits to be 0
  return +itn !== 0
}

No additional RegExp is required.

@vird
Copy link

vird commented Mar 18, 2024

Last condition should be not simplified
Reason. If I want to remove part of logic I need remove 1 line
In your case I can't remove last line

@Deimos308
Copy link

Deimos308 commented Mar 18, 2024

It sounds strange to me in several ways:

  1. Using an extra if condition + a regular expression where it can be very easy to skip it.
  2. "If I want..." - sounds like premature optimization in order to have a "universal solution", but what's the point?
    The function is as simple as possible...
  3. "In your case I can't remove last line", do we need it?:
   ...
   // edge case. We don't allow all digits to be 0
   return +itn !== 0
}
  ...
  return true
}

Moreover, in this case I would personally prefer the format with a single line format provided by @tshemsedinov :

   ...
   // valid format should contain 10 digits, no letters allowed
   // edge case. We don't allow all digits to be 0
   return !/^\d{10}$/.test(itn) && +itn !== 0
}

Perhaps I don't fully understand your position... Can you explain with an example please?

@vird
Copy link

vird commented Mar 18, 2024

  • v8 will optimize last condition
  • my solution is more readable
  • my solution is more editable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants