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 848953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:01:28+00:00 2026-05-15T07:01:28+00:00

I was browsing my teacher’s code when I stumbled across this: Order* order1 =

  • 0

I was browsing my teacher’s code when I stumbled across this:

Order* order1 = NULL;

then

order1 = order(customer1, product2);

which calls

Order* order(Customer* customer, Product* product)
{
    return new Order(customer, product);
}

This looks like silly code. I’m not sure why, but the teacher initialized all pointers to NULL instead of declaring them right away(looking at the code it’s entirely possible, but he chose not to).

My question is: is this good or acceptable code? Does the function call have any benefits over calling a constructor explicitely? And how does new work in this case? Can I imagine the code now as kind of like:

order1 = new Order(customer, product);

  • 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-15T07:01:28+00:00Added an answer on May 15, 2026 at 7:01 am

    Init to NULL

    [edit] since there’s a valid discussion, I’ve changed the order of the options a bit to emphasize the recommended option.

    Variables should be declared as local and as late as possible, and initialized immediately. Thus, the most common pattern is:

    Order * order1 = order(...);
    

    just before order1 is required.
    If there is any reason to separate the declaration of order1 from the instantiation, like this:

    Order * order1;  // Oh no! not initialized!
    // ... some code
    order1 = order(...);
    

    order1 should be initialized to NULL, to prevent common bugs that occur with uninitialized variables, easily introduced when // some code changes.

    Factory method
    Again, there’s some more change resilence here: the requirements for instantiating an Order may change. There are two scenarios I can think of right off top of my head:

    (1) Validation that can’t be done by Order’s constructor. Order may come from a 3rd party library and can’t be changed, or instantiation needs to add validation that isn’t within the scope of Order:

    Order* order(Customer* customer, Product* product)             
    {      
        // Order can't validate these, since it doesn't "know" the database       
        database.ValidateCustomer(customer); // throws on error
        database.ValidateProduct(product); // throws on error
    
        return new Order(customer, product);             
    }   
    

    (2) You may need an order that behaves differently.

    class DemoOrder : public Order  { ... }
    
    Order* order(Customer* customer, Product* product)             
    {             
        if (demoMode)
          return new DemoOrder(customer, product); // doesn't write to web service
        else
          return new Order(customer, product);             
    }   
    

    However, I wouldn’t make this a general pattern blindly.

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

Sidebar

Related Questions

While browsing some source code I came across a function like this: void someFunction(char
I was just browsing Sizzle's source code and I came across this line of
While browsing some code, I came across this line: if False: #shedskin I understand
While browsing I came across this blog post about using the Wikipedia API from
While browsing MSDN documentation, you may come across this gem: TextBox.Watermark. Awesome! I've been
Browsing through the source code of Microsoft's sample StockTrader application, I found this snippet
Browsing the ruleby source code, I noticed that they were calling Container(:and) , which
Was browsing the jQuery source code when I met this line: jQuery(this)[ state ?
Browsing the code sample from C# 4.0 in a nutshell I came across some
While browsing the code of an erlang application, I came across an interesting design

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.