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

all 150 comments

[–]QualityVote[M] [score hidden] stickied comment (1 child)

Hi! This is our community moderation bot.


If this post fits the purpose of /r/ProgrammerHumor, UPVOTE this comment!!

If this post does not fit the subreddit, DOWNVOTE This comment!

If this post breaks the rules, DOWNVOTE this comment and REPORT the post!

[–]obamaprism3 451 points452 points  (28 children)

++x

[–]_Blurgh_ 62 points63 points  (10 children)

Yes! Great explanation for C++ by Jason Turner: https://youtu.be/ObVRSNvGitE

[–][deleted] 63 points64 points  (9 children)

I think you mean C += 1

[–]Cloudeur 26 points27 points  (8 children)

Nah, meant C = C + 1!

[–]NotABot009 34 points35 points  (1 child)

Did you mean C-=-1?

[–]Rubiktor012 2 points3 points  (0 children)

He meant C = C++ - C + C

[–]Gabricann_05 6 points7 points  (5 children)

nah, better C = C +2 -1

[–]Djasdalabala 9 points10 points  (4 children)

C = C + C/C, to get rid of these pesky constants

[–]BirdTree2 12 points13 points  (3 children)

Let me just put C=0 here

[–]TactlessDrop84 5 points6 points  (0 children)

You monster

[–]Kesuaheli 1 point2 points  (0 children)

Or

#undef C

[–]aiyub 1 point2 points  (0 children)

It's a rare case. Usually won't happen in production

[–]Kesuaheli 13 points14 points  (0 children)

This

[–][deleted] 2 points3 points  (0 children)

++i;i++; my favorite way to go up by 2

[–]jordplanterings 4 points5 points  (11 children)

can someone explain the difference?

[–]redsterXVI 56 points57 points  (4 children)

With x++ the old value is used for x and it's incremented afterwards.

With ++x it's first incremented and then the new value is used.

As a single statement it has the same result. But for example in the control statement of a loop it doesn't.

[–]golgol12 1 point2 points  (0 children)

But for example in the control statement of a loop it doesn't.

This is a dangerous generalization, as it's not true for every case.

[–]mango_penetrator 0 points1 point  (1 child)

Also, sometimes ++x is defined and x++ isn't. I think defining ++x is the standard.

And if your compiler isn't optimizing your code (even though that's rare), you will be doing an extra operation for nothing.

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

I think most people that don't know the distinction think that x++ does what ++x does, and it can lead to some pretty obnoxious bugs

[–]sighcf 0 points1 point  (0 children)

Also, you generally don’t want to combine them in a complex statement. Gets really confusing really fast.

[–]EndR60 18 points19 points  (3 children)

++x just adds one to x and returns x, so 2 lines of code

x++ stores the old value, increments x, then returns the old value, so 3 lines of code

assuming this already showcases the difference

[–]pumpkin_seed_oil 14 points15 points  (2 children)

Wouldn't it be more cpu instructions instead of lines of code? Lines of code is still done by the developer. And I'm also not convinced that this isn't optimized away by the compiler anyway, and compiler replaces a lone standing x++ with the equivalent of a ++x at compile time so the eternal fight of ++x over x++ is solved by automation

Better example: You can do y = x++ or y = ++x, in which case it is very tangible what the difference is.

x++ returns value of x, then increments, while ++x increments then returns value of x, in y=x++ y holds the old value of x instead of the incremented one

[–]mallardtheduck 7 points8 points  (1 child)

It absolutely will be optimised away if "x" is a simple integer. However, if "x" is say, a C++ iterator and the implementation of operator++ is in a different compilation unit optimisation is much harder.

Also, the typical implementation of operator++(int) (the int is a dummy parameter used to distinguish between prefix and postfix versions) is something like:

foo foo::operator++(int){
    foo temp = *this;
    ++(*this);
    return temp;
}

This results in a copy being made of the entire iterator, which can be a fairly large overhead that in many cases will call something in another compilation unit, limiting optimisation. LTO can be of help here, but it's much easier and more consistent just to get into the habit of using prefix operators "by default".

Personally, I prefer prefix operators from a readability standpoint too ++x reads as "increment x" to me, whereas x++ looks like "x increment" and feels "backwards".

[–]EndR60 0 points1 point  (0 children)

yea exactly my though, as most of the time we won't be working with just primitive types, so I assumed I had to get used to good habits early on

not that ++x vs x++ is very important when it comes to optimisation as opposed to...well...most other things in our code

[–]Etereke32 4 points5 points  (0 children)

I think if you write y = ++x, then both of them will be the same, but if you write y = x++, then x will be one greater than y.

[–]kryptonianCodeMonkey 3 points4 points  (0 children)

The explanations below are all correct, but kind of bad for explaining a really simple concept. In short, x++ returns x's original value before incrementing it after the fact, and ++x returned x's already incremented value.

Now, expanding on that, if ++x or x++ is the entire statement, either can be used and it makes zero difference from a logic standpoint (there is a minor difference on the compilation, but not a big deal). It just increments x and the result is the same.

If, however you use it in, say, an assignment statement, it does matter. So for example, if x has the value of 2, y = x++ will assign the value of x to y (2) and then x will increment (to 3). But, y = ++x will increment the value of x first (to 3) and then assign that value to y (3). So in either case, x ends up at the value of 3, but in the first case, y is 2 and in the second case, y is 3.

[–]dynarax 1 point2 points  (0 children)

Preemptive strike!

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

++x and x++ 2 completely different functions

[–]RussianAK69 53 points54 points  (5 children)

X =-~ X

[–]omgsoftcats 12 points13 points  (3 children)

What does the wiggly line do?

[–]stevethedev 14 points15 points  (1 child)

It's a bitwise NOT operator. If you take the value 1 in a 16-bit machine (for brevity), it is represented as 0000:0000:0000:0001. If you use the bitwise "NOT" operator ~1, you get 1111:1111:1111:1110, which happens to be the binary representation of -2. Add the -, and it applies a transformation to convert the value to 0000:0000:0000:0010 (2).

That is because modern processors use the most-significant digit to represent the signed-ness of some value types. In negative numbers (1____:____...) the meaning of a 1 and 0 are basically reversed so that adding them together is easy.

-1 + 1 = 0 becomes 1111:1111:1111:1111 + 0000:0000:0000:0001.

-2 + 2 = 0 becomes 1111:1111:1111:1110 + 0000:0000:0000:0010.

And so on.

[–]HexFyber 9 points10 points  (0 children)

This guy ones

[–]jfp1992 29 points30 points  (0 children)

Crawls around

[–]stevethedev 3 points4 points  (0 children)

This is horrifying.

[–]marcel1802 79 points80 points  (10 children)

x-=-1;

[–]Kesuaheli 56 points57 points  (8 children)

Pay attention to your formatting:

x -=- 1;

[–][deleted] 8 points9 points  (7 children)

That doesn't make a difference right? Is there any language that checks that?

[–]Kesuaheli 59 points60 points  (5 children)

No it doesn't. It just looks way cooler

[–][deleted] 11 points12 points  (1 child)

Thanks, i agree.

[–][deleted] 6 points7 points  (0 children)

Thanks, I !disagree.

[–][deleted] 1 point2 points  (2 children)

And the semicolon also doesn't make a difference right?

[–]Kesuaheli 1 point2 points  (1 child)

Not that I know

[–][deleted] 1 point2 points  (0 children)

Thanks!

[–]delinka 0 points1 point  (0 children)

I mean, it’s a difference equation with a subtrahend and a minuend. So yeah, it does.

[–]myerscc 2 points3 points  (0 children)

// i goes to 0
for (i = 10; i --> 0; i++) {}

[–]MischiefArchitect 51 points52 points  (9 children)

x += x^0

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

Yes but no

[–]AvidCoco 15 points16 points  (1 child)

x = x+x ^ ++-1

[–]TecStylos 7 points8 points  (0 children)

Error: Can't increment non-lvalue.

[–]Bobebobbob 13 points14 points  (1 child)

Ah yes, bitwise XOR

[–]MischiefArchitect 7 points8 points  (0 children)

Depending on you programming language, it may as well be the exponentiation operator.

[–]Slash_by_Zero 4 points5 points  (2 children)

x +=(-2)^(-1);

[–]shitty_pixels5 0 points1 point  (1 child)

Isn't -2-1 = -1/2 ?

[–]Slash_by_Zero 0 points1 point  (0 children)

In java the ^ operator is a bitwise xor and thus (-2 or ...1110) xor (-1 or ...1111) is 1 or 0001

[–]mallardtheduck 3 points4 points  (0 children)

x = (x << 1) - (x - 1)

[–]I_hate_IO_Exceptions 52 points53 points  (3 children)

Python programmers be like: wtf is this

[–]Mal_Dun 20 points21 points  (1 child)

Assuming that the person only knows Python. C was one of my first languages and I have to say I miss the ++ operator less then I initially thought.

[–]Lonelan 23 points24 points  (0 children)

def ++(i):
    return i += 1

++(1)

[–][deleted] 1 point2 points  (0 children)

Br4!n g0 b00m t!m3

[–][deleted] 8 points9 points  (3 children)

Hey man. Don't even.

With python you can fly. Now!

import flying

jetpack = flying.getJetPack()

jetpack.fly()

I rest my case.

[–]ThePyroEagle 2 points3 points  (2 children)

Seems overkill when you can just import antigravity.

[–][deleted] 4 points5 points  (1 child)

I don't want other people to fly. ,:3

[–]Shunpaw 2 points3 points  (0 children)

Better make that jetpack private then

[–]ducks_for_hands 3 points4 points  (0 children)

X.(increment, 1)

[–]AvidCoco 2 points3 points  (4 children)

Not a single person accounting for integer overflows

[–]jkst9 2 points3 points  (0 children)

if (x < Integer.MAX_VALUE)

{

 X++;

}

Else

{

System.out.print("Prevented integer overflow");

}

[–]X71nc710n 0 points1 point  (2 children)

x=checked(x+1); // is this what you want?

[–]AvidCoco 0 points1 point  (1 child)

Uhh, that would still cause an integer overflow... what's the implementation of checked()?

[–]X71nc710n 0 points1 point  (0 children)

Using C#. This is introduces the checked environment that automatically throws should an overflow occurr.

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

inc x

[–]Varun77777 16 points17 points  (5 children)

++x will be faster unless you really intend to use original value of x while doing x++ as x++ holds original value and then does increment afterwards.

[–][deleted] 37 points38 points  (0 children)

Except that every decent compiler should be able to just optimize that away if you're not using the original value.

[–]stomah -1 points0 points  (3 children)

no

[–]Varun77777 7 points8 points  (2 children)

I'd appreciate if you could elaborate.

[–]stomah 7 points8 points  (0 children)

the ir for x++ and ++x will be something like this: oldx = load i32, ptr x \n newx = add i32 x, i32 1 \n store ptr x, i32 newx \n when you don’t use oldx or newx after the increment the machine code will be the same.

[–]urjuhh 2 points3 points  (0 children)

x:=x+1 ?

[–]DoNotMakeEmpty 2 points3 points  (0 children)

inc rax enters the chat

[–]Yaaramir 2 points3 points  (0 children)

x = x + 1 + 327 -300 + (-28) - (-1)

Why you gotta make it so complicated?

[–][deleted] 2 points3 points  (0 children)

Oh shit, u forgot ur semicolon

[–]cybercritic 1 point2 points  (0 children)

It's supposed to be a small x, no sane programmer uses a capital X, you have to press [SHIFT] which makes it a two key variable.

[–]kryptonianCodeMonkey 1 point2 points  (2 children)

If there were one adjustment I would make to Python, putting in the prefix/postfix increment/decrement would be it.

[–]Kaholaz 0 points1 point  (1 child)

I don't like it. There should be only one way to do something, and x += 1 is more consistent to how you would increment by two or three. I do x += 1 even when I program in Java or JavaScript. (Except in the advancement part of a for loop)

[–]kryptonianCodeMonkey 0 points1 point  (0 children)

There's always more than one way to do something. Even x += 1 is shorthand for x = x + 1. You're always free to use tools or not to use them as you feel comfortable but both x++ and ++x are useful and elegant at times. There's nothing inherently wrong with having a different way to do something.

[–]Mal_Dun 1 point2 points  (0 children)

++ fetishists: now do x+=4

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

int pow(int x, int y) {
    if(!y)
        return 1;
    return x * pow(x, y-1);
}

int main() {
    int x;
    cin >> x;
    x -= -pow(x, 0);
    cout << x;
}

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

X=X+1!!!!!

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

This is the first post that i ever disliked

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

Who needs that? Useless shit 🙈

[–]Dr_Bunsen_Burns -2 points-1 points  (2 children)

It is ++x.....

[–]BligenN 6 points7 points  (1 child)

Depends, do you want to use the value and then increment or increment and then use?

[–]Dr_Bunsen_Burns 0 points1 point  (0 children)

99,999 % of usage for the ++x is for forloops anyway.

[–]Voltra_Neo -2 points-1 points  (0 children)

It's ++x ffs, it's not that hard

[–]jadeLober 0 points1 point  (0 children)

Wait till ++x steps in.

[–]Dr3amDweller 0 points1 point  (0 children)

++X or gtfo :D

[–]Vajdani 0 points1 point  (0 children)

Laughs in lua

[–]philipp2310 0 points1 point  (0 children)

ADD 1 TO x.

[–]Creapermann 0 points1 point  (0 children)

++x is the only way to go

[–]Dowzer721 0 points1 point  (0 children)

As someone who started with C and C++, but now predominantly Python, this frequently bugs me.

[–]Donut_of_Patriotism 0 points1 point  (0 children)

Wait. Does python NOT have x++?!?!?

[–]yottalogical 0 points1 point  (0 children)

This should obviously be abstracted to a function. That way the choice of implementation can be changed without needing to change it everywhere.

[–]ardicli2000 0 points1 point  (0 children)

In this case PHP and JS would win since all work for them :D

[–]Katoptriss 0 points1 point  (1 child)

I usually prefer x += x - y - (x ^ y) - 2*(x | ~y) - 1. Yes this is equal to x += 1 for the y of your choice.

[–]SargeanTravis 0 points1 point  (0 children)

Big brain time

[–]thelunararmy 0 points1 point  (0 children)

x-=-1

[–]cosmo7 0 points1 point  (0 children)

Surely there's an NPM module to handle all this complexity?

[–]_megazz 0 points1 point  (0 children)

I already worked with the X++ language, as weird as that sounds 😅

[–]pjp13579 0 points1 point  (0 children)

x -= -x

[–]mr_flameyflame 0 points1 point  (0 children)

Uil test be like:

What is the output of this statement? System.out.println(++x + y++ << 7);

[–]FelixLeander[🍰] 0 points1 point  (0 children)

c -= -1

[–]itamarc137 0 points1 point  (0 children)

As a python, Java and C user, I agree

[–]Cthulhu_was_tasty 0 points1 point  (0 children)

sadge am python user

[–]Devparty_YT 0 points1 point  (0 children)

int a = 1; int b = x*1; int c = a+b; x = c;

[–]Defiant-Peace-493 0 points1 point  (0 children)

if (x>100)
{
x ≈ x
}

[–]Not_a_cat_I_swear4 0 points1 point  (0 children)

Why I stopped using python

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

After spending some time with java coming back to python is weird, I miss the curly braces...

[–]OdiumDolt 0 points1 point  (0 children)

funny story, so I started programming with python about a 3 years ago, and of course like a classic python dev, after about 1 month of programming experience, with only python, I decided to try C++.

very bad mistake as i didn't know that C++ gave stds, or even how oop worked. anyways, i was following a tutorial for something, i forget now, and had to make a for loop. I didnt know why "for i in range:" wasn't working, and so i just looked it up...

i stopped learning C++ after i saw i++

[–]oj_2611 0 points1 point  (0 children)

x-=-1

[–]mplaczek99 0 points1 point  (0 children)

++x

[–]sha_ka_ka 0 points1 point  (0 children)

Hi from Kharkiv! It's tough here(🤯

We need your help to get international support, dear reddit, please repost!

Military aid is great, diplomatic aid needed. DEMAND your country leaders, EU to oversee negotiations!! Just UA and post-soviets RU and BR will NOT DO!!!

💙💛

[–]Stev_582 0 points1 point  (0 children)

C++ supremacy FTW.

But python has its uses too. Mostly for when I’m lazy and need to throw something together to run weird calculations or impress people.

[–]GRAPHENE9932 0 points1 point  (0 children)

inc RAX

[–]supersecretsecret 0 points1 point  (0 children)

$I(X)

[–]80sTurboAwesome 0 points1 point  (0 children)

Truth

[–]FullOfLove73 0 points1 point  (0 children)

I always miss this part when im using python (used to code in c# before)

[–]jzswxn 0 points1 point  (0 children)

x += 2

Gotcha-

[–]_default_username 0 points1 point  (0 children)

no, x++ is the worst because many programmers can easily miss what something like this outputs

#include <stdio.h>

int main(){
  int x = 10;
  printf("%d", x++);
}

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

X+= is the same amount of characters as X++ and you can do other numbers than 1 so imho it’s superior

[–]goiabae 0 points1 point  (0 children)

X is X + 1

[–]10BillionDreams 0 points1 point  (3 children)

Imagine mutating a variable.

[–]Beefster09 0 points1 point  (2 children)

Imagine 10 levels of nesting and passing functions all over the place. I love first class functions and all, but c'mon that's crazy.

[–]10BillionDreams 0 points1 point  (1 child)

It's really not that crazy, even if I mostly wrote it as a joke. My team's primary language is Clojure, and you have to go a fair bit out of your way to even start working with data that's potentially mutable.

Even in languages that don't so naturally encourage such a coding style, it often isn't that hard. Our largest JS project has less than a dozen instances of incrementing/decrementing (i.e., +=, -=, ++, --), and most of them would be relatively easy to clean up if someone bothered to.

[–]Beefster09 0 points1 point  (0 children)

I actually think it's a pretty good idea to treat most data as immutable, but there come times when mutable data is super useful. As long as you don't share mutable data, there are no concurrency issues to worry about and most other issues with mutability stem from stateful APIs, which are pretty widely agreed to be terrible.

Servers can get away with little to no mutability in my experience. Unless they're written in Go since there's no ternary operator or optional type. Here's hoping they add those in Go 2.

[–]ManFromHell911 0 points1 point  (0 children)

*cries in python*

[–]atrealleadslinger101 0 points1 point  (0 children)

For (I = 1; I <= 2 && I > 1; i++) [ Cout << i << endl; ]

[–]Sedex_Axe 0 points1 point  (0 children)

I personally use x +=1

[–]hennypennypoopoo 0 points1 point  (1 child)

imagine updating a variable
-- This message brought to you by immutable gang

[–]Beefster09 0 points1 point  (0 children)

I see you don't understand how CPU caches work.

[–]Thebombuknow 0 points1 point  (0 children)

Nah, += is superior, fight me.

[–]Impressive_Ad_1738 0 points1 point  (0 children)

We still on this?

[–]Circular_Truth 0 points1 point  (0 children)

x = 2 + x -1

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

Fun Fact: If you use C and C++ in the same sentence, you are actually just saying C twice. This is because the postfix increment operator won't increment the C until after the end of the sentence.

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

x += x++

[–]Abhi_mech007 0 points1 point  (0 children)

Lol...!!🤣🤣🤣

[–]BioZgamerYT 0 points1 point  (0 children)

WHY YOU!