Main Content
Wimbledon Neck Archive - Originally posted on "The Horse's Mouth" - 2005-06-20 08:58:36 - Graham Ellis
What use is the "+=" operator? Why did the language designers bother to provide it? Yet they did ...
In Perl and PHP
$number_on_bus = $number_on_bus + $goton_here;
becomes
$number_on_bus += $goton_here;
In Python
number_on_bus = number_on_bus + goton_here
becomes
number_on_bus += goton_here
In C and Java
number_on_bus = number_on_bus + goton_here;
becomes
number_on_bus += goton_here;
The += operator adds the result of evaluating the expression to the right into the variable named on the left.
The code is shorter, but there's more too it than that. It makes the code easier to maintain - it saves what we call "Wimbledon Neck" which is very appropriate for me to be writing about this week as the tennis championships are just about to start. If I write
$longvariablename = $longthingummyname + $some;
then anyone coming along to maintain the code has to take a careful look on both sides of the assignment to see whether or not the information is being saved in the same slot from which it was loaded or not; can you visualise the maintainer turning his neck to the left, to the right, to the left, to the right rather like watching a tennis volley from the side line? If the element being used is a member of an array (= list in Perl) or hash (= dictionary, = associative array) then that's all the more checking to do.
So - it may not come natural to you to code += (or -= *= /= etc) but please do so ... you're making life easier for those who follow behind!
Some other articles
Y103 - Conditionals and Loops Conditional operators in Python Equality (in Python) Method, Class, Module, Package - how to they relate in Python? Setting up and tearing down with the Python with keyword Finding sum, minimum, maximum and average in Python (and Ruby) Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) If elif elif elif - multiway selection in Python Identity in Python Flowchart to program - learning to program with Well House Learning to program - the if statement. Python. Finding the total, average, minimum and maximum in a program Python or Lua - which should I use / learn? Python for loops - applying a temporary second name to the same object Does a for loop evaluate its end condition once, or on every iteration? How a for loop works Java, Perl and other languages Python - fresh examples from recent courses Groupsave tickets - 3 or 4 train tickets for the price of 2 Learning to program in Python 2 ... and / or in Python 3 Saying NOT in Perl, PHP, Python, Lua ... Equality, sameness and identity - Python Decisions - small ones, or big ones? No switch in Python Python is like a narrowboat Python - when to use the in operator New - Conditional expressions in Python 2.5 Python - block insets help with documentation The ternary operator in Python This article What - no switch or case statement? P204 - Conditionals and Loops Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Showing what programming errors look like - web site pitfall Flowchart to program - learning to program with Well House Does a for loop evaluate its end condition once, or on every iteration? Increment operators for counting - Perl, PHP, C and others Are you learning Perl? Some more examples for you! For loop - checked once, or evety time? Ruby v Perl comparison and contrast Do not copy and paste code - there are much better ways Ternary operators alternatives - Perl and Lua lazy operators Equality and looks like tests - Perl Saying NOT in Perl, PHP, Python, Lua ... Learning to program in Perl Decisions - small ones, or big ones? Lexical v Arithemetic testing, Bash and Perl Smart English Output - via PHP and Perl ? : operator -> , >= and => in Perl This article J704 - Loops and Conditional Statements Java example - for loop and conditionals from course exercise A behaviour driven example of writing a Java program Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Does a for loop evaluate its end condition once, or on every iteration? How a for loop works Java, Perl and other languages Setting up individual variables, and arrays, in Java - some commented examples for and foreach in Java Saying NOT in Perl, PHP, Python, Lua ... Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) This article H104 - Control Statements Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) While, for, foreach or something else to loop. Flowchart to program - learning to program with Well House Does a for loop evaluate its end condition once, or on every iteration? How a for loop works Java, Perl and other languages Predictions for the seagull population Extracting real data from an exported file in PHP or Perl Question Mark - Colon operator (Perl and PHP) Saying NOT in Perl, PHP, Python, Lua ... Decisions - small ones, or big ones? for loop - how it works (Perl, PHP, Java, C, etc) Testing for one of a list of values. Smart English Output - via PHP and Perl ? : operator Breaking a loop - Ruby and other languages Double and Triple equals operator in PHP The ternary operator in Python Don't repeat code - use loops or functions Assignment, equality and identity in PHP This article Code and code maintainance efficiency C203 - Conditionals and Loops Learning to program sample program - past its prime, but still useful Learning to program - Loop statements such as while Learning to Program - the conditional statement (if) Does a for loop evaluate its end condition once, or on every iteration? Loops - a comparison of goto, while and for Breaking the running sequence - an introduction to conditional statements and loops How a for loop works Java, Perl and other languages New year, new C Course Increment operators for counting - Perl, PHP, C and others Function Prototypes in C New C Examples - pointers, realloc, structs and more Saying NOT in Perl, PHP, Python, Lua ... Ruby, C, Java and more - getting out of loops for loop - how it works (Perl, PHP, Java, C, etc) Smart English Output - via PHP and Perl ? : operator Breaking a loop - Ruby and other languages This article