I'm using Linux, and don't want to install Chinese input method, so I'm going to write this diary in English.
Server days ago, I was asked to reduce code redundancy (see below) in a job interview.
char *a = (char*)malloc(100*sizeof(char));
if(doSomething1(a))
free(a);
return;
if(doSomething2(a))
free(a);
return;
if(doSomething3(a))
free(a);
return;
free(a);
return
Out of nervous, I just came up with this:
if (doSomething1(a) || doSomething2(a) || doSomething3(a))
;
free(a);
return;
However, I was told this doesn't fit into situation where additional operation specified to doSomething1() is required, e.g.
if(doSomething1(a))
foo;
free(a);
return;
if(doSomething2(a))
free(a);
return;
So I post a
question on stackoverflow, and got nice answers specified to c++. Just now I came up with this:
while(True)
if (doSomething1(a))
foo;
break;
if (doSomething2(a)) break;
if (doSomething3(a)) break;
break;
free(a);
return;
However, I was told in the interview that there are at least five ways for this. Do you have an idea?