Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Wrote this function for a Stack Overflow answer Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).

The problem the code attempts to solve is finding a case-insensitive string in an array.

Soo... anything that can be improved here?

Should I handle null and undefined?

Does my documentation look helpful from a IDE usage perspective?

/**
 * Searches through an array for a case-insensitive string match.
 * Attempts to imitate Array.indexOf where it can.
 * @param arr The array to search through
 * @param searchingFor The string to search for
 * @param fromIndex Optional, an index to start searching at.
 * @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
 var lowercaseSearchString:String = searchingFor.toLowerCase();
 var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
 for(var index:uint = fromIndex; index < arrayLength; index++) {
 var element:* = arr[index];
 if(element is String && element.toLowerCase() == lowercaseSearchString) {
 return index;
 }
 }
 return -1;//It wasn't found in the array.
}

Wrote this function for a Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).

The problem the code attempts to solve is finding a case-insensitive string in an array.

Soo... anything that can be improved here?

Should I handle null and undefined?

Does my documentation look helpful from a IDE usage perspective?

/**
 * Searches through an array for a case-insensitive string match.
 * Attempts to imitate Array.indexOf where it can.
 * @param arr The array to search through
 * @param searchingFor The string to search for
 * @param fromIndex Optional, an index to start searching at.
 * @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
 var lowercaseSearchString:String = searchingFor.toLowerCase();
 var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
 for(var index:uint = fromIndex; index < arrayLength; index++) {
 var element:* = arr[index];
 if(element is String && element.toLowerCase() == lowercaseSearchString) {
 return index;
 }
 }
 return -1;//It wasn't found in the array.
}

Wrote this function for a Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).

The problem the code attempts to solve is finding a case-insensitive string in an array.

Soo... anything that can be improved here?

Should I handle null and undefined?

Does my documentation look helpful from a IDE usage perspective?

/**
 * Searches through an array for a case-insensitive string match.
 * Attempts to imitate Array.indexOf where it can.
 * @param arr The array to search through
 * @param searchingFor The string to search for
 * @param fromIndex Optional, an index to start searching at.
 * @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
 var lowercaseSearchString:String = searchingFor.toLowerCase();
 var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
 for(var index:uint = fromIndex; index < arrayLength; index++) {
 var element:* = arr[index];
 if(element is String && element.toLowerCase() == lowercaseSearchString) {
 return index;
 }
 }
 return -1;//It wasn't found in the array.
}
Notice removed Draw attention by Pimgd
Bounty Ended with Simon Forsberg's answer chosen by Pimgd
Tweeted twitter.com/#!/StackCodeReview/status/525641761104752640
edited tags
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144

Wrote this function for a Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).

The problem the code attempts to solve is finding a case-insensitive string in an array.

Soo... anything that can be improved here?

Should I handle null and undefined?

Does my documentation look helpful from a IDE usage perspective?

/**
 * Searches through an array for a case-insensitive string match.
 * Attempts to imitate Array.indexOf where it can.
 * @param arr The array to search through
 * @param searchingFor The string to search for
 * @param fromIndex Optional, an index to start searching at.
 * @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
 var lowercaseSearchString:String = searchingFor.toLowerCase();
 var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
 for(var index:uint = fromIndex; index < arrayLength; index++) {
 var element:* = arr[index];
 if(element is String && element.toLowerCase() == lowercaseSearchString) {
 return index;
 }
 }
 return -1;//It wasn't found in the array.
}

Wrote this function for a Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).

Soo... anything that can be improved here?

Should I handle null and undefined?

Does my documentation look helpful from a IDE usage perspective?

/**
 * Searches through an array for a case-insensitive string match.
 * Attempts to imitate Array.indexOf where it can.
 * @param arr The array to search through
 * @param searchingFor The string to search for
 * @param fromIndex Optional, an index to start searching at.
 * @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
 var lowercaseSearchString:String = searchingFor.toLowerCase();
 var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
 for(var index:uint = fromIndex; index < arrayLength; index++) {
 var element:* = arr[index];
 if(element is String && element.toLowerCase() == lowercaseSearchString) {
 return index;
 }
 }
 return -1;//It wasn't found in the array.
}

Wrote this function for a Stack Overflow answer. It started off pretty bad but in the end I polished it a bit and now I have something I think to be pretty clean. Actually, when I look at the code, this is what I usually write when I'm programming AS3 (minus the documentation, I'm a lazy bastard, although that's slowly changing too).

The problem the code attempts to solve is finding a case-insensitive string in an array.

Soo... anything that can be improved here?

Should I handle null and undefined?

Does my documentation look helpful from a IDE usage perspective?

/**
 * Searches through an array for a case-insensitive string match.
 * Attempts to imitate Array.indexOf where it can.
 * @param arr The array to search through
 * @param searchingFor The string to search for
 * @param fromIndex Optional, an index to start searching at.
 * @returns The index of the array that a match was found at (zero-indexed), or -1 if no match was found.
*/
public static function indexOfCaseInsensitiveString(arr:Array, searchingFor:String, fromIndex:uint = 0):int {
 var lowercaseSearchString:String = searchingFor.toLowerCase();
 var arrayLength:uint = arr.length;//Retrieving array length is expensive, optimization
 for(var index:uint = fromIndex; index < arrayLength; index++) {
 var element:* = arr[index];
 if(element is String && element.toLowerCase() == lowercaseSearchString) {
 return index;
 }
 }
 return -1;//It wasn't found in the array.
}
edited tags
Link
RubberDuck
  • 31.1k
  • 6
  • 73
  • 176
Notice added Draw attention by Pimgd
Bounty Started worth 50 reputation by Pimgd
Source Link
Pimgd
  • 22.5k
  • 5
  • 68
  • 144
Loading
default

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