Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8154781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:28:01+00:00 2026-06-06T16:28:01+00:00

Take the following method (written in Ruby but this question could be applied to

  • 0

Take the following method (written in Ruby but this question could be applied to most OO languages)

# Getter method which returns an alert
def alertView
  _alertView = AlertView.new
  _alertView.title = "This is the title"
  _alertView.body = "This is the body of my alert"
  _alertView
end

Suppose this method is called regularly throughout the lifecycle of an application. The title and body attribute strings would be instantiated as new objects each time.

To optimise this, we could do the following:

ALERT_TITLE = "This is the title"
ALERT_BODY  = "This is the body of my alert"

# Getter method which returns an alert
def alertView
  _alertView = AlertView.new
  _alertView.title = ALERT_TITLE
  _alertView.body = ALERT_BODY
  _alertView
end

This way, the ALERT_TITLE and ALERT_BODY strings are only instantiated once, when the class is defined, and then reused throughout the application’s lifecycle.

My question is: is the second approach optimal? While it means less work for the garbage collector and probably more stable memory use, it would also mean that the application is taking more memory all the time, rather than freeing objects that are not currently required. I’m torn between applying this to all of the constant strings within my application or not applying this approach at all and defining each string as and when required.

A third approach would be to use class variables but the only advantage this offers over the second approach is that the variables are lazy-loaded.

# Getter method which returns an alert
def alertView
  _alertView = AlertView.new
  _alertView.title = @@alert_title ||= "This is the title"
  _alertView.body = @@alert_body ||= "This is the body of my alert"
  _alertView
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:28:02+00:00Added an answer on June 6, 2026 at 4:28 pm

    The two variants above are not entirely equal. Observe:

    1.9.3p194 :001 > A = 'a'
     => "a" 
    1.9.3p194 :002 > s1 = A # constant assignment
     => "a" 
    1.9.3p194 :003 > s2 = A
     => "a" 
    1.9.3p194 :004 > s1 << 'b'
     => "ab" 
    1.9.3p194 :005 > s2 # you changed s2 as well!
     => "ab" 
    1.9.3p194 :006 > s1 = 'a' # literal assignment
     => "a" 
    1.9.3p194 :007 > s2 = 'a'
     => "a" 
    1.9.3p194 :008 > s1 << 'b'
     => "ab" 
    1.9.3p194 :009 > s2 # s2 stayed the same.
     => "a" 
    

    When two variables are assigned the same constant, there still is just one instance of the string in memory. When you assign a literal string, every variable has its own instance in memory (this doesn’t apply to symbols, which you correctly noted in another comment). So, everything depends on what you want to achieve (or avoid), how many instances you create and so on.

    If the question is really about strings, as in your example, I would say that you stop caring about efficiency and start caring about code readability, as all these are tiny tiny fractions of the memory available to your programs nowadays.

    So, I’d advice to do whatever you consider cleaner, from programmer’s perspective.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following method of course works, but after a certain number of uploads (this
Take the following example plugin: (function($) { $.fn.alertOnClick = function(text) { return this.each(function(){ $(this).click(alert(text));
I have the following HAML written to take the place of a scaffold index
I have made the following method which runs hard-coded xPath queries in a hard-coded
I'm gonna create a BackgroundWorker with an anonymous method. I've written the following code
I have the following method which I use to fill a DropDownList-Control. protected void
Take a look at the following code: method TMakerRect.Clone: TMakerObject; var newRect:TMakerRect; begin newRect
Take the following example, I have a class public class SomeItem { public string
Take the following : #include <stdio.h> main() { unsigned long long verybig = 285212672;
Take the following code snippet: SPAttachmentCollection attachments = item.Attachments ; What exactly is SPAttachmentCollection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.