By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,384 Members | 845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,384 IT Pros & Developers. It's quick & easy.

intermediate/beginner question on classes

P: n/a
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 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 2007-07-24 10:36, pa**********@att.net 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
Jul 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.