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

  • SEARCH
  • Home
  • 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 6114621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:59:46+00:00 2026-05-23T14:59:46+00:00

I often come to this question when coding. Which of the following examples is

  • 0

I often come to this question when coding.

Which of the following examples is a better practice? I am aware that other factors will influence whether one or the other one is better. But in general, what are the advantages of one over the other.

if(object.getA().Value != null) {
    return object.getA().Value;
}
return null;

Vs.

string x = string.null;
x = object.getA().Value;
return (x != null) ? x : null;

Here is another similar example:

var a = object.method(x).Value;
var b = object.method(x).Key;

Vs.

var y = object,method(x);
var a = y.Value;
var b = y.Key;

In other words my question is:

Is it better to call a method twice and have one less variable?
or
Is it better to save it into a variable and call the method twice?

Of course if the method results in a lot of processing it might be smart to call it once, but for general cases where the method is not too demanding and the space of the variable is not too big, which one is better and why? or which are the advantages of one or the other?

The difference between them might not make a big difference but I am trying to find better practices and will like to hear the input of some experienced programmers.

Many thanks

  • 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-05-23T14:59:46+00:00Added an answer on May 23, 2026 at 2:59 pm

    Caching the value in a variable is a basic optimization (related to memoizing).

    When it becomes truly necessary is if the second call to the function is on the stack a significant percent of the time.

    For example, if that second call is on the stack 10% or 20% of the time, then that’s how much overall time you can save by caching the first result.

    You can keep doing things like that, until the code is as fast as possible.

    If I can give an example, ages ago I worked on an app that had code like this:

    if (!Done()){
      do some stuff
    }
    ....
    if (!Done()){
      do some other stuff
    }
    

    Since Done() was such a short, clean, and simple function to call, it got called a lot.
    Never mind that it did a lot including querying a ton of stuff from a DB and throwing most of it away.
    Stackshots found the problem instantly.

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

Sidebar

Related Questions

This is sort of a general question that has come up in several contexts,
This is a problem I come across every so often; I always end up
I often come across web applications that expose internal database primary keys through forms
So we have a lot of routines that come out from exporting. We often
As mentioned in this other question , if a user grants the publish_stream permission,
This is a general kind of question. Very often, I need to write JavaScript
[Caveat] This is not directly a programing question, but it is something that comes
I often come accross the problem that I have a class that has a
Very often I come across negative feedback on Java Date and other date-time-related classes.
I come from a python background, where it's often said that it's easier to

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.