47
md5: fe1f1a313e3049a2ec28fb5799943177
🔍
How would you improve this code?
bool my_function()
{
FILE *fp;
fopen_s(&fp, "/dev/null", "w");
do
{
if(!do_something(fp))
break;
if(!do_something2(fp))
break;
if(!do_something3(fp))
break;
if(!do_something4(fp))
break;
if(!do_something5(fp))
break;
fclose(fp);
return true;
} while(0);
fclose(fp);
return false;
}
I would start with the word Certainly!
>>105796804I miss the days when you knew some jeet wouldn't just copy paste it into ChatGPT and didn't have the time to ask it on StackOverflow. I get it I even asked it something else today as a way to autocomplete something but if I had copy pasted what it told me it would of introduced subtle bugs. Later when I asked to clarify it again just told me the complete opposite and said sorry the code is wrong of course!
I wondered and curiously it actually gave me worse "improvements". Altough it's a matter of taste I suppose.
bool my_function()
{
FILE *fp = NULL;
// Check if file opened successfully
if (fopen_s(&fp, "/dev/null", "w") != 0 || fp == NULL) {
return false;
}
// Use early returns instead of do-while(0) pattern
if (!do_something(fp)) {
fclose(fp);
return false;
}
if (!do_something2(fp)) {
fclose(fp);
return false;
}
if (!do_something3(fp)) {
fclose(fp);
return false;
}
if (!do_something4(fp)) {
fclose(fp);
return false;
}
if (!do_something5(fp)) {
fclose(fp);
return false;
}
fclose(fp);
return true;
}
// Alternative: Using a cleanup helper function
bool my_function_with_cleanup()
{
FILE *fp = NULL;
bool success = false;
if (fopen_s(&fp, "/dev/null", "w") != 0 || fp == NULL) {
return false;
}
if (do_something(fp) &&
do_something2(fp) &&
do_something3(fp) &&
do_something4(fp) &&
do_something5(fp)) {
success = true;
}
fclose(fp);
return success;
}
// Best approach: Using RAII-style cleanup with goto (common C pattern)
bool my_function_goto_cleanup()
{
FILE *fp = NULL;
bool result = false;
if (fopen_s(&fp, "/dev/null", "w") != 0 || fp == NULL) {
return false;
}
if (!do_something(fp)) goto cleanup;
if (!do_something2(fp)) goto cleanup;
if (!do_something3(fp)) goto cleanup;
if (!do_something4(fp)) goto cleanup;
if (!do_something5(fp)) goto cleanup;
result = true;
cleanup:
if (fp) fclose(fp);
return result;
}
>>105796777 (OP)What's the do-while for? This doesn't loop and it's not isolating a scope, is it just to allow break because gotos are evil?
Why not just set the return value instead of breaking, and remove the while and the duplicate fclose and return?
>>105796870>This doesn't loop and it's not isolating a scope, is it just to allow break because gotos are evil?I like this pattern for cleanup yes, it's not that I think gotos are evil but sometimes I prefer doing it this way.
bool my_function()
{
FILE *fp;
fopen_s(&fp, "/dev/null", "w");
bool ret = (
do_something(fp) &&
do_something2(fp) &&
do_something3(fp) &&
do_something4(fp) &&
do_something5(fp)
);
fclose(fp);
return ret;
}
Why would you even need a do while(0)?
>>105797072(me) Am I missing something or is OP retarded?
bool my_function() {
FILE *fp;
if (fopen_s(&fp, "/dev/null", "w")) return false;
bool ok = do_something(fp)
&& do_something2(fp)
&& do_something3(fp)
&& do_something4(fp)
&& do_something5(fp);
fclose(fp);
return ok;
}
fn my_function() -> Result<(), Error> {
let fp = File::options().write(true).open("/dev/null");
do_something1(&fp)?;
do_something2(&fp)?;
do_something3(&fp)?;
do_something4(&fp)?;
do_something5(&fp)
}
>>105797117>rust tranny has to be pedantic to open the file but can't even fclose properlySo this is the power of rust.
>>105797164dumb retard
https://doc.rust-lang.org/stable/std/ops/trait.Drop.html
>When a value is no longer needed, Rust will run a “destructor” on that value. The most common way that a value is no longer needed is when it goes out of scope.
>>105797164You are embarrassing other cniles.
int
my_function(const char *pathname)
{
FILE *fp;
int err;
fp = fopen(pathname, "w");
if(fp == NULL){
return -1;
}
do
{
err = do_something1(fp);
if(err != 0){
break;
}
err = do_something2(fp);
if(err != 0){
break;
}
err = do_something3(fp);
if(err != 0){
break;
}
err = do_something4(fp);
if(err != 0){
break;
}
err = do_something5(fp);
} while(0);
(void) fclose(fp);
return err;
}
mmm Donnie looking like a real sweetheart