I have a question for everyone: which way do you p...
# getting-started
s
I have a question for everyone: which way do you prefer to increment a variable by 1 in Kotlin: 1.
i++
2.
i += 1
Reason I’m asking: Swift does not have increment or decrement operators like Kotlin and other languages. When I posted about this on social media, I got comments saying using the increment operator is confusing, which doesn’t make sense to me. What’s your opinion on this?
k
Why do you care what other languages have or don't have.
What is "this" that you've posted about? About "i++" being idiomatic Kotlin? And then you got a comment from some Swift developer that doesn't understand what it does?
p
++i
j
Honestly? I don't recall the last time I had to use an increment at all in Kotlin. Functional loops are far more prevalent. I would use
i++
should I need to, but I almost never need it in the first place.
👍 3
☝🏻 1
c
You can look for the reasons in the Swift 2.2 „release notes“. https://www.swift.org/blog/swift-2.2-new-features/ Spoiler: there they also deprecated c-style-for-loops ;-)
Also can you post a link to the social media post!?
s
@Kirill Grouchnikov Could you please read the question one more time? I’m not caring about other languages; the thing I clearly stated is that I want to know which one you prefer using in Kotlin, and I just gave a little bit of extra context behind asking this question.
https://www.threads.com/@heyysudarshan/post/DSc6nyNDa4g?xmt=AQF0PGD8CvpHm7e7n7b7ZbOpXy1volvlbKs_SuPSPMfIjUJK_jkCJFEI9VDqnMpy4NEepNJG&slof=1 This is the post I’m referring to. It’s not about caring about Swift, but when I posted about ++ not being available in Swift and heard people saying it’s a confusing feature, I just wanted to hear your opinion about what the Kotlin community thinks about it. It’s not about caring about another language; it’s about asking the Kotlin community what they choose.
e
I've known i++ since I started with C, decades ago, it couldn't be confusing for me. But it's curious I've hardly used it in Kotlin.
🙈 1
y
Use
i += 1
because it’s universally readable-engineers from any background immediately understand it. It also avoids the classic C/C++ thoughts about whether
++i
or
i++
(and the side effects that sometimes come with that discussion). In general, we should reduce cognitive load in code reviews and maintenance. So we prefer
i += 1
when the code is meant to be read clearly and consistently.
❤️ 2
➕ 2
l
My project is currently at 10100 lines of code and I just counted 199 instances of
++
.
😆 1
Sorry. I missed a zeo. It's at 101000 lines of code.
@Mohd saif Ahmed So yeah, the reaction emoji was well-placed, given the previous mistake
👻 2