#I still don t see what the difference in
1 messages ยท Page 1 of 1 (latest)
ok, you see, simply from a code reading perspective:
for x++ you read the variable x -> what could the next step be? i could use the variable, call a method on it, access another property.... -> oh no, i'm just incrementing.
for ++x you read now i'm incrementing something....do i need to know what, or can i already move on to the next line? -> (read next line OR) -> ah, i'm incrementing x
with x++ you also have the case: oh i guess i'm incrementing x, but why did the programmer use postincrement istead of preincrement? did he forget to assign the previous value to something? or is it just his style?
I understand it from a semantic point of view (what does the operation actually do), I guess the reason I just find it strange is that x++ on a single line is equal is equal to x += 1, where the operator is also "behind" the variable.
understandable, but it's a whole different syntax. even though they look kind of similar, these are 2 very different things language wise
Yep, I agree with that, I guess it's just the way I was taught. I'll try switching to preincrementing though and see how that feels ๐
I'm also not a fan of a single line being x++ in the first place (and some languages don't even support it)
preincrement increases the reading flow of code the same way lefthand explicit typing does. when you read new code, you care about most WHAT is happening or what the result was. not HOW i got it. if you use var, you will immediately miss the most important information. if you write the explicit type, you immediately know the most important information and can move on to the next line
Makes sense! I guess I use var nowadays since iirc when they introduced var the new() "shortcut" wasn't part of the language (although I might have just missed that)
(And I was always too lazy to type the variable type twice)
yes. but now new() is available, which makes var just a pain in the ass legacy (mis-)feature
Yeah maybe I'll try that out. I'm guessing rider should be able to convert my definitions from var ... = new type(), to type ... = new()
it's usually a 2 step process. -> convert explicit type to explicit -> reduce righthand type creation to new()
Thanks for the talk / insights, I appreciate it!
In the end, it's still all just 'style', but i try to reason with people about these things, since they easily improve workflow / productivity, especially in large and maintained projects, and most people just 'learned' and didn't spend a second thinking about why they write what they write.
I totally agree with you that it improves showing the intention of what you are doing (in the case of type ... = new() and ++i) and makes it easier to read, but it feels like an uphill battle, since like I mentioned I've rarely seen it this way (atleast the ++i in loops)
This is riders default after all
And the default in all IDEs I've used iirc
(Which of course doesn't mean it's good or correct ๐ )
it can seem like an uphill battle, but it is only that if people like me don't make others aware of the better solutions. and while ide's ^^ might default to that, these 'templates' can easily be corrected and overwritten with an .editorconfig, which any good project should have anyway
pre/post incrementing makes no difference in modern compilers. the discussion is mostly shifted because what it does change is the return value. though if you write int a = ++b; and then use a afterwards I'll do something to you. that stuff is obfuscating code and creates bugs in teams. that's not about coding style. make it clear when and what you increment. not everything has to be written in 1 line. it will be the same instructions in assembly anyway.
Yeah definitely, I just see i++ seemingly always when I check any resources online
It's not about performance, it's about intention
And reading flow I guess
(i.e. code style)
your for loop will read the same with ++i or i++. 99.9% have agreed to i++
That's why I feel it's an interesting talk, since just because it's used more extensively doesn't mean there isn't also merit to other options
your example is misleading. first of all, int a = ++b is a most rare occurence, and second, what does it do? if you change a, you won't change b. you will simply have a simple bug where your seemingly expected number is 1 off of b, which can also happen equally as often when you write in a = b++;
because it made no difference we picked the most natural to us. reading left to right.
also more in line to how we write out math formulas
ok, i get your point that it way not immediately improve code flow in a for loop, but in a for loop it is especially important for consistency now. if you preincrement everything in your project but not in your for loops, that's just inconsistency hell
but the benefit comes especially when reading left to right
code is not like a math formula. you don't immediately care what it's doing, but what the result is.