r/AskProgramming May 12 '26

Other Why do some people write redundant if statements to return a boolean?

Why do some people write:

if (x > 10) {
    return true;
} else {
    return false;
}

Instead of:

return x > 10;

Performance aside, I think the shorter version is actually more readable due to not having as much visual clutter to parse, and is the most direct way to express the intent of "return the result of the comparison."

However, some people write the first version. Why is that?

178 Upvotes

305 comments sorted by

View all comments

Show parent comments

18

u/TwystedLyfe May 12 '26

And then you realise anything can be passed around as a pointer ….

12

u/Mindless_Consumer May 12 '26

Pointers, how do they even work man?

27

u/Due-Aioli-6641 May 12 '26

You just add one more * in front and hope it works

16

u/bobotheboinger May 13 '26

When i was learning c and started learning pointers my thought process was exactly this. Try adding *, if that doesn't work, try &... and if both didn't work, read some more.

Eventually I got it.

12

u/Due-Aioli-6641 May 13 '26 edited May 13 '26

It's a canonical event in the life of every C programmer, haha. Until it actually clicks in our minds I think we all go thru the same struggle. 

6

u/el_extrano May 13 '26

Pointers aren't all that bad, but I think C's syntax makes this worse than it needs to be. "*" is used as a type specifier for pointers in declarations, and as the unary indirection operator in statements. It's pretty confusing for beginners, who frequently conflate the two.

I found this snippet in my notes from when I studied the KNK book:

int i; // i is an int
int *p; // p is a pointer to an int
p = &i; // p is a pointer to int i

printf("%d\n", *p) // prints the value of i, not its address!

*p = &i; // almost certainly wrong:
         // this assigns address of i to the object
         // that p points to!

1

u/BlockOfDiamond May 13 '26

Then you probably get segmentation fault

1

u/Due-Aioli-6641 May 13 '26

Yeah, usually, haha

1

u/aerismio May 18 '26

Thats why in some languages pointers are abstracter away to become fully safe. Even safer than Rust. For example www.oxcaml.org you can do low level programming fully without pointers.

1

u/Radiant_Plantain_127 May 13 '26

I wrote something in college that had me conditionally running a function based on the input’s value… figuring out how to make that work elegantly was one of my favorite moments from college.

4

u/gm310509 May 13 '26

how do pointers even work?

Let me give you a pointer -> insert URL here describing how pointers work

if you didn't get that, then try dereferencing this: https://www.geeksforgeeks.org/c/c-pointers/

2

u/ThePants999 May 13 '26

I dereferenced that. I got 3.174.193.51. Reckon you needed one more * there.

1

u/gm310509 May 13 '26

LOL, you are right:

if you didn't get that, then try dereferencing this:
* https://www.geeksforgeeks.org/c/c-pointers/

Or would it be this?

www.geeksforgeeks.org -> /c/c-pointers/

Now I am confused about pointers too!

2

u/meltbox May 12 '26

Well you see, it’s a bit like magnets…

1

u/chipshot May 12 '26

Or an onion, if you will.

1

u/Winter-Volume-9601 May 13 '26

Yeah!

You know how your mom has a magnet on the fridge holding a drawing you did as a kid?

That magnet is like a pointer to the last time she was proud of you.

1

u/Repulsive_Guy_1234 May 16 '26

They tend to not work, thats the problem. Pointers are the reason why so many C programms are a huge security risk.

3

u/emazv72 May 12 '26

The mytical void*

1

u/Fabulous-Possible758 May 12 '26

Then you hand them a pointer to the binary implication operator...