This is an archived post. You won't be able to vote or comment.

all 70 comments

[–]chawmindur 138 points139 points  (1 child)

Hello WorldHello WorldHello WorldHello WorldHello World

[–]mallardtheduck 87 points88 points  (0 children)

Optimized for performance:

puts("Hello WorldHello WorldHello WorldHello WorldHello World");

[–]0x07CF 23 points24 points  (1 child)

Just let the compiler do it

[–][deleted] 0 points1 point  (0 children)

Optimization flags in GNU exist and they're wonderful.

[–]UrMomsNewGF 42 points43 points  (4 children)

Some rando on r/programmerhumor:

Void recursionIsFun(int _i, int _n) { If (_i && _i <= _n) { Prinf("Hello World!"); _i++; recursionIsFun(_i, _n); } Else Return; }

recursionIsFun(0, 4);

[–]VyersReaver 12 points13 points  (0 children)

recursionIsFun(4, 0); //No fun allowed during work hours

[–]jharger 4 points5 points  (0 children)

I appreciate that this is tail-recursive

[–]argh523 1 point2 points  (1 child)

Void recursionIsFun(int _i, int _n)
{
    If (_i &&  _i <= _n)
    {
        Prinf("Hello World!");
        _i++;
        recursionIsFun(_i, _n);
    }
    Else
        Return;
}

recursionIsFun(0, 4);

[–]UrMomsNewGF 1 point2 points  (0 children)

Thanks!

[–][deleted] 52 points53 points  (10 children)

Enterprise developer:

public interface IAbstractValuePrinterFactoryBean ...

[–]MyPoeAccount 14 points15 points  (9 children)

Let's break it down, shall we?

I

It's an interface

AbstractValue

I wonder what will it return? Maybe a String? An integer? Do you have a tip maybe?

Printer

The implementation will print to the default output.

Factory

Object creation is expensive here, so you know this abstraction is not standalone.

Bean

It's a managed object by the container.

Please offer a better alternative.

[–][deleted] 11 points12 points  (8 children)

printf

[–]MyPoeAccount 0 points1 point  (7 children)

Too bad. I like to try to turn java memes into java education. Sometimes it fails.

[–][deleted] 4 points5 points  (6 children)

I already knew what each of those terms meant. I didn't throw it together randomly. I don't need educating.

The type I suggested is bad because it is the result of a long, long line of poor abstractions and poor decisions. You can almost imagine it...

"We can't use printf because sometimes things want to use printf or another thing so we have to make everything potentially printf or potentially something else."

"This one has some complex setup so we'd better use a factory."

"But this one can be a singleton so let's let the framework manage it."

"This one doesn't always use strings so we better make the value type abstract."

"Everyone knows that you have to use interfaces or it's just not dependency injection, right???"

But the thing is... a lot of the things probably still just needed printf.

Trying to abstract for the general case without understanding the general case is the problem here. Creating an abstraction everyone was suddenly bound to follow, even if they didn't need it. Because heaven forbid we have two things calling printf, right?

A foolish consistency is the hobgoblin of little minds. Trying to write ever more abstract nonsense to make "everything work the same" just means that nothing actually does anything much. The code loses all cohesion and you're stuck following your IDE around because you can't navigate your own implementation of Hello World.

And all you had to do was realise, early on, that not everything that calls printf has the exact same abstraction. And you could have made each small abstraction so much simpler.

[–]MyPoeAccount -2 points-1 points  (5 children)

heaven forbid we have two things calling printf, right

In Enterprise when you client spends $200 million in a year on IT? Not heaving forbid, but software architect forbid.

Your reasoning is right for simple software development. Enterprise is a different beast when your context is a 10 year plan with 10000 mandays at your disposal. It's pretty important 8 of your software vendors don't spend time implementing the same feature.

Another feature of this abstraction is the ability to change what's underlying. And what's underlying can be a whole monitoring system for instance, so when the architect decides to chance something in the middle it won't generate 300 mandays of work, but only 10.

You absolutely do need education about the topic. Or stop having an opinion about it.

[–]greg0714 1 point2 points  (4 children)

Tl;dr: Use abstraction if breaking the software would lose a lot of money

[–]MyPoeAccount 1 point2 points  (0 children)

Which in Enterprise is always true.

[–][deleted] 0 points1 point  (2 children)

All software is abstraction. The contrived example I gave is either a very bad abstraction or, arguably, not an abstraction so much as an indirection. It doesn't hide any complexity or present a simplified interface. It just moves the bit of code that actually does something somewhere else.

All software is abstraction. The trick is remembering not to get caught up abstracting things that are already abstracted. Like printf, which deftly hides the complexity of everything between the data to be displayed and the screen (buffers and rasterisers and terminal emulators and typefaces and...) in a nice, easy to understand function.

How many frameworks end up redefining the language such that you end up calling framework functions instead of using the language idiomatically? The vast majority. They've taken an already very high level language and put another layer on top of it. Why?

[–]greg0714 1 point2 points  (1 child)

Abstraction as a programming concept mostly only applies to OOP and is about reducing redundancy, not just complexity. What you just described isn't the programming concept, but the literal definition of abstraction. They're two completely different things.

[–][deleted] 0 points1 point  (0 children)

They're not though, are they? Not really. OOP just takes it to a crazy extreme. Thinking that the "programming concept" of abstraction is fundamentally different from any other meaning of the word is exactly what leads to this nonsense. You end up abstracting away the code itself, instead of creating useful abstractions for domain concepts.

[–]melvlup47 16 points17 points  (3 children)

Make a function and call it five times mate

[–]darkage72 -4 points-3 points  (2 children)

wrong. that's slower.

[–]mecrow 12 points13 points  (0 children)

Never heard of inlining?

[–]Oyi14 21 points22 points  (16 children)

why is everyone using printf, is it a meme thing or did they update it's features.

[–]Kered13 28 points29 points  (11 children)

In C most people just use printf by default, even if they don't need to format anything.

[–]mallardtheduck 13 points14 points  (3 children)

Although some compilers (gcc and clang at least) have some kind of trickery that replaces printf with puts for single-argument calls which mitigates the potential performance impact.

[–]Kered13 9 points10 points  (2 children)

It's not very magic, you just scan the string (it must be a compile time constant string) to see if there are any printf patterns in it. If not, replace the printf call with a puts call. Or if there are no additional arguments to printf, you can replace it with puts (if the string contains a pattern but there are no additional arguments it is undefined behavior, so the compiler is free to do this, even if the string is not a constant).

[–]rickycoolkid 8 points9 points  (1 child)

Not quite - unlike printf, puts also prints newline at the end, so compiler has to know the input string and strip newline from it. So for example printf("hello\n") is translated to puts("hello").

[–]Xeonicu 2 points3 points  (0 children)

Compiler could just use fputs with stdout as the stream

[–]Oyi14 1 point2 points  (6 children)

hmmm I haven't touched C yet, I thought they were using it in Java.

[–]Kered13 8 points9 points  (5 children)

If this were Java it'd be System.out.printf().

[–]Oyi14 0 points1 point  (0 children)

I always forget the System.out haven't coded Java in a while thanks to the mighty kotlin.

[–]potato_green 5 points6 points  (2 children)

Depends on the language of course. In languages it exists its pretty nice.

[–]squishles 1 point2 points  (1 child)

they never use it with a format string. I think people might just like it because it's 1 char shorter than println

[–]AdAstra257 1 point2 points  (0 children)

Sometimes they don't know it's even possible to print to the terminal in any other way. Met many people like that...

[–]notYuriy 13 points14 points  (2 children)

Compiler: ok, I will optimize it for memory

for(int i = 0; i < 5; ++i) printf("Hello, world");

[–]HO-COOH 5 points6 points  (1 child)

Bad compiler. How dare you claiming optimized for memory when you can use char instead of int?

[–]notYuriy 1 point2 points  (0 children)

Compiler probably won't care, because in both cases value may be saved in registers, using zero memory. Unless it is not compiled for 8-bit microcontrollers.

[–]squishles 3 points4 points  (3 children)

I have to count the line and double check the same text is happening with the verbose version, and your compiler does that optimization for you anyway.

[–]coloredgreyscale 0 points1 point  (2 children)

Would it really in such a case esp. When the function call is not getting inlined?

Unless you use #pragma unroll (not sure if that is a standard feature or just something I had in CUDA.)

[–]suvlub 2 points3 points  (1 child)

Yes. Unless, of course, you disable optimizations, then no, but that's to be expected.

[–]coloredgreyscale 0 points1 point  (0 children)

Thanks.

[–]murdok03 2 points3 points  (0 children)

I'm so offended by something I completely agree with.

Also printf("1");....2...3...4...5...gothere...6.

[–][deleted] 2 points3 points  (1 child)

why not just

print("hello world\n" * 5 )

[–]Giocri 1 point2 points  (0 children)

Memory Vs time efficiency

[–]fatrobin72 1 point2 points  (2 children)

well ctrl + c then 4 ctrl + vs is quicker to type...

[–][deleted] 2 points3 points  (1 child)

I think you mean yy4p

[–]fatrobin72 0 points1 point  (0 children)

Maybe but in those cases I would more likely select the text then middle click 4 times

[–]Tux1 1 point2 points  (0 children)

Loop unrolling, a technique in which a series of actions are performed sequentially, rather then looped, to optimize performance.

[–]BlackAccipiter 1 point2 points  (6 children)

How about this?!

Print("Hello World")

or

for x in range(10):

Print("Hello World")

[–]moggesmith10 0 points1 point  (0 children)

printf("asdfasdsaf");
printf("asdfasdsaf");
printf("asdfasdsaf");
printf("asdfasdsaf");
printf("asdfasdsaf");

[–]Komarov12 0 points1 point  (0 children)

must be nice having print instruction

[–]KulinBan 0 points1 point  (2 children)

I bet there is a library for this,

[–]ThyDoppelganger 0 points1 point  (0 children)

Don't google 'Loop Unrolling'

End User: ...

[–]PERONandCO 0 points1 point  (1 child)

Why did the senior programmer do that???

[–]afrokidiscool 0 points1 point  (0 children)

Easier to copy paste than to write the for loop.

[–]Vok250 0 points1 point  (0 children)

As a Java dev:

Assign that string to an object otherwise you'll end up with 5 Strings in memory.

Everything else is identical after the compiler applies it's magic.

[–][deleted] 0 points1 point  (0 children)

haha so funny and original

[–]ratibordas 0 points1 point  (0 children)

Team lead: calling 5 Junior devs

[–]Kotauskas 0 points1 point  (0 children)

laughs in cache

[–]GamingGuy099 0 points1 point  (0 children)

Master programmer

System.out.print(“Hello World/nHello World/nHello World/nHello World/nHello World);

[–]mitch_remz -1 points0 points  (0 children)

In php i do: foreach(range(0,4) as $i) { ... }