Poorly named functions, part 1

on

As I’ve started learning python, one thing I appreciate is the reasonable naming of functions.  One name that has *never* made sense to me in PHP is “explode.”  You couldn’t tell it from its name, but this is not a function that blows up your scripts.  Rather, it breaks apart a string on a certain character (exploding it into an array?).  For those of you not “in the know,” a string is a bunch of characters lined up one after another.

Similarly, strings may be joined in PHP with a certain character using the function ” implode.”  What is it, a building?

In python, these functions have the much more reasonable names “split” and “join,” which do to a string exactly what they say they will.  Namely, splitting a string apart into a list and joining a list together.

To demonstrate:

In PHP:

$arrayToHoldWords = array(); // initialize empty array to hold words from string
$mystring ='This is a sentence.';  // create a string to split apart
$arrayToHoldWords = explode(' ', ); // split apart the string on spaces, store in array
$underbarString = implode('_', $arrayToHoldWords); // create a string with underbars between the "words" 

In Python: 
mystring = "This is a sentence." # create a string to split apart
array_to_hold_words = mystring.split(" ") # split apart the string on spaces, store in list
underbar_string = array_to_hold_words.join("_") # put the string back together with underbars between the words

Leave a Reply

Your email address will not be published. Required fields are marked *