intermediate/beginner question on classes

P

pauldepstein

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
 
A

Alf P. Steinbach

* (e-mail address removed):
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top