this post was submitted on 05 Apr 2025
147 points (88.1% liked)

Programmer Humor

23853 readers
2705 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 2 years ago
MODERATORS
top 50 comments
sorted by: hot top controversial new old
[–] heavydust@sh.itjust.works 49 points 2 months ago (3 children)

No debate, std::endl can be a disaster on some platforms due to flushing crap all the time.

[–] mmddmm@lemm.ee 36 points 2 months ago (2 children)

It's a very C++ thing that the language developers saw the clusterfuck that is stream flushing on the kernel and decided that the right course of action was to create another fucking layer of hidden inconsistent flushing.

I hear C++ was greatly inspired by the fifth circle of hell.

[–] jdeath@lemm.ee 2 points 2 months ago

programmers manage to do stupid shit in every language. i was wondering if there was a way to stop them, and golang comes close but maybe proves it can't be done. idk!

[–] joyjoy@lemm.ee 20 points 2 months ago

Just because the box says something is flushable doesn't mean you should flush it.

load more comments (1 replies)
[–] pelya@lemmy.world 30 points 2 months ago* (last edited 2 months ago) (1 children)

printf is superior and more concise, and snprintf is practically the only C string manipulation function that is not painful to use.

Try to print a 32-bit unsigned int as hexadecimal number of exactly 8 digits, using cout. You can do std::hex and std::setw(8) and std::setfill('0') and don't forget to use std::dec afterwards, or you can just, you know, printf("%08x") like a sane person.

Just don't forget to use -Werror=format but that is the default option on many compilers today.

C++23 now includes std::print which is exactly like printf but better, so the whole argument is over.

[–] SqueakyBeaver@lemmy.blahaj.zone 3 points 2 months ago

I went digging in cppref at the format library bc I thought c++20 or c++23 added something cool.

Found std::print and was about to reply to this comment to share it bc I thought it was interesting. Then I read the last sentence.

Darn you and your predicting my every move /j

[–] Oinks@lemmy.blahaj.zone 23 points 2 months ago* (last edited 2 months ago) (1 children)

I am very sorry to remind everyone about the existence of Visual Basic, but it has:

  • VbCrLf
  • VbNewLine
  • ControlChars.CrLf
  • ControlChars.NewLine
  • Environment.NewLine
  • Chr(13) & Chr(10)

And I know what you're asking: Yes, of course all of them have subtly different behavior, and some of them only work in VB.NET and not in classic VB or VBA.

The only thing you can rely on is that "\r\n" doesn't work.

[–] MTK@lemmy.world 5 points 2 months ago (1 children)

Apology not accepted, fuck you for reminding me!

[–] jdeath@lemm.ee 4 points 2 months ago

great reminder to avoid microsoft products as much as i can

[–] ulterno@programming.dev 20 points 2 months ago* (last edited 2 months ago) (1 children)

Simple. \n when you just want a newline.
endl when you need to flush at the moment.

Useful in case you are printing a debug output right before some function that might do bed stuff to buffers.


Edit: I wrote println instead of endl somehow. Guess I need more downtime

[–] embed_me@programming.dev 5 points 2 months ago (2 children)

I only program in C. I was under the assumption that \n also flushes

[–] pelya@lemmy.world 3 points 2 months ago* (last edited 2 months ago)

It depends on whether you are printing to a terminal or to a file (and yes the terminal is also a file), and even then you can control the flushing behaviour using something like unbuffer

[–] ulterno@programming.dev 2 points 2 months ago

I remember having to fflush a couple of times.

[–] BananaOnionJuice@lemmy.dbzer0.com 17 points 2 months ago
std::cout << "\nwhy not both" << std::endl;
[–] LambdaRX@sh.itjust.works 17 points 2 months ago* (last edited 2 months ago) (1 children)

I prefer \n for 0.001% better performance

[–] Gustephan@lemmy.world 13 points 2 months ago (7 children)

Fuck endl, all my homies hate endl

[–] jdeath@lemm.ee 2 points 2 months ago

bloods 4 lyfe

load more comments (6 replies)
[–] Archr@lemmy.world 7 points 2 months ago* (last edited 2 months ago) (1 children)
[–] Croquette@sh.itjust.works 5 points 2 months ago (3 children)

I just learned that in Python, it's fucking terrible. Python is a fucking mess and my next script will be in a different language.

[–] qaz@lemmy.world 4 points 2 months ago (1 children)

Perhaps TS is not a terrible language for shell scripts after all

[–] Croquette@sh.itjust.works 2 points 2 months ago* (last edited 2 months ago)

Never tried it, but I will probably be more at home than python.

[–] jdeath@lemm.ee 4 points 2 months ago

python is a bad joke that never ends

[–] andMoonsValue@lemmy.world 3 points 2 months ago (1 children)

As a python lover, I have to ask, what don't you like about it and what languages do you generally prefer?

[–] Croquette@sh.itjust.works 1 points 2 months ago

I prefer strongly typed languages. Using bytes isn't intuitive.

Transforming certain data types into other data types is often not straightforward.

The identation is the worst though. Let me format the code however I want.

[–] diemartin@sh.itjust.works 7 points 2 months ago
fprintf(stdout, "%c", '\012');
[–] rikudou@lemmings.world 5 points 2 months ago (3 children)

In PHP it exists as well. I try to use PHP_EOL but when I'm lazy I simply do "\n".

load more comments (3 replies)
[–] ArbitraryValue@sh.itjust.works 3 points 2 months ago

C++ style text streams are bad and a dead-end design and '\n'.

[–] Sibbo@sopuli.xyz 3 points 2 months ago

Wasn't this {fmt} library merged into STL now? Does this solve this issue?

Anyways, there was also a constant that is the OS line ending without a flush, right?

[–] astrsk@fedia.io 3 points 2 months ago

Just puts(“I’m a teapot”); :)

[–] r00ty@kbin.life 3 points 2 months ago (1 children)

Maybe c# has similar. There's \r\n or \n like c++ and Environment.NewLine.

Probably it's similar in that Environment.NewLine takes into account the operating system in use and I wonder if endl in c++ does the same thing?

[–] vithigar@lemmy.ca 4 points 2 months ago

C# also has verbatim strings, in which you can just put a literal newline.

string foo = @"This string 
has a line break!";
[–] schnurrito@discuss.tchncs.de 2 points 2 months ago (1 children)

Kinda in Java, you can call System.out.println or you can call System.out.print and explicitly write the newline.

[–] uranibaba@lemmy.world 2 points 2 months ago (1 children)

I haven't looked at the code but I always assumed that println was a call to print with a new line added to the original input.
Something like this:

void print(String text) { ... }
void println(String text) { this.print(text + '\n'); }
[–] Scoopta@programming.dev 2 points 2 months ago (1 children)

That is pretty much what it does except it doesn't hardcode \n but instead uses the proper line ending for the platform it's running on.

[–] uranibaba@lemmy.world 1 points 1 month ago

I haven't worked with java for a couple of months now, currently working in Delphi, so could not remember the how else to do new line except backslash n on top of my head. :-)

[–] palordrolap@fedia.io 2 points 2 months ago (2 children)

If endl is a function call and/or macro that magically knows the right line ending for whatever ultimately stores or reads the output stream, then, ugly though it is, endl is the right thing to use.

If a language or compiler automatically "do(es) the right thing" with \n as well, then check your local style guide. Is this your code? Do what you will. Is this for your company? Better to check what's acceptable.

If you want to guarantee a Unix line ending use \012 instead. Or \cJ if your language is sufficiently warped.

[–] BatmanAoD@programming.dev 9 points 2 months ago

It's a "stream manipulator" function that not only generates a new line, it also flushes the stream.

[–] pelya@lemmy.world 4 points 2 months ago

Ah don't worry, if you do fopen(file, "w") on Windows and forget to use "wb" flag, it will automatically replace all your \n with \r\n when you do fwrite, then you will try to debug for half a day your corrupted jpeg file, which totally never happened to me because I'm an experienced C++ developer who can never make such a novice mistake.

[–] slazer2au@lemmy.world 1 points 2 months ago

As long as it doesn't end in ;

load more comments
view more: next ›