this post was submitted on 02 Feb 2024
295 points (96.8% liked)

Programmer Humor

19606 readers
1140 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] db2@lemmy.world 76 points 9 months ago* (last edited 9 months ago) (2 children)

There is an other.

int * p;

[–] kambusha@feddit.ch 95 points 9 months ago (2 children)
[–] otter@lemmy.ca 42 points 9 months ago (1 children)
[–] far_university1990@feddit.de 16 points 9 months ago

i am in this picture and i do like it

[–] Deceptichum@kbin.social 4 points 9 months ago

Male otters kidnap children otters and hold them for ransom until the mum gives them food.

[–] thomasloven@lemmy.world 7 points 9 months ago

I’ve seen that in style guides ”because it should piss of everyone equally”.

[–] GissaMittJobb@lemmy.ml 68 points 9 months ago (2 children)

Having an asterisk both be the type indicator and the dereference operator is one of the great programming language design blunders of our time, along with allowing nulls for any type in so many languages.

[–] darkpanda@lemmy.ca 20 points 9 months ago (3 children)

I also sometimes wish that the syntax in if statements was inverted, where () was optional and {} was required.

[–] CodexArcanum@lemmy.world 12 points 9 months ago

Rust makes this choice and it is way better.

[–] victorz@lemmy.world 5 points 9 months ago
[–] clay_pidgin@sh.itjust.works 3 points 9 months ago* (last edited 9 months ago) (2 children)

Can you give me an example? I'm not sure I follow. Might be language specific?

[–] noli@programming.dev 9 points 9 months ago (1 children)

if(condition) statement; Is valid in typical C-style syntax.

if condition { ... }

Is invalid in typical C-style syntax

[–] clay_pidgin@sh.itjust.works 3 points 9 months ago

Gotcha, thanks.

[–] darkpanda@lemmy.ca 6 points 9 months ago* (last edited 9 months ago) (1 children)

The code in the image is C or C++ or similar. In those languages and languages derived from them, curly braces are optional but the parentheses are required. It should be the other way around to avoid logic errors like this:

if (some expression)
  doSomething()
else if (some other expression)
  printf(“some debugging code that’s only here temporarily”);
  doSomethingElse();

Based on the indentation you’d think that doSomethingElse was only meant to run if the else if condition was true, but because of the lack of braces and the printf it actually happens regardless of either of the if conditions. This can sometimes lead to logic errors and it doesn’t hold up to a principle of durability under edit — that is, inserting some code into the if statement changes the outcome entirely because it changes the code path entirely, so the code is in a sense fragile to edits. If the curly braces were required instead of optional, this wouldn’t happen.

I have all of my linters set up to flag a lack of curly braces in these languages as an error because of this. It’s a topic that sometimes causes some debate, ‘cause some people will vociferously defend their right to not have the braces there for one liners and more compact code, but I have found that in general having them be required consistently has led to fewer issues than having arguments about their absence, but to each their own. I know many big projects that have the opposite stance or have other guidelines, but I just make ‘em required on my own projects or projects that I’m in charge of and be done with it.

[–] clay_pidgin@sh.itjust.works 2 points 9 months ago

That makes perfect sense, thank you.

[–] Blackmist@feddit.uk 6 points 9 months ago (1 children)

Having assignments return a value is right up there as well.

[–] GissaMittJobb@lemmy.ml 8 points 9 months ago (1 children)

Because of the possibility of accidentally performing an assignment in a conditional expression?

If yes, I agree that it's not great.

[–] Blackmist@feddit.uk 3 points 9 months ago

Yeah, exactly that.

[–] blackstrat@lemmy.fwgx.uk 58 points 9 months ago (8 children)

The fact it's a pointer is part of the type, not part of the variable name. So int* p is the way.

[–] sweng@programming.dev 87 points 9 months ago* (last edited 9 months ago) (6 children)

You would think so, but int* a, b is actually eqivalent to int* a; int b, so the asterisk actually does go with the name. Writing int* a, *b is inconsistent, so int *a, *b is the way to go.

[–] newH0pe@feddit.de 56 points 9 months ago

Yeah, and I'd say that's a design flaw of the language as it is unintuitive behaviour.

[–] Slotos@feddit.nl 38 points 9 months ago (1 children)

When people say “pointers are hard”, they mean “I have no idea where the star goes and now an ampersand is also implicated”.

[–] T156@lemmy.world 24 points 9 months ago (1 children)

That's the part where you give up and randomly shove/unshove symbols in until the code works.

[–] CanadaPlus@futurology.today 5 points 9 months ago

I've definitely never been guilty of this. /s

[–] blackstrat@lemmy.fwgx.uk 25 points 9 months ago

While technically true, that's also one of the worst 'features' of the language and I personally consider it a bug in the language. Use two lines and make it clear and correct.

[–] Gladaed@feddit.de 15 points 9 months ago

Don't declare more than 1 pointer per line. This resolves that, badly.

[–] CanadaPlus@futurology.today 8 points 9 months ago

Alright, I'll never, ever write something this way now. Good to know.

[–] ono@lemmy.ca 4 points 9 months ago

This is true in C, but not in D.

[–] shami1kemi1@sopuli.xyz 16 points 9 months ago

Then again, at least in C, the mantra is "declaration follows usage". Surely you don't write pointer dereferences as * ptr? Most likely not, you most likely write it as *ptr. The idea behind the int *ptr; syntax is basically that when you do *ptr, you get an int.

And with this idea, stuff like function pointers (int (*f)(void)), arrays of pointers (int *a[10]) versus pointers of arrays (int (*a)[10]) etc. start making sense. It's certainly not the best way to design the syntax, and I'm as much a fan of the Pascal-styled "type follows the identifier" syntax (e.g. let x: number;) as anyone, but the C way does have a rhyme and a reason for the way it is.

[–] marcos@lemmy.world 13 points 9 months ago* (last edited 9 months ago) (1 children)
int* i, j

The C syntax is just messed up.

load more comments (1 replies)
[–] Traister101 6 points 9 months ago* (last edited 9 months ago) (3 children)

It's part of the type yet it's also a unique identifier. That's the whole thing with east or west const. const int * is a ~~immutable~~ mutable pointer that points to ~~mutable~~ immutable memory. int *const is a ~~mutable~~ immutable pointer that points to ~~immutable~~ memory. int const * is the same type as the first example, a ~~immutable~~ mutable pointer that points to ~~mutable~~ immutable memory.

Same stuff applies to references which makes it easier to think of the variable owning the * or & as if you want that pointer or reference to be const it has to go after.

Edit:I am a moron who managed to get it exactly backwards :|

[–] Valmond@lemmy.mindoki.com 7 points 9 months ago (2 children)

Found the guy that can probably do function pointers!

[–] SpaceNoodle@lemmy.world 4 points 9 months ago (2 children)

I wrote a couple unholy lines of C++ the other day using the ternary conditional operator to select a class member function to be called with a fixed argument.

I think my teammates were too scared to call me out on it.

load more comments (2 replies)
[–] Traister101 3 points 9 months ago

Lol yeah. I don't even really write C++ but I sure as shit know a bunch of syntax and junk haha

[–] YaBoyMax@programming.dev 3 points 9 months ago (1 children)

I think you've got it backwards. I learned to read pointer decls from right-to-left, so const int * is a (mutable) pointer to an int which is const while int *const is a const pointer to a (mutable) int.

[–] Traister101 2 points 9 months ago

Fuck me man that's what I get for writing that just before bed

load more comments (1 replies)
[–] Scoopta@programming.dev 4 points 9 months ago (1 children)

I do this in my code because it looks better and makes more sense...until I decide to declare 2 vars on one line and then I use the very cursed int* a, *b

[–] blackstrat@lemmy.fwgx.uk 4 points 9 months ago

I just wouldn't do that.

[–] owsei@lemmy.world 3 points 9 months ago

tbh I always think about it as 'p' is a pointer to int

therefore *p is an int

therefore I should call it int *p;

however, of course, you should use what your team prefers. Having good yet inconsistent style is worst than mid consistent style.

[–] SpaceNoodle@lemmy.world 2 points 9 months ago

And yet the default clang formatter gets it wrong.

load more comments (1 replies)
[–] fl42v@lemmy.ml 17 points 9 months ago

std::shared_ptr<int> p;

[–] dbilitated@aussie.zone 15 points 9 months ago (2 children)

I'm just a c# dev wishing to fuck we had something visual to indicate reference types so my coworkers could stop misusing them

[–] victorz@lemmy.world 14 points 9 months ago
[–] catconnoisseur@lemmy.ml 4 points 9 months ago

no int * ptr fans here?

load more comments
view more: next ›