Sometimes when you find that rare function, it just makes you day. My first Eureka moment came early on in my career during one of my coop terms. I was working on some old code that had a lot of sprintf. I had to do some wrapping around it, and I couldn't figure out how to wrap the sprintf function.
sprintf(buffer "X has a value of %d and Y has a value of %X\n, x,y);
The declaration of sprintf is:
int sprintf ( char * str, const char * format, ... );
Notice the ..., as a variable parameter list. So I made my wrapper function:
MySprintf(char * buffer, const char * format, ...)
{}
But how do I call sprintf given the variable parameter list.
The answer: a function called vsprintf that takes in a variable argument list.Problem solved. More recently I was supporting some old ASP web code and the original programmers were manually encoding/decoding HTML. There was a bug in that, I tracked it down... the readily available function Server.HTMLEncode would have been better to use... Or more recently I was working some new library and I had written some code that was *logically* correct. Yet it would occasionally fail. One of the people on my team took a second look and found a more appropriate API and it worked all the time.
I know it gets darn near impossible to know every single library and API we are sometimes subjected to. Yet, as Judge Judy says... "if it doesn't make sense, it's probably not true." I try and give the original authors of APIs and libraries the benefit of the doubt. There must be a better way via the library to get things done. Sometimes you find out there is no other way and the library authors just didn't expect your situation. But in general, it is worth the search.
0 comments:
Post a Comment