The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Strings: += and + Considered Harmful

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Corvus Corvidae

Posts: 12
Nickname: corvidae
Registered: Feb, 2006

Corvus Corvidae is not dead
Strings: += and + Considered Harmful Posted: Feb 23, 2006 2:53 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Corvus Corvidae.
Original Post: Strings: += and + Considered Harmful
Feed Title: corvidae_rb
Feed URL: http://corvidae-rb.livejournal.com/data/rss
Feed Description: corvidae_rb - LiveJournal.com
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Corvus Corvidae
Latest Posts From corvidae_rb

Advertisement
Just a little note about memory management in Ruby. Sure, Ruby
has a garbage collection scheme that takes care of memory for us,
but that doesn't mean we should just litter willy-nilly.

The += operation does not update mutable objects in-place, and
the + operator evaluates each side before adding them. That means
that this snippet:
  s = "foo"
  ('a'..'z').each do |x|
    s += "foo:" + x
  end

creates a lot of unnecessary string litter. Three times as much as
if we used the << operator:
  s = "foo"
  ('a'..'z').each do |x|
    s << "foo:" << x
  end

About 79 standard library files show evidence of using += or + with
strings, and most cases (if not all) could be probably be replaced
with << for a more memory efficient standard code base.

Read: Strings: += and + Considered Harmful

Topic: A Little Language Labeling Previous Topic   Next Topic Topic: Ruport Released

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use