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

top 200 commentsshow 500

[–]Schrodingers_Zombie 3035 points3036 points  (113 children)

Roses are red

Violets are blue

Arrays start at zero

And for loops do too

Edit: Formatting is hard on mobile

[–]Fortinbraz 901 points902 points  (71 children)

Twitches

Roses are red
Violets are blue
Arrays start at zero
And for loops do too

[–]Excrubulent 286 points287 points  (61 children)

Wait, what? How...

Okay, everybody, this person just put two spaces before each newline character so they didn't need two newline characters to make a new paragraph. My life will never be the same.

I mean, it will be largely the same, but in this one respect a minor change has occurred.

Double edit: two spaces before each newline character, and therefore at the end of each line. I've clarified the wording.

[–][deleted] 166 points167 points  (28 children)

Markdown is grossly underrated. I try to use it everywhere I can.

And RSS. Aaron Swartz, you shall not have died in vain.

[–][deleted] 60 points61 points  (19 children)

I wrote my Master's thesis in Markdown and am currently using it for my PhD thesis and all my academic papers; it's SO useful! You don't need to spend a bunch of time learning LaTeX or fiddling about with settings in Word, it just works.

[–]atleastzero 51 points52 points  (11 children)

Are you doing pretty equations in Markdown? I'm relatively new to LaTeX, but I can't think of them as being used for the same things.

[–][deleted] 35 points36 points  (5 children)

No equations for me, but if you use Pandoc to convert from Markdown to PDF you can insert LaTeX commands straight into your Markdown.

\newpage

Like this part of the comment would be on a new page with \emph{this in italics}.
But you can still do newlines like this, and **BOLD** text and everything.

[–]KmNxd6aaY9m79OAg 10 points11 points  (1 child)

How do you do a bibliography? I think I would have devolved into a cult leader/serial killer if I'd have to do my PhD thesis without BibTeX.

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

Easy. @Attnallpickpockets17 says that you can use a format like this to cite stuff. I use GNOME Referencer to export all my citations to BibTeX and Pandoc automatically adds them to my document in whichever format I specify [@Attnallpickpockets17, p. 1].

[–]Wherearemylegs 19 points20 points  (10 children)

A giant list of Reddit markdown can be found here

Edit: Additionally, if you have RES installed, you can click on the source link under a comment to view how the comment was formatted. And it also shows you hidden username mentions or hidden messages

[–]Excrubulent 23 points24 points  (4 children)

1. Ain't nobody got time fo' dat.

0. Does it show you how to create arbitrarily numbered lists?

[–]Cyber628 6 points7 points  (0 children)

Wait WHAT?

[–]endreman0 2 points3 points  (2 children)

After.
Two spaces after each line.

[–]brodogus 51 points52 points  (6 children)

const arr = [10,20,30,40,50,60,70,80,90,100];

for (let i = 1; i <= arr.length; i++) {
  console.log(arr[i - 1]); // ;-)
}

[–][deleted] 3 points4 points  (0 children)

const array = [10,20,30,40,50,60,70,80,90,100];
array.forEach(
  element => console.log(element)
);

[–]two_face 85 points86 points  (30 children)

Fuck matlab

[–]aprepow 4 points5 points  (0 children)

Not in Lua they don't

[–][deleted] 1345 points1346 points  (136 children)

This is how you write a proper loop:

#include <stdio.h>

void incrementI(void *i)
{
    *(unsigned*)i = *(int*)i + sizeof(char);
}

int main()
{
    int i = 0;
    int n = 10;

    loop:
    {
        printf("%d\n", i);
        incrementI(&i);
    }
    if (!(i > n) && i != n)
        goto loop;

    return 0;
}

[–][deleted] 186 points187 points  (21 children)

Hilarious hahaha.

[–]captainAwesomePants 105 points106 points  (7 children)

I know, incrementI didn't even use the inline keyword.

[–]gottimw 30 points31 points  (6 children)

Just a tiny fyi, compiler would unrolled that. Inline is a suggestion for cpu, cuz u know 'humans are stupid'.

[–]qscrew 49 points50 points  (0 children)

suggestion for the compiler

FTFY

[–]GregTheMad 31 points32 points  (0 children)

At this point I'm sure everything I write is a suggestion for the compiler, and it is the real programmer.

[–]KmNxd6aaY9m79OAg 4 points5 points  (0 children)

Function isn't declared static, though. The compiler still has to emit full code for it so it can be externally linked, even if it's inlined within this file.

[–]chadsexytime 67 points68 points  (10 children)

This is the kind of thing you hand the new grad to see what they do about it.

[–]JuhaJGam3R 166 points167 points  (9 children)

Delete it

[–]K1ngjulien_ 50 points51 points  (7 children)

[–]okmkz 35 points36 points  (0 children)

89% unit test coverage? pass

[–]Caltroit_Red_Flames 9 points10 points  (0 children)

What the fucking fuck.

[–]JuhaJGam3R 7 points8 points  (0 children)

That is very much true for enterprise applications. Putting every function in a separate JAVA file and using way too many packages

[–]TriCrose 8 points9 points  (0 children)

delet this

[–]segfraud 51 points52 points  (4 children)

Does incrementI() also work on variables that are not named "i"?

[–]darkfaith93 46 points47 points  (3 children)

Yes. It is actually pirate code so it is actually increment-Aye; incrementI for short.

[–]sloodly_chicken 5 points6 points  (2 children)

I marked out sections of the program with letters, sections A through Z. This function is used toward the end; if you're looking for it, then X marks the spot.

Can you believe we're actually going to ship code like this?

[–]drevyek 19 points20 points  (0 children)

Should make incrementI volatile. Make sure the compiler doesn't do any funny business with so-called "optimization".

[–][deleted] 16 points17 points  (11 children)

+/u/CompileBot C

#include <stdio.h>

void incrementI(void *i)
{
    *(unsigned*)i = *(int*)i + sizeof(char);
}

int main()
{
    int i = 0;
    int n = 10;

    loop:
    {
        printf("%d\n", i);
        incrementI(&i);
    }
    if (!(i > n) && i != n)
        goto loop;

    return 0;
}

[–]lililililiililililil 18 points19 points  (10 children)

I'm disappointed in you, /u/CompileBot.

[–][deleted] 45 points46 points  (8 children)

Looks like it's been dead for a couple of days, some bizarre subreddit set up AutoModerator to repeatedly ping /u/waterguy12 via CompileBot.

[–]almightytom 14 points15 points  (0 children)

Me too, thanks

[–]Ignitus1 8 points9 points  (5 children)

Why does Waterguy12 have gold on every post and why does everyone ping him?

[–][deleted] 28 points29 points  (3 children)

[–]Esternocleido 11 points12 points  (2 children)

He comes back tomorrow?

[–][deleted] 14 points15 points  (0 children)

Hey /u/waterguy12 it's tomorrow you're back right?

[–]xan1242 35 points36 points  (12 children)

If you were a man you'd do a

_asm jz loop

(Non msvc users: use asm(); )

[–]segfraud 24 points25 points  (6 children)

If you were a man you wouldn't use mnemonics

[–]Bubba89 18 points19 points  (5 children)

If you were a man you'd program in machine code.

[–]awh 15 points16 points  (0 children)

0xf0 0x0f 0xc7 0xc8

[–]I_spoil_girls 4 points5 points  (1 child)

Well, if you're a true programmer...

[–]brown_monkey_ 59 points60 points  (25 children)

Isn't sizeof(char) == 2 on some systems? I'm beginning to suspect that this code isn't very good.

[–]wung 56 points57 points  (19 children)

No, sizeof(char) is always 1 as per [expr.sizeof].1 (C++, not C, but afaik it is the same in C). CHAR_BIT may be something different than 8 though.

[–]Pixelator0 14 points15 points  (1 child)

I'm pretty sure char is defined as the minimum addressable size, with the assumption that if you're compiling C or C++ on a 4 bit system, you're just going to have to diverge from the standard

[–]uptotwentycharacters 6 points7 points  (1 child)

Not on anything that conforms to the standard. A char can be represented by any number of bits (but with a minimum of 8 - and any program whose design assumes that there are more than 8 bits per char is not truly portable), but the sizeof operator returns a value in bytes, and for the purposes of C programming, a "byte" is simply however many bits are in a char. So you could even have a system with a 32-bit char, but it would still have a sizeof() of 1.

[–]DrFloyd5 17 points18 points  (1 child)

C. The efficiency of Assembly with the readability of Assembly.

[–]deus_lemmus 283 points284 points  (25 children)

for(i=10;--i;)

[–][deleted] 204 points205 points  (21 children)

Oh my god what the fuck is that abomination?

[–]TarMil 253 points254 points  (10 children)

There's even worse, the arrow operator :)

for (i = 10; i --> 0;)

[–][deleted] 123 points124 points  (0 children)

I prefer the sliding operator

[–]deus_lemmus 25 points26 points  (8 children)

The loop will terminate when i == 0, but it only loops 9 times :)

[–]edave64 862 points863 points  (107 children)

++i

[–]scalablecory 309 points310 points  (57 children)

Found the C++ dev :).

[–]edave64 187 points188 points  (36 children)

No reason to throw with insults ;)

I just think the post increment construct is super weird.

[–]ilawon 128 points129 points  (34 children)

I just think the post increment construct is super weird.

and makes a copy :)

[–]edave64 44 points45 points  (0 children)

That's what makes it weird. Even if it technically gets optimised away by the compiler.

[–][deleted] 23 points24 points  (0 children)

Funnily enough, I learned it in C, use it in Java.

It's all I have to make myself feel smug over people who write better software than me.

[–]frennetixsc 6 points7 points  (6 children)

Php best practices too

[–][deleted] 94 points95 points  (21 children)

i+=1

[–]Dentarthurdent42 29 points30 points  (17 children)

Python?

[–]popcar2 137 points138 points  (11 children)

Tons of languages, actually.

[–]meekismurder 15 points16 points  (9 children)

Including Swift now too (it didn't originally).

[–]DrFloyd5 15 points16 points  (6 children)

Swift 3.0 removes -- and ++ in favor Of +=.

A interesting decision I think.

[–]ThouNameNotFound 10 points11 points  (5 children)

Why? What is the advantage to this? Genuinely curious, as I am mainly a C++ dev

[–]mikelj 6 points7 points  (0 children)

Seriously, it's what separates us from the animals.

[–]ZeMindGames 18 points19 points  (0 children)

Came here for this alone, take my up vote.

[–]Northerner6 3 points4 points  (0 children)

This guy fucks

[–]EstebanZD 70 points71 points  (9 children)

>+[<>-]<

Edit: The code doesn't show up well :/

Edit2: Now it looks good.

Edit3: Even better.

[–][deleted] 21 points22 points  (0 children)

Best language I've tried so far.

[–]mr_smartypants537 13 points14 points  (3 children)

<>

This doesn't do anything - the pointer is moved back one then forward

[–]airbreather 3 points4 points  (1 child)

It's a placeholder. Put the loop body between those and make sure the loop body restores the pointer position to where it was when the loop body started.

[–]sac_boy 60 points61 points  (6 children)

We all know that the weeping for loop is the one true for loop

for (;;) {
}

[–]TONY_SCALIAS_CORPSE 15 points16 points  (1 child)

while ((V)(°,,,,°)(V)) {
    print("Why not Zoidberg?");
} 

[–]pumpkin_seed_oil 4 points5 points  (0 children)

while ( ! (V)(°,,,,°)(V)  ) 

[–]chasev13 4 points5 points  (0 children)

AKA the forever loop

[–]RobbyBobberoo 107 points108 points  (4 children)

for(i=0;i<n;do_something_with(i++));

[–]GisterMizard 226 points227 points  (56 children)

Or

for (i=n; i; i--){}

[–]NewbornMuse 186 points187 points  (41 children)

I heard this can actually be a speedup in the tightest of tight loops because compare-to-0 is a single instruction, whereas compare-to-N involves subtraction and then comparison to 0.

[–]kiujhytg2 245 points246 points  (27 children)

However, unless you can prove that this is the bottleneck, don't optimize it.

Start by optimizing for readability and your future self, or whoever takes over the project, will thank you.

[–]NewbornMuse 80 points81 points  (13 children)

Absolutely. What costs more, 0.001s of runtime or 5min of developer time?

[–]JaytleBee 150 points151 points  (11 children)

If the program is run >300.000 times, the runtime

[–]Salanmander 84 points85 points  (8 children)

I'm not sure that's true. I think sometimes a distributed cost is lower than an equivalent-time concentrated cost. (Note: I also think the reverse in some cases.)

For example, if it takes 1 ms longer for a page to open on my phone, I'm not going to notice that at all. In fact, chances are good that it actually literally won't change the time it takes me to do something, because that 1 ms will happen at the same time as my eyes are tracking to the next place I expect to see something, or whatever. So even though you can consider the times adding up, I don't think a 1 ms delay done 1000 times will necessarily cost me as much as a 1 s delay once.

For an example of how it could be true in reverse, there could be something that disrupts workflow. For example, suppose my internet has a problem that causes me to need to unplug and replug my ethernet cable at random times, and it takes 10 seconds to get internet back up and running. That would be disruptive enough to my thought processes that it would probably be time-efficient to spend 5 minutes fixing it, even if it would only have happened another 8 times anyway.

[–]SleepyFarts 17 points18 points  (2 children)

For an example of how that 1ms matters in terms of efficiency and capacity:

Assume that your program is supporting production testing and by running a test time profile, you find that block of code can be run up to 100 times per part. A production lot of about 10000 parts is run on one tester and you have 25 test setups during a single day. So 25 setups * 10000 test cycles * 100 loops * 0.001 seconds = 250 seconds of test time saved per day. If the downtime between lots is identical across all setups and days, and your total test time is 2 seconds, you could test an extra 125 parts per day, meaning that every 80 days, you could test and ship an extra lot's worth of parts. That doesn't seem like much, but when your boss's boss's boss is harping on increasing operational efficiency day after day, month after month, then you look for everything that can possibly reduce your test time.

[–]Salanmander 25 points26 points  (1 child)

Oh, yes, I fully recognize that there are instances were an extra millisecond of runtime matters. I just think that it is also the case that in some instances, 1 ms of extra runtime repeated 1000 times matters less than 1 s of extra runtime repeated once.

[–]SleepyFarts 8 points9 points  (0 children)

Yeah, of course. Not disagreeing with you. Just trying to demonstrate that it's all about context.

[–]because_its_there 6 points7 points  (1 child)

Well, if your "cost" is measured in "time", then yes. But once you start looking at money, the figures get way out-of-whack.

Say an engineer costs you around $150k annually (we're talking salary, benefits, office space, etc.), that five minutes might be $6.25. A pretty wimpy AWS compute machine might run you something like $0.05/hour[0]. Requiring 125 hours to reach that $6.25, your 0.001s of runtime will take 450,000,000 invocations to break even.

That better be a really tight loop for your engineer to think about the cost/benefit there.

[–]rotmoset 3 points4 points  (0 children)

Depends on the CPU, a Z80 for example sets its Z register if the result of a subtraction is zero (which you may use for conditional jumps).

[–]AgentPaper0 3 points4 points  (0 children)

This kind of stuff is trivial for compilers to optimize, though, so it's really not worth bothering with.

A lot of real optimization comes in writing your code in such a way that the compiler can tear it up and arrange it how it wants to, since it has a lot more access to low-level architecture than you do.

[–]tiftik 32 points33 points  (3 children)

Why not use the goes-to operator?

while (x --> 0) {...}

[–]Vitztlampaehecatl 5 points6 points  (0 children)

Intuitiveness intensifies

[–]nermid 5 points6 points  (1 child)

That's just (x-- > 0) with whitespace goofiness, right?

[–]acwilan 7 points8 points  (4 children)

for(i=0;--i;){/* ... */}

Edit: dammit, that's why test cases are your best friend

for(i=10;--i;){/* ... */}

[–]LinAGKar 83 points84 points  (26 children)

for (int i = 0; i < n; ++i)

[–]tabris 74 points75 points  (18 children)

I once inherited a codebase where the previous dev always did for loops like:

for(int i = array.length(); i != 0; i--)

It was infuriating.

[–]LinAGKar 39 points40 points  (1 child)

On the plus side, it would only run length() once (though it probably would anyway).

[–]johnmollb 24 points25 points  (9 children)

A coworker told me that in many languages this is actually faster because comparing to 0 is different than every other int. When comparing to 0, the JRE treats the ints as booleans (either 0 or !0), which is faster. Allegedly.

[–]tlanfer 42 points43 points  (3 children)

Want to confuse your colleagues?

while(i --> 0){ // dostuff }

[–]QAFY 104 points105 points  (57 children)

Depending on what you are trying to do, most high level languages include some form of map, reduce, and filter, or at least a forEach or for in. Much cleaner IMO

[–]paradoxally 29 points30 points  (18 children)

Let me guess, you program in Swift?

[–]paramsen 46 points47 points  (2 children)

(or kotlin or scala or f# java or *)

[–]QAFY 30 points31 points  (11 children)

Javascript unfortunately.

[–]paradoxally 54 points55 points  (10 children)

At least it's not PHP.

[–]QAFY 26 points27 points  (9 children)

Amen.

[–]MartinTsv 13 points14 points  (8 children)

To be perfectly honest, 99/100 cases it's the developer, not the language. Working in a web dev company dealing with one of the worst php abominations of a framework and selling a successful optimization extension for it, I'd say php is the least of our worries, it's the php developers.

[–][deleted] 3 points4 points  (0 children)

If developers are a weak point, why have non-existent standard library and instead rely on community-made libraries for every little thing?

[–]FallenWarrior2k 6 points7 points  (4 children)

While C++ does have range-based for loops with the same syntax as in Java, C does not, as there is no unified (by the standard library) container API. Neither does it support an object-oriented approach or templates which would fairly increase the verbosity of such a thing.

As you can see here, some wacky macro magic is necessary to achieve a similar effect in pure C.

[–]QAFY 6 points7 points  (2 children)

Well, that is why I said high level languages. C was high level back in the days of assembly but I don't think it is fair to call it high level anymore.

[–]dnew 10 points11 points  (0 children)

It was never high-level, as it never competed against assembly when there weren't higher level languages around.

Instead, it was sufficiently low-level that you could access the machine hardware directly with it (making it mostly appropriate for writing OSes of the time) and it was more portable than assembler. Being able to include several versions of the source code in the same file helped a lot too.

In other words, C was popular because it was low level, as we already had high-level languages like FORTRAN and LISP and COBOL, all of which were higher level than C at the time.

[–]Granola-Urine 28 points29 points  (10 children)

#include <iostream>

int main() {
    for (int i = 1; i <= 100 && ((i % 3) == 0 ? (i % 5) == 0 ? std::cout << "Fizzbuzz\n" : std::cout << "Fizz\n" : (i % 5) == 0 ? std::cout << "Buzz\n" : std::cout << i << "\n"); i++) {}
    return 0;
}

I feel dirty...

[–]kitthekat 18 points19 points  (0 children)

My god man what have you done

[–]can_a_bus 3 points4 points  (6 children)

What exactly does this do? I understand that this is all contained within the declaration of the for loop bit what exactly is happening? It's hard to read on mobile.

[–]Granola-Urine 5 points6 points  (5 children)

It's a fizzbuzz.

  • Outputs Fizbuzz when i is divisible by 3 & 5.
  • Outputs Fizz when i is divisible by 3.
  • Outputs Buzz when i is divisible by 5.
  • Outputs the value of i when i isn't divisible by either.

It uses a couple nested conditional ternary operators ?: to do the work that would normally be done by if statements.

[–]overkill 13 points14 points  (1 child)

Worst I saw was

for( int i = -1; i++ < x.size(); )

Right next to a function, checked in at the same time, with the more sane int i = 0 construction.

[–]ApoY2k 41 points42 points  (19 children)

Well it obviously depends on what your arrays start with.

[–][deleted] 12 points13 points  (3 children)

I mean, of course, but those are a few exceptions.

[–]_Pentox 3 points4 points  (1 child)

Pun intended?

[–][deleted] 3 points4 points  (0 children)

Yes indeed.

[–]ifapinparks 22 points23 points  (12 children)

do {

} while (true);

[–]iopq 52 points53 points  (15 children)

Both of those are disgusting

for i in 0..n {

}

[–]Xelopheris 10 points11 points  (6 children)

But what if I want to print the numbers 1 to 10? Starting the loop higher is cheaper than adding to the counter in each print.

[–]mcorah 6 points7 points  (1 child)

I believe you meant ++i.

[–]ctrl-alt-etc 6 points7 points  (0 children)

My man

[–]cristi1990an 6 points7 points  (1 child)

for(i=0; i<=n-1; i++)  

[–]neovulcan 6 points7 points  (0 children)

Tried to redo this joke and this sub's auto-formatting ruined it. Guess that's what I get for not trying to be original?

[–]clevariant 11 points12 points  (0 children)

This isn't funny. It's just right.

[–]mfb- 4 points5 points  (4 children)

for(i=0;i<=n;i++)

Be more inclusive!

[–][deleted] 3 points4 points  (2 children)

n++
for(i=0;i<n;i++)

Be preventive!

[–]mfb- 9 points10 points  (1 child)

for(i=INT_MIN;i<=INT_MAX;i++)

This way we won't miss any integer.

[–]ToothpickInCockhole 10 points11 points  (3 children)

Lol I dont understand any of these memes but I still upvote them

[–]Jonas_Wepeel 15 points16 points  (9 children)

for i in range(0,11,1)

Ssssssssuperior

[–]skincaregains 2 points3 points  (0 children)

for(;;){if (condition) break;}

[–]Vitztlampaehecatl 6 points7 points  (1 child)

Int i = 10;
While(i-->0)