Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
2 Answers
Greg Kaleka
39,019 Points
Carlos Ramirez
5,084 Points
Carlos Ramirez 5,084 Points Carlos Ramirez
Thank you. My question is why teachers in treehouse decide to use i += 1 instead of i++ (which is more commonly used at least in Java). You know i += 1 is harder to write and also is more easy to make mistakes. I saw a question months ago which someone following the courses wrote i =+ 1. What I mean is that must be a very good reason for using i += 1, I want to know which is.
Greg Kaleka
39,019 Points
There's not really a reason other than preference.
If you want a reason for why someone might have a preference for += 1 over ++, a recent change to Swift (for iOS programming) gives a great explanation.
Swift did away with the ++ operator altogether. Here are the reasons Chris Latner, the creator of Swift, cited (not all of these apply to Javascript, but many do):
- These operators increase the burden to learn Swift as a first programming language - or any other case where you don't already know these operators from a different language.
- Their expressive advantage is minimal - x++ is not much shorter than x += 1.
- Swift already deviates from C in that the =, += and other assignment-like operations returns Void (for a number of reasons). These operators are inconsistent with that model.
- Swift has powerful features that eliminate many of the common reasons you'd use ++i in a C-style for loop in other languages, so these are relatively infrequently used in well-written Swift code. These features include the for-in loop, ranges, enumerate, map, etc.
- Code that actually uses the result value of these operators is often confusing and subtle to a reader/maintainer of code. They encourage "overly tricky" code which may be cute, but difficult to understand.
- While Swift has well defined order of evaluation, any code that depended on it (like foo(++a, a++)) would be undesirable even if it was well-defined.
- These operators are applicable to relatively few types: integer and floating point scalars, and iterator-like concepts. They do not apply to complex numbers, matrices, etc.
Finally, these fail the metric of "if we didn't already have these, would we add them to Swift 3?"
See AlsoBeep Me! (Beeper Codes)
Here's a Stack Overflow thread specific to Javascript that has good arguments on both sides of the "debate".
Steven Parker
218,654 Points
Note that "
i++
", "i += 1
", and "i = i + 1
" are all the same when used as stand-alone statements. But when used as an expression, "i++
" returns the value before incrementing, while the others return the final value. A fourth option is "++i
" which also returns the final value.
Greg Kaleka
39,019 Points
Good point Steven - that distinction is really the reason for item 6 on Chris's list. The fact that you can do that is also the reason for # 5. If you're taking advantage of the difference between i++ and ++i, you're probably getting too cute.
Steven Parker
218,654 Points
I don't think I'd use the term "cute", but I agree that making use of the difference between the pre-increment and post-increment operators is definitely an advanced topic and not recommended for beginning programming. I only brought it up as a point of information.
For most students, I recommend sticking to the addition assignment operator ("+=
") for consistency and readability.
Greg Kaleka
39,019 Points
My mistake
Sure - there are arguments to be made for the operators. They exist, after all! Absolutely worth bringing up.
dylan jasper
1,928 Points
I watched a video from Douglas Crockford (guy who wrote JavaScript: The Good Parts) who said that he never uses the prefix or postfix ++ operator as some people don’t understand the difference and it can lead to mistakes. He just sticks with x += 1 style.