Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Wasteful repeated method calls

You're calling str.length() multiple times within the same scope. It would be better to call only once, save the result and reuse:

int length = str.length();

Input validation

As @Pimgd @Pimgd pointed out, you may get a NumberFormatException here:

array[i] = Integer.parseInt("" + reverse.charAt(i));

I propose validating like this:

int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
 return false;
}

Naming

These methods could be named better:

  • Instead of validate, how about isValid
  • Instead of creditCardType, how about getCreditCardType

These variables could be named better:

  • Instead of input1Char, how about first1
  • Instead of input2Chars, how about first2
  • Instead of input6Chars, how about first6
  • ... and so on

Unit testing

It's important to test validators thoroughly. Consider adding unit tests, for example:

@Test
public void testValidVisa() {
 assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
 assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
 assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}

These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.

Wasteful repeated method calls

You're calling str.length() multiple times within the same scope. It would be better to call only once, save the result and reuse:

int length = str.length();

Input validation

As @Pimgd pointed out, you may get a NumberFormatException here:

array[i] = Integer.parseInt("" + reverse.charAt(i));

I propose validating like this:

int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
 return false;
}

Naming

These methods could be named better:

  • Instead of validate, how about isValid
  • Instead of creditCardType, how about getCreditCardType

These variables could be named better:

  • Instead of input1Char, how about first1
  • Instead of input2Chars, how about first2
  • Instead of input6Chars, how about first6
  • ... and so on

Unit testing

It's important to test validators thoroughly. Consider adding unit tests, for example:

@Test
public void testValidVisa() {
 assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
 assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
 assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}

These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.

Wasteful repeated method calls

You're calling str.length() multiple times within the same scope. It would be better to call only once, save the result and reuse:

int length = str.length();

Input validation

As @Pimgd pointed out, you may get a NumberFormatException here:

array[i] = Integer.parseInt("" + reverse.charAt(i));

I propose validating like this:

int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
 return false;
}

Naming

These methods could be named better:

  • Instead of validate, how about isValid
  • Instead of creditCardType, how about getCreditCardType

These variables could be named better:

  • Instead of input1Char, how about first1
  • Instead of input2Chars, how about first2
  • Instead of input6Chars, how about first6
  • ... and so on

Unit testing

It's important to test validators thoroughly. Consider adding unit tests, for example:

@Test
public void testValidVisa() {
 assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
 assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
 assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}

These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.

added 11 characters in body
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

WastedWasteful repeated method calls

You're calling str.length() multiple times within the same scope. It would be better to call only once, save the result and reuse:

int length = str.length();

Input validation

As @Pimgd pointed out, you may get a NumberFormatException here:

array[i] = Integer.parseInt("" + reverse.charAt(i));

I propose validating like this:

int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
 return false;
}

Naming

These methods could be named better:

  • Instead of validate, how about isValid
  • Instead of creditCardType, how about getCreditCardType

These variables could be named better:

  • Instead of input1Char, how about first1
  • Instead of input2Chars, how about first2
  • Instead of input6Chars, how about first6
  • ... and so on

Unit testing

It's important to test validators thoroughly. Consider adding unit tests, for example:

@Test
public void testValidVisa() {
 assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
 assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
 assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}

These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.

Wasted method calls

You're calling str.length() multiple times within the same scope. It would be better to call only once, save the result and reuse:

int length = str.length();

Input validation

As @Pimgd pointed out, you may get a NumberFormatException here:

array[i] = Integer.parseInt("" + reverse.charAt(i));

I propose validating like this:

int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
 return false;
}

Naming

These methods could be named better:

  • Instead of validate, how about isValid
  • Instead of creditCardType, how about getCreditCardType

These variables could be named better:

  • Instead of input1Char, how about first1
  • Instead of input2Chars, how about first2
  • Instead of input6Chars, how about first6
  • ... and so on

Unit testing

It's important to test validators thoroughly. Consider adding unit tests, for example:

@Test
public void testValidVisa() {
 assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
 assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
 assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}

These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.

Wasteful repeated method calls

You're calling str.length() multiple times within the same scope. It would be better to call only once, save the result and reuse:

int length = str.length();

Input validation

As @Pimgd pointed out, you may get a NumberFormatException here:

array[i] = Integer.parseInt("" + reverse.charAt(i));

I propose validating like this:

int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
 return false;
}

Naming

These methods could be named better:

  • Instead of validate, how about isValid
  • Instead of creditCardType, how about getCreditCardType

These variables could be named better:

  • Instead of input1Char, how about first1
  • Instead of input2Chars, how about first2
  • Instead of input6Chars, how about first6
  • ... and so on

Unit testing

It's important to test validators thoroughly. Consider adding unit tests, for example:

@Test
public void testValidVisa() {
 assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
 assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
 assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}

These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.

added 1448 characters in body
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

Wasted method calls

You're calling str.length() multiple times within the same scope. It would be better to call only once, save the result and reuse:

int length = str.length();

Input validation

As @Pimgd pointed out, you may get a NumberFormatException here:

array[i] = Integer.parseInt("" + reverse.charAt(i));

I propose validating like this:

int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
 return false;
}

Naming

These methods could be named better:

  • Instead of validate, how about isValid
  • Instead of creditCardType, how about getCreditCardType

These variables could be named better:

  • Instead of input1Char, how about first1
  • Instead of input2Chars, how about first2
  • Instead of input6Chars, how about first6
  • ... and so on

Unit testing

It's important to test validators thoroughly. Consider adding unit tests, for example:

@Test
public void testValidVisa() {
 assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
 assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
 assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}

These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.

Wasted method calls

You're calling str.length() multiple times within the same scope. It would be better to call only once, save the result and reuse:

int length = str.length();

Input validation

As @Pimgd pointed out, you may get a NumberFormatException here:

array[i] = Integer.parseInt("" + reverse.charAt(i));

I propose validating like this:

int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
 return false;
}

Naming

These methods could be named better:

  • Instead of validate, how about isValid
  • Instead of creditCardType, how about getCreditCardType

These variables could be named better:

  • Instead of input1Char, how about first1
  • Instead of input2Chars, how about first2
  • Instead of input6Chars, how about first6
  • ... and so on

Wasted method calls

You're calling str.length() multiple times within the same scope. It would be better to call only once, save the result and reuse:

int length = str.length();

Input validation

As @Pimgd pointed out, you may get a NumberFormatException here:

array[i] = Integer.parseInt("" + reverse.charAt(i));

I propose validating like this:

int digit = reverse.charAt(i) - '0';
if (digit < 0 || digit > 9) {
 return false;
}

Naming

These methods could be named better:

  • Instead of validate, how about isValid
  • Instead of creditCardType, how about getCreditCardType

These variables could be named better:

  • Instead of input1Char, how about first1
  • Instead of input2Chars, how about first2
  • Instead of input6Chars, how about first6
  • ... and so on

Unit testing

It's important to test validators thoroughly. Consider adding unit tests, for example:

@Test
public void testValidVisa() {
 assertTrue(CreditCardValidator.isValid("1234567812342222"));
}
@Test
public void testVisa() {
 assertEquals("Visa", CreditCardValidator.getCreditCardType("4234567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("1234567812342222"));
}
@Test
public void testMaster() {
 assertEquals("MasterCard", CreditCardValidator.getCreditCardType("5134567812342222"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("51345678123422221111"));
}
@Test
public void testDiscover() {
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6011513456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6221263456781234"));
 assertEquals("Discover", CreditCardValidator.getCreditCardType("6441263456781234"));
 assertEquals("Invalid Credit Card Type", CreditCardValidator.getCreditCardType("6741263456781234"));
}

These are just examples. Add more, try to cover all corner cases. Tests like these make refactoring easy too: once you have passing tests for all corner cases, you can boldly go and refactor, knowing that if something breaks, you'll know it immediately.

Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /