The delete statement below is a code redundancy.
int *a = new int[100]
if (!doSomething1(a))
delete[] a
return
if (!doSomething2(a))
delete[] a
return
if (!doSomething3(a))
delete[] a
return
return
One alternative I came up with is:
if(doSomething1(a) || doSomething2(a) || doSomething3(a))
;
delete[] a;
return;
But it does not fit in situation where I want to add instructions specified to doSomething1(), e.g.
if(!doSomething1(a))
foo;
delete[] a;
return;
if(!doSomething2(a))
delete[] a;
return;
I was told there are at least 5 ways to reduced this kind of code redundancy. So any suggestions?
update, one more question: same question, Let's limit the domain to C only, note process logic change.
char *a = (char*)malloc(100*sizeof(char));
if(doSomething1(a))
foo;
free(a)
return;
if(doSomething2(a))
free(a)
return;
if(doSomething3(a))
free(a);
return;
free(a);
return