intermediate/beginner question on classes

Discussion in 'C++' started by pauldepstein@att.net, Jul 24, 2007.

  1. Guest

    I am coding for a large company and I have a block of code in front of
    me that looks like the below, but with names changed for
    confidentiality reasons.

    SomeFunction(StudentClass Mary, StudentClass Jill, StudentClass Vijay)
    // In reality there are about a dozen variable names

    if (Mary.HasProblem() || Jill.HasProblem() || Vijay.HasProblem() )
    // and on and on

    { return StudentClass("A student has a problem");}

    So I'm changing it to

    if(Mary.HasProblem()) returnStudentClass("Mary has a problem.");
    if(Jill.HasProblem()) returnStudentClass("Jill has a problem.");
    if(Vijay.HasProblem()) returnStudentClass("Vijay has a problem.");

    // many more similar statements.

    I'm doing this to get more accurate diagnostic messages.

    What is a more compact way of doing the above?

    Thanks a lot for your help.

    Paul Epstein
    , Jul 24, 2007
    #1
    1. Advertising

  2. * :
    > I am coding for a large company


    Ah, good, that's probably what I should be doing. But seriously, what
    you describe simply cannot exist as code in a large company, no matter
    how incompetent they are. This is a first-year student assignment.


    > and I have a block of code in front of
    > me that looks like the below, but with names changed for
    > confidentiality reasons.
    >
    > SomeFunction(StudentClass Mary, StudentClass Jill, StudentClass Vijay)
    > // In reality there are about a dozen variable names


    This is not the real code: it won't compile without a result type.

    But anyway, change the signature to

    void foo( std::vector<Student> const& students )

    Note the name change, from "StudentClass" to "Student".

    The word "Class" is superflous; only retain that idiocy (hey, I'm in a
    bad mood, just ignore my choice of words) if it's absolutely required by
    equally idiotic coding guidelines, common in large companies and
    sometimes in colleges.


    > if (Mary.HasProblem() || Jill.HasProblem() || Vijay.HasProblem() )
    > // and on and on
    >
    > { return StudentClass("A student has a problem");}
    >
    > So I'm changing it to
    >
    > if(Mary.HasProblem()) returnStudentClass("Mary has a problem.");
    > if(Jill.HasProblem()) returnStudentClass("Jill has a problem.");
    > if(Vijay.HasProblem()) returnStudentClass("Vijay has a problem.");
    >
    > // many more similar statements.
    >
    > I'm doing this to get more accurate diagnostic messages.
    >
    > What is a more compact way of doing the above?


    Store names Student (e.g. as std::string), and a loop.

    for( size_t i = 0; i < students.size(); ++i )
    {
    if( students.at( i ).hasProblem() )
    {
    return students.at( i ).name() + " has a problem.";
    }
    }

    I'm assuming that "return StudentClass(...)" was just a transcription
    error on your part.

    If the function actually returns a student named "Mary has a problem"
    then there's more serious wrong that needs righting.

    Hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 24, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    312
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jul 24, 2007
  2. Learning Python
    Replies:
    1
    Views:
    561
    Chris Rebert
    Feb 24, 2009
  3. Robin
    Replies:
    1
    Views:
    183
    Tassilo v. Parseval
    Jan 22, 2004
  4. Robin
    Replies:
    6
    Views:
    253
    Anno Siegel
    Apr 17, 2004
  5. SarahEmmm
    Replies:
    4
    Views:
    89
    Tad McClellan
    Dec 6, 2004
Loading...

Share This Page