Skip to main content
added 1 character in body
Source Link
Robert Harvey
  • 200.1k
  • 55
  • 468
  • 679

There are few possible reasons for this sort of thing:

  1. As in JacquesB's answer it may simply be convenienceconvenience for the library user to call a single method rather than two methods and keep their code more succinct.

  2. PerformancePerformance may be a consideration, calling. Calling .substring(3) will result in a new stingstring being created, and there foretherefore you are looping over the string twice, it is possible*possible 1 for .startsWith("bar", 3) to be implemented without creating a temporary substring and to just loop through the string once. .startsWith has another tiny optimization point,: it can bailoutbail out early if the first character does not match, .indexOf will need to keep checking the rest of the string.

  3. Better expressing intent,Expressing intent: while its fairly obvious what you are doing with either of your two variants it does take the reader a little while to consider the purpose compared to a single method call that will tell you exactly what you are doing.

  4. As Phillipp points out, historyhistory can be a factor, adding new methods to an API is fairly easy, changing or removing existing methods is much harder as it will break existing code, so sometimes you may just be seing an artefact of the history of the development of the API**API 2.

* I have no idea if this is actually the case here but I'd be surprised if it does.1 I have no idea if this is actually the case here but I'd be surprised if it does.
** Probably not in this case though.2 Probably not in this case though.

There are few possible reasons for this sort of thing:

  1. As in JacquesB's answer it may simply be convenience for the library user to call a single method rather than two methods and keep their code more succinct.

  2. Performance may be a consideration, calling .substring(3) will result in a new sting being created, and there fore you are looping over the string twice, it is possible* for .startsWith("bar", 3) to be implemented without creating a temporary substring and to just loop through the string once. .startsWith has another tiny optimization point, it can bailout early if the first character does not match, .indexOf will need to keep checking the rest of the string.

  3. Better expressing intent, while its fairly obvious what you are doing with either of your two variants it does take the reader a little while to consider the purpose compared to a single method call that will tell you exactly what you are doing.

  4. As Phillipp points out, history can be a factor, adding new methods to an API is fairly easy, changing or removing existing methods is much harder as it will break existing code, so sometimes you may just be seing an artefact of the history of the development of the API**

* I have no idea if this is actually the case here but I'd be surprised if it does.
** Probably not in this case though.

There are few possible reasons for this sort of thing:

  1. As in JacquesB's answer it may simply be convenience for the library user to call a single method rather than two methods and keep their code more succinct.

  2. Performance may be a consideration. Calling .substring(3) will result in a new string being created, and therefore you are looping over the string twice, it is possible 1 for .startsWith("bar", 3) to be implemented without creating a temporary substring and to just loop through the string once. .startsWith has another tiny optimization point: it can bail out early if the first character does not match, .indexOf will need to keep checking the rest of the string.

  3. Expressing intent: while its fairly obvious what you are doing with either of your two variants it does take the reader a little while to consider the purpose compared to a single method call that will tell you exactly what you are doing.

  4. As Phillipp points out, history can be a factor, adding new methods to an API is fairly easy, changing or removing existing methods is much harder as it will break existing code, so sometimes you may just be seing an artefact of the history of the development of the API 2.

1 I have no idea if this is actually the case here but I'd be surprised if it does.
2 Probably not in this case though.

typo, formatting
Source Link
svick
  • 10.1k
  • 1
  • 39
  • 52

There are few possible reasons for this sort of thing:

  1. As in JacquesB's answer it may simply be convenience for the library user to call a single method rather than two methods and keep their code more succinct.

  2. Performance may be a consideration, calling .substring(3) will result in a new sting being created, and there fore you are looping over the string twice, it is possible* for .startsWith("bar", 3) to be implemented without creating a temporary substring and to just loop through the string once. .startsWith has anotheanother tiny optimization point, it can bailout early if the first character does not match, .indexOf will need to keep checking the rest of the string.

  3. Better expressing intent, while its fairly obvious what you are doing with either of your two variants it does take the reader a little while to consider the purpose compared to a single method call that will tell you exactly what you are doing.

  4. As Phillipp points out, history can be a factor, adding new methods to an API is fairly easy, changing or removing existing methods is much harder as it will break existing code, so sometimes you may just be seing an artefact of the history of the development of the API**

*I* I have no idea if this is actually the case here but I'd be surprised if it does. 
**Probably** Probably not in this case though.

There are few possible reasons for this sort of thing:

  1. As in JacquesB's answer it may simply be convenience for the library user to call a single method rather than two methods and keep their code more succinct.

  2. Performance may be a consideration, calling .substring(3) will result in a new sting being created, and there fore you are looping over the string twice, it is possible* for .startsWith("bar", 3) to be implemented without creating a temporary substring and to just loop through the string once. .startsWith has anothe tiny optimization point, it can bailout early if the first character does not match, .indexOf will need to keep checking the rest of the string.

  3. Better expressing intent, while its fairly obvious what you are doing with either of your two variants it does take the reader a little while to consider the purpose compared to a single method call that will tell you exactly what you are doing.

  4. As Phillipp points out, history can be a factor, adding new methods to an API is fairly easy, changing or removing existing methods is much harder as it will break existing code, so sometimes you may just be seing an artefact of the history of the development of the API**

*I have no idea if this is actually the case here but I'd be surprised if it does. **Probably not in this case though.

There are few possible reasons for this sort of thing:

  1. As in JacquesB's answer it may simply be convenience for the library user to call a single method rather than two methods and keep their code more succinct.

  2. Performance may be a consideration, calling .substring(3) will result in a new sting being created, and there fore you are looping over the string twice, it is possible* for .startsWith("bar", 3) to be implemented without creating a temporary substring and to just loop through the string once. .startsWith has another tiny optimization point, it can bailout early if the first character does not match, .indexOf will need to keep checking the rest of the string.

  3. Better expressing intent, while its fairly obvious what you are doing with either of your two variants it does take the reader a little while to consider the purpose compared to a single method call that will tell you exactly what you are doing.

  4. As Phillipp points out, history can be a factor, adding new methods to an API is fairly easy, changing or removing existing methods is much harder as it will break existing code, so sometimes you may just be seing an artefact of the history of the development of the API**

* I have no idea if this is actually the case here but I'd be surprised if it does. 
** Probably not in this case though.

added 1 character in body
Source Link
jk.
  • 10.3k
  • 1
  • 34
  • 43

There are few possible reasons for this sort of thing:

  1. As in JacquesB's answer it may simply be convenience for the library user to call a single method rather than two methods and keep their code more succinct.

  2. Performance may be a consideration, calling .substring(3) will result in a new sting being created, and there fore you are looping over the string twice, it is possible* for .startsWith("bar", 3) to be implemented without creating a temporary substring and to just loop through the string once. .startsWith has anothe tiny optimization point, it can bailout early if the first character does not match, .indexOf will need to keep checking the rest of the string.

  3. Better expressing intent, while its fairly obvious what you are doing with either of your two variants it does take the reader a little while to consider the purpose compared to a single method call that will tell you exactly what you are doing.

  4. As Phillipp points out, history can be a factor, adding new methods to an API is fairly easy, changing or removing existing methods is much harder as it will break existing code, so sometimes you may just be seing an artefact of the history of the development of the API**

*I have no idea if this is actually the case here but I'd be surprised if it does. **Probably not in this case though.

There are few possible reasons for this sort of thing:

  1. As in JacquesB's answer it may simply be convenience for the library user to call a single method rather than two methods and keep their code more succinct.

  2. Performance may be a consideration, calling .substring(3) will result in a new sting being created, and there fore you are looping over the string twice, it is possible* for .startsWith("bar", 3) to be implemented without creating a temporary substring and to just loop through the string once. .startsWith has anothe tiny optimization point, it can bailout early if the first character does not match, .indexOf will need to keep checking the rest of the string.

  3. Better expressing intent, while its fairly obvious what you are doing with either of your two variants it does take the reader a little while to consider the purpose compared to a single method call that will tell you exactly what you are doing.

  4. As Phillipp points out, history can be a factor, adding new methods to an API is fairly easy, changing or removing existing methods is much harder as it will break existing code so sometimes you may just be seing an artefact of the history of the development of the API**

*I have no idea if this is actually the case here but I'd be surprised if it does. **Probably not in this case though.

There are few possible reasons for this sort of thing:

  1. As in JacquesB's answer it may simply be convenience for the library user to call a single method rather than two methods and keep their code more succinct.

  2. Performance may be a consideration, calling .substring(3) will result in a new sting being created, and there fore you are looping over the string twice, it is possible* for .startsWith("bar", 3) to be implemented without creating a temporary substring and to just loop through the string once. .startsWith has anothe tiny optimization point, it can bailout early if the first character does not match, .indexOf will need to keep checking the rest of the string.

  3. Better expressing intent, while its fairly obvious what you are doing with either of your two variants it does take the reader a little while to consider the purpose compared to a single method call that will tell you exactly what you are doing.

  4. As Phillipp points out, history can be a factor, adding new methods to an API is fairly easy, changing or removing existing methods is much harder as it will break existing code, so sometimes you may just be seing an artefact of the history of the development of the API**

*I have no idea if this is actually the case here but I'd be surprised if it does. **Probably not in this case though.

added 316 characters in body
Source Link
jk.
  • 10.3k
  • 1
  • 34
  • 43
Loading
added 172 characters in body
Source Link
jk.
  • 10.3k
  • 1
  • 34
  • 43
Loading
added 1 character in body
Source Link
Arseni Mourzenko
  • 135.8k
  • 31
  • 350
  • 522
Loading
Source Link
jk.
  • 10.3k
  • 1
  • 34
  • 43
Loading