Thread 105796777 - /g/ [Archived: 658 hours ago]

Anonymous
7/4/2025, 9:31:27 AM No.105796777
47
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;
}
Replies: >>105796870
Anonymous
7/4/2025, 9:38:05 AM No.105796804
I would start with the word Certainly!
Replies: >>105796838
Anonymous
7/4/2025, 9:45:13 AM No.105796838
>>105796804
I 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!
Anonymous
7/4/2025, 9:49:26 AM No.105796865
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;
}
Anonymous
7/4/2025, 9:49:42 AM No.105796870
>>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?
Replies: >>105796876
Anonymous
7/4/2025, 9:51:14 AM No.105796876
>>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.
Anonymous
7/4/2025, 10:17:33 AM No.105797072
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)?
Replies: >>105797084
Anonymous
7/4/2025, 10:20:03 AM No.105797084
>>105797072
(me) Am I missing something or is OP retarded?
Anonymous
7/4/2025, 10:24:36 AM No.105797114
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;
}
Replies: >>105797132
Anonymous
7/4/2025, 10:24:40 AM No.105797117
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)
}
Replies: >>105797164
Anonymous
7/4/2025, 10:26:27 AM No.105797132
>>105797114
Rate this
Anonymous
7/4/2025, 10:32:21 AM No.105797164
>>105797117
>rust tranny has to be pedantic to open the file but can't even fclose properly
So this is the power of rust.
Replies: >>105797171 >>105797186
Anonymous
7/4/2025, 10:33:33 AM No.105797171
>>105797164
dumb 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.
Anonymous
7/4/2025, 10:35:40 AM No.105797186
>>105797164
You are embarrassing other cniles.
Anonymous
7/4/2025, 11:44:33 AM No.105797565
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;
}
Anonymous
7/4/2025, 12:02:22 PM No.105797671
mmm Donnie looking like a real sweetheart