#I still don t see what the difference in

1 messages ยท Page 1 of 1 (latest)

elfin moat
#

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?

regal tangle
#

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.

elfin moat
#

understandable, but it's a whole different syntax. even though they look kind of similar, these are 2 very different things language wise

regal tangle
#

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)

elfin moat
#

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

regal tangle
#

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)

elfin moat
#

yes. but now new() is available, which makes var just a pain in the ass legacy (mis-)feature

regal tangle
#

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()

elfin moat
#

it's usually a 2 step process. -> convert explicit type to explicit -> reduce righthand type creation to new()

regal tangle
#

Thanks for the talk / insights, I appreciate it!

elfin moat
#

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.

regal tangle
#

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 ๐Ÿ™‚ )

elfin moat
#

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

worldly tartan
#

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.

regal tangle
regal tangle
#

And reading flow I guess

#

(i.e. code style)

worldly tartan
#

your for loop will read the same with ++i or i++. 99.9% have agreed to i++

regal tangle
#

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

elfin moat
worldly tartan
#

also more in line to how we write out math formulas

elfin moat
elfin moat
#

code is not like a math formula. you don't immediately care what it's doing, but what the result is.