in reply to String Length

<rant volume="low"> Folks, this node is a good example of how we often take a vague statement of requirements and jump right in to solutions before we're clear on the problem that we're trying to solve. (I'm guilty here, too.)

Take a moment to look at the problem statement we were given to work with. Does it warrant the solutions that have been proposed, or might it be more appropriate to get clarifications before proceeding to code.

Here's what we were given to work with:

  1. Find the length of a string.
  2. Remove the last two characters from the string.
  3. Save the remaining character to a variable.
We can drop item 1) as being a non-requirement. It's really one step towards a possible implementation of 2)

Next we're given an example:

If I had the following strings: "1ST", "21ST" and "101ST", I would like to remove the "ST" and retain the numbers.
At this point we should stand back and ask:
  1. Will all input be leading digits plus a two character suffix?
  2. Will the suffix always be "ST"? What about "st"? What about "TH"?
  3. What should we do with input that isn't leading digits plus a two character suffix?
Only after we get answers to theses are we really in a position to start suggesting implementations. To do otherwise risks having the problem come back to us n more times, each time revealing a new requirement that our solution didn't satisfy.

</rant>

Replies are listed 'Best First'.
Re: (dws - rant)Re: String Length
by rchiav (Deacon) on Apr 05, 2001 at 05:24 UTC
    If I was in possesion of enough chits to vote on posts, I'd definately ++ this. But seeing as though I haven't earned my voting badge yet, I'll just have to resort to saying that you're %100 correct. -Rich
Re: (dws - rant)Re: String Length
by Boldra (Curate) on Apr 05, 2001 at 17:46 UTC
    Actually, I'm a huge fan of diving in with code before the problem has been specified. I quite enjoy deliberately misinterpreting the newbies question and posting an irrelevant chunk of code.
    $processing->further(s/T"$//);

    I also think this is a great way for the newbies to learn to clarify their questions!

    ep: the lesson here is that at least half of your job is understanding the project's requirements. If the Monks demonstrate how they get nowhere by misunderstanding your requirements, then maybe you'll learn that you will get nowhere until you understand your client's requirements.

Re: (dws - rant)Re: String Length
by ep (Initiate) on Apr 05, 2001 at 17:38 UTC
    1) No,we cannot assume that the input will be leading digits plus a two char. suff. ... we can only hope! 2) Hopefully, the suffix will be "ST" or "TH" or "RD", but we cant be assured of case or it meet the criteria above. 3)Input that is not leading digits plus a two character suffix is junk and therefore not needed! Hope this helps- ep
      What should happen to input that does not have leading digits plus a two character suffix? Will have you already screened such input out? If not, is stripping off the last two characters the right thing to do, or should something else happen?

      The devil is in the details.