I'm new to C which I'm learning in university now, and I'm not sure if the following is considered in C good practices or not.
For an assignment in a simple Classification problem I wrote the following code:
int train(FILE *file, const dim dim, const int m, vector *linearSeparator)
{
size_t len = 0;
char *line;
bool stop = false;
Sample *sample = NULL;
for(int i = 0; i < m && getline(&line, &len, file) != -1 && !stop; i++)
{
if (strToSample(line, SEPARATOR, dim, sample) != EXIT_SUCCESS ||
calibrateLinearSeparator(sample, linearSeparator, dim) != EXIT_SUCCESS)
{
printError(TRAINING, "Failed processing line");
stop = true;
}
free(line);
freeSample(sample);
}
line = NULL;
return stop ? EXIT_FAILURE : EXIT_SUCCESS;
}
The functions:
strToSample
gets achar* line
and constructs aSample
object out of it, setting the*sample
pointer. It returnsEXIT_SUCCESS
if succeeded and an error code if failed.calibrateLinearSeparator
uses thesample
set by the previous function to manipulate thelinearSeparator
object (vector
- a custom struct to represent adouble[]
). This function too returnsEXIT_SUCCESS
if succeeded and an error code if failed.
Both these functions print details about their internal errors and at this level I'm printing in what sample id that internal error occurred.
My question is:
Is this way of writing the if statement a good practice? Is it readable? Should I do it differently?
It feels as if these functions are having side effects. They get pointers to set but are used in an if statement to check if they succeeded in a way that order matters.
The idea behind writing it this way is that if either of these functions fail I want to print an error and not continue the loop. If the first one fails then the second is not performed (which is what I want). In any case of success or failure I want to free the allocated memory.
Is it a good practice in C to write the for condition the way I did? Having 3 conditions in it? (checking
i
, trying to bring nextline
and!stop
?) It seems to me that this helps making the code cleaner as there are less "details" lines (of increasingi++
for example if the loop was awhile
loop ongetLine
)
// Print an error message
— what error message? This code looks incomplete and not ready for review. \$\endgroup\$i + ""
work in C? \$\endgroup\$sprintf
instead. \$\endgroup\$if statement
that way. Not sure if it is considered a bad/ok/bad practice. And a bit less about the reading and classifying the lines. \$\endgroup\$