Do you feel bad because of the Python docs?
rosuav at gmail.com
Tue Feb 26 15:26:47 CET 2013
On Wed, Feb 27, 2013 at 12:56 AM, Roy Smith <roy at panix.com> wrote:
> When people ask PHP questions, the questions tend to be phrased as "what
> do I type to get X", and the answers come back that way too. The forums
> are full of, "I had the same problem. Somebody told me to do this. I
> don't really understand it, but it worked for me and maybe it'll work
> for you too".
A problem that's majorly exacerbated by the myriad ways of doing some
things, with some of those ways deprecated and others theoretically
plausible but hopelessly impractical.
Here's an actual example that came up today at work. Suppose you have
a user-provided string that's supposed to contain a URL, and you need
to ensure that it doesn't have a trailing slash, so you can later add
"/foo" or "/bar". (Or alternatively, ensure that it DOES have a
trailing slash. Either works.) Start the timer, go find out how to do
it. Assume you are broadly familiar with PHP, and know how to do the
basics of string handling, and are competent at searching the web.
I'll wait for you to come back.
... Okay, some of you are back now. Just giving the stragglers time to
finish losing their marbles...
Alright. Here's what I found in a recreation of today's search.
Google search: php last character of string
-- okay, so I can use substr, but not string indexing, to find out
what the last character is
-- "Returns the extracted part of string; or FALSE on failure, or an
empty string." What kind of failures result in FALSE, and what kind in
an empty string?
Comment on question: "There are more than 10 ways of doing this!"
The accepted answer, fortunately, is a good one. Use rtrim. Okay.
That's nice. But look at the other answers: one mentions substr (as
above, that's half the question); one suggests the unwieldy form of
indexing from the back (with an explicit strlen); one answers
altogether the wrong question (suggesting the use of basename); and
one... suggests a regular expression. Now you have two problems.
And just to add to the pile of ways this could be done, the first
response in this thread
suggests *reversing the string* and indexing from the front.
Maybe you tried different search terms and got better results, I don't
know. This wasn't the only search I did in trying to find the best way
to do this, but it was the one with the most amusing results.
The original rant specifically excluded sites like Stack Overflow as
valid sources, which in a way is fair enough. But it wasn't from
php.net that I got that information.
Okay. Now I'll try it again, for Python.
Google search: python last character of string
Besides numbers, Python can also manipulate strings, which can be
expressed ... word[-1] # The last character 'A' >>> word[-2] # The
last-but-one character 'p' ...
The use of code with directly connected comments means that I can read
this part of the solution right there, without even clicking the link.
Search Engine Optimization FTW.
Searching for the second part, by adding the word 'remove' to the
search, brings up more third-party sites - for PHP:
and for Python:
In both cases, answering the question, but not from the language's own
site. Forcing the matter with a site: search brings up poor results
for both languages. Python gives me:
but nothing about slicing. PHP: Take your pick of functions, and I
hope you already know what they do, because the snippets don't help
substr, rtrim, chop (which, once you go to the page, reveals itself to
be an alias for rtrim), strrpos, substr_replace, strstr
I'd have to say that, in the shoot-out, both languages died on their
own merits, but stood on the merits of third-party support. Fifteen
years ago, I would have called that a critical failure; in the 90s, I
would search for information only from official sources. But these
days, forum archives and blogs are some of the best way to find what
you need - granted, Sturgeon's Law applies, but frankly that applies
to a lot of documentation too (and Google's fairly good at helping you
find the 10%).
More information about the Python-list