Encapsulating logic in functions and structs - the C approach to Object Oriented techniques
Archive - Originally posted on "The Horse's Mouth" - 2016-01-30 09:19:10 - Graham EllisAlthough C is not what is described as an "Object Oriented Language", many of the good programming principles that are applied naturally in OO programming can be applied in C too.
I wrote an example that showed this yesterday - with a C struct defining what is in essence an object, and being referenced through (and only through) functions. Complex logic, then, encapsulated with the functions, good and thorough test points being available, detailed functional code to access the data being easily re-usable in other programs.
Here's my initial main (test, customer-facing) program:
int main(int argc, char ** argv) {
visit * today ;
int nVisits;
int matched;
printf("Testing / running program %s\n",*argv);
/* open and read file via a factory */
nVisits = loadVisits(&today,"ac_20160128");
printf("Today is at %d\n",today);
/* count up accesses to the home page */
matched = countVisits("/mouth/3592_",today,nVisits);
printf("Matched %d of %d\n",matched,nVisits);
return 0;
}
and it's a good starting structure - you can't see how the data is handled it's checked and secure within the strucures passed to each function. One slight issue though - my separate int count which is passed back from my loader and passed in to subsequent calls - a potential weakness. I should also check the return status of my loader ... here's modified 'main' code with the changes:
int test_03(int argc, char ** argv) {
visit * today ;
int nVisits;
int matched;
printf("Testing / running program %s\n",*argv);
/* open and read file via a factory */
if (loadVisits(&today,"ac_20160128")) {
/* count up accesses to the home page */
/* Now with a regular expression */
matched = countVisits("[[:space:]]/index\.html",today);
/* count up total accesses */
nVisits = countVisits(NULL,today);
printf("Matched %d of %d\n",matched,nVisits);
return 0;
} else {
fprintf(stderr,"Input file not avaulable\n");
return 1;
}
}
Internally, I used malloc and realloc to provide memory and I needn't bother the user with the detail ;-) ... but if you want to take a look, source code as I build up is [here], [here], [here] and [here].
You'll note that "main" has been changed to "test_03" in my final example above. That's because I've built it into a test framework so that - over time - lots of tests can be built up and all run together. So that a change to something in the code base can be checked for "ripple effect" - in other words it can hep you check that fixing one bug hasn't given rise to another. All pretty important stuff as your system grows! See [here] for the article describing the test framework.