r/ProgrammerHumor • • 5d ago

Meme iThoughtIGotRidOfYou

Post image
833 Upvotes

63 comments sorted by

190

u/FurieMan 5d ago

"We have no pointers!"

Looks inside: Only pointers.

31

u/HumunculiTzu 5d ago

I've had bugs because the original developer who wrote the code didn't understand that and it was causing duplicates in the 1 path where it was passing by reference

7

u/BusEquivalent9605 5d ago

When everyone’s a pointer, no one is.

Except primitive types of course - `int, double, boolean, char,…` which are stored on the stack

1

u/golder_cz 23h ago

When 128==128, then this is also true right?

Integer a = 128; Integer b = 128; System.out.println(a == b);

89

u/IamSunka 5d ago

We can give you references, though. 

15

u/Cesalv 5d ago

with or without value?

11

u/Ok-Scheme-913 4d ago

Well, java actually only has primitive "value" types and pointers.

C++ has both pointers and references.

It's very mindfuck, but they are not the same thing, java just renamed them because people were afraid of pointers, but it lives on under the hood (NullPointerException)

2

u/danielcw189 4d ago

doesn't Java have references instead of pointers?

4

u/Ok-Scheme-913 4d ago

It's called that, but they are pointers.

Not many languages have actual references and the difference is very small.

C++ has both references and pointers, and it's the one that shows the difference best.

If you write a swap function that takes two CPP pointers or take two CPP references, the latter can be used to actually swap around the variables. The pointer one can only swap around the values stored within

In java the same

String origA = "a"; String origB = "b";

void swap(String a, String b) {
  var tmp = a;
  a = b;
  b = tmp;
}

will behave identically to cpp's pointer version. The origA itself will still point to "a" even if you pass it into the swap function.

→ More replies (6)

1

u/tasos_lydia4 1d ago

Are those references borrowed, or do they come with a lifetime?

43

u/Makonede 5d ago

21

u/Jbolt3737 5d ago

Literally an XKCD for everything

24

u/stupled 5d ago

I was expecting Rust

10

u/aeropl3b 5d ago

They were trying to think outside the Box

20

u/bwmat 5d ago

NullPointerException

4

u/SoggyCaterpillar5773 5d ago

You should try Golang.

6

u/Ok-Scheme-913 4d ago

"fun" fact: golang is not memory safe.

You can cause memory corruption by data races. Slices are fat pointers but are not written atomically. So if you change a slice from two different threads, you can observe false states like a pointer to the first slice's data but the length of a second. After which your code can read past the memory slice, a la c/c++

1

u/1Soundwave3 4d ago edited 4d ago

Isn't it bad taste to do it like this? Shouldn't channels be used to prevent data races?

In c# we have thread-safe collections, methods and so on. Those are a different set of language primitives compared to what is used more regularly. Isn't it the same in go?

1

u/Ok-Scheme-913 4d ago

There are many kinds of parallelism, channels are just one type and it doesn't solve every issue/not the most efficient.

But sure, it's not necessarily a too common thing in go, that's why it's a "fun fact".

2

u/AsaKeyholderUK 5d ago

CAmelCase variable names do not hide in key dreams. Mine is still isLoadingUserProfile from a project I finished in 2023.

3

u/mallusrgreatv2 4d ago

I misread your username as AssKeyholderUK

1

u/geeshta 4d ago

Yeah it's so safe that every refernce can be null at any point and the compiler won't force you to check

1

u/ProsodySpeaks 3d ago

Safest place to start is base64 encoding and decoding

1

u/Choice-Mango-4019 3d ago

only if there was a better VM language like c# that allowed you to do whatever you want

1

u/MrFrog2222 1d ago

Him realizing everything's a pointer in Java

1

u/Puzzleheaded-Weird66 5d ago

why no references? also why does C# have the ref keyword but requires the variable to pass be also ref (introduces some annoying syntax errors), its quite annoying since in my head I thought it was like rust's &mut

5

u/AcornAnomaly 5d ago

I see it as a behavior warning/guard.

A variable passed using ref can be changed in ways that might not be expected if the person is expecting a normal function call, including having the underlying reference replaced entirely out from under them.

1

u/Puzzleheaded-Weird66 5d ago

my main issue is now everything has to be ref

3

u/Xialian 5d ago edited 5d ago

Reference types are always passed by reference by default - passing them with the ref keyword is 99% of the time unnecessary (and likely doesn't do what you want).

Passing a value type (aka, primitives/non-"classes") with ref is basically like passing T& instead of T, so it can be useful there, but generally you don't really need that keyword much

1

u/j_torres_493 2d ago

C# `ref` lets a method reassign the caller's variable; Rust `&mut` is an exclusive borrow enforced by the compiler. Mutating a referenced C# object's fields needs no `ref`.

-1

u/drawkbox 5d ago

Everything worked out and was "safer" until Log4Shell and Java4Shell where Java was wide open for a decade.

1

u/Ok-Scheme-913 4d ago

That's like nothing compared to the number of C vulnerabilities.

1

u/mallusrgreatv2 4d ago

Literally everything has been wide open for as long as it existed, what's your point

1

u/drawkbox 4d ago

You made my point, Java is as open as anything.

-8

u/El_RoviSoft 5d ago

Imho, languages with references tends to be even more unintuitive than C++ as a first language. So I highly recommend to pick C++/Rust/Zig as a first language (and pls not C, it won’t teach you anything good for projects development).

4

u/Dragaliona 5d ago

Honestly, after trying Python and C, Java is the one that sparked the flame of programming passion on me, so I’m really fond of it. I really like it and it’s the one I chose whenever I want to program something. though I guess the teacher also does a lot for the enjoyment ([at]BroCodez on YouTube).

Imho, Java is one of the best to start mainly because of its verbosity. I think it helps the programmer keep a better understanding of the program and it makes programs easier to read and decipher without needing a bajillion comments.

On the other hand, even the course I followed wasn’t that good and it bored me to death, I think C is also an excellent choice to start, because it forces the programmer to stay closer to the machine and that experience with lower level programming makes programming in higher level languages easier and more efficient, since the programmer learns stuff it would have otherwise been abstracted away with higher level languages. Also going from Python or C++ or Java to C is much harder than the opposite, precisely because one loses the libraries/abstractions of those higher languages that one has learnt to rely on.

PS: Sorry for the rambling. It’s 4:50 for me right now and my brain ain’t braining well no more :/

2

u/El_RoviSoft 5d ago

Kinda agree with Java but references are still kinda unexplained, if you are not familiar with memory models as a concept, it can potentially blow up your legs in the future. People need at least know how shared_ptrs work (or its alternatives).

4

u/Scolmann 5d ago

Mutability may be unintuitive but there's much more unintuitive things about system languages in general that I think languages like Python, Java, or JavaScript end up being much better for beginners

C++ in particular I would never recommend to a beginner

2

u/El_RoviSoft 5d ago

At the time I found good teacher in IT school who taught C++ in a very good way. But it was 10 years ago or so.

So Ig Zig is the most right choice rn because Rust in particular teaches a lot of unusable-in-other-languages practices and architectural designs so I don’t really know which language is the beat for start now…

1

u/Ok-Scheme-913 4d ago

But like.. not even Stroustrup knows the whole c++ language, it's just so goddamn complex. Like tell me how coroutines actually work.

1

u/El_RoviSoft 4d ago

Yeah, coroutines is just a mess, recently tried to use them and it was horrible experience and several hours of reading AI explanations…

1

u/realrealvantheman 5d ago edited 5d ago

How dare you disrespect the best language ever!! Edit: /s

2

u/GreatScottGatsby 5d ago

Without the sarcasm, C is pretty great.

1

u/El_RoviSoft 5d ago

I don’t think it’s a good first language, that’s it. As a C++ dev I regularly interact with it and wrote some programs, the main issue with C that it is quite struggling and unrewarding language in some way, especially when you wanna build some big projects. There is a reason why some projects are written in C++ initially and then C interop is added.

1

u/realrealvantheman 5d ago

I mean it really depends on what your making. C is just so incredibly flexible while being one of the single most portable languages ever made and usually being pretty fast. I can see how something like c++ can be easier for larger projects tho. I do think c++ is a bit overcomplex for what it is and it isnt a first good lang due to that.

1

u/El_RoviSoft 5d ago

it’s just not as rewarding for learning, that’s my only issue in current context

I personally didn’t have feel of power and variety when I used it and this is main aspect of learning new things

sadly we don’t have middle ground nowadays which will teach something complicated with just enough power (In my school days it was Pascal + Delphi)

→ More replies (1)

1

u/someNameThisIs 5d ago

I don't see what's wrong with not teaching C as a first language, though C++ and Rust would also be good.

Zig wouldn't be, it's too immature and not enough resources for people coming in with no preexisting knowledge.

1

u/El_RoviSoft 5d ago

Imho, the inly purpose of the first language is to teach you pointers and memory model as well as build small application. If you have a good friend who knows programming/informatics well, Zig is way better choice than C or C++ but not sure about Rust tho except it’s language specific practices.

1

u/someNameThisIs 5d ago

C would be fine teaching pointers and memory models. It's how I first learnt them being self taught, as my first language was Python.

1

u/El_RoviSoft 5d ago

I just tried to find middle ground between C and some high level languages, so that’s why I told C++/Rust/Zig

1

u/Bomaruto 5d ago

Mutability where it doesn't belong is the issue, not languages with references in itself and C++ doesn't solve this.

1

u/El_RoviSoft 5d ago

Mutability by default is good but C++ has a solution at least (with const function/arg/ref specifier).

1

u/rosuav 5d ago

C++ has a solution for everything except complexity. Sometimes I have no clue what problem is being solved, but hey, there's more layers of complexity there!

0

u/Personal-Code-2496 5d ago

unsafe doesn't mean that it isn't safe. Just means that you have full control over the memory and as such need to think about how the memory you allocated gets freed later. You can do retarded memory shit in every language.

0

u/Extension_Option_122 1d ago

Come on, pointers aren't difficult. Like at all. Maybe initially grasping the concept is somewhat difficult, but if you can't understand pointers you shouldn't write software as a profession.