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. On 2007-07-24 10:36, wrote:
    > 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?


    Probably not they way you are doing, what if more than one student have
    a problem? Personally I find it quite dubious to return a StudentClass
    object if a student have a problem, perhaps throw an exception instead?

    The place where you should change the code is not in SomeFunction(), but
    in the code that calls, it. If SomeFunction() returns with the status
    that a student has problems you should then loop through the students
    and check their status and print the names of those with problems.

    By the way, it might be better to pass a collection of students (such as
    vector or list) instead of passing each student.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, 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:
    332
    Alf P. Steinbach
    Jul 24, 2007
  2. Learning Python
    Replies:
    1
    Views:
    572
    Chris Rebert
    Feb 24, 2009
  3. Robin
    Replies:
    1
    Views:
    190
    Tassilo v. Parseval
    Jan 22, 2004
  4. Robin
    Replies:
    6
    Views:
    278
    Anno Siegel
    Apr 17, 2004
  5. SarahEmmm
    Replies:
    4
    Views:
    101
    Tad McClellan
    Dec 6, 2004
Loading...

Share This Page