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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:54:51+00:00 2026-05-27T07:54:51+00:00

When I code stuff, I try to divide all things in functions (methods, if

  • 0

When I code stuff, I try to divide all things in functions (methods, if you like). Function X does stuff X, Y does tuff Y and not like method X does stuff X, Y & Z! This gives me more re-usable code. I like that. 🙂

Lets take a look on this code:

var user = {
  users: [],
  userCount: 0,
  addUser: function(user) {
    (this.users).push(user);
  },
  incrementCount: function() {
    ++this.userCount;
  }
}

var user = { // 2nd example.
  users: [],
  userCount: 0,
  addUser: function(user) {
    (this.users).push(user);
    ++this.userCount;
  }
}

(It’s in JavaScript, but language here is non-essential.)

In my opinion, second example will be easier and safer to use for, lets say, API user.
It’s easy to forget to call user.incrementCount(). What do you think? Second example does it automatically.

So how to find a balance? Any best-practices about calling functions inside of functions?

Thanks for reading this.

Edit:

This came in my mind just now:

var user = {
  users: [],
  userCount: 0,
  addUser: function(user) {
    (this.users).push(user);
    this.incrementCount();
  },
  incrementCount: function() {
    ++this.userCount;
  }
}
  • 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-27T07:54:51+00:00Added an answer on May 27, 2026 at 7:54 am

    It’s a little different in JS, since there isn’t a way to make functions truly private when using object literal notation, but…

    It’s all about the API you want your objects to expose to their consumers. Do you want consumers of your API to be able to increment the count separately from adding a user? If so:

    { addUser: /* snip */, incrementCount: /* snip */ }
    

    Otherwise:

    { addUser: /* snip */, _incrementCount: /* snip */ }
    // or just
    { addUser: /* snip */ }
    

    In this particular case, I would strongly recommend against storing any separate count at all, since the users array already does that for you.

    var user = {
      _users: [],
      addUser: function(user) {
          this._users.push(user);
      },
      getUserCount: function () {
          return this._users.length;
      }
      // and if you need to expose the users array directly,
      , getUsers: function () {
          return this._users;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My cUrl call is returning HTML code like: "Some stuff." and I'd like to
I see a lot in autoconf code about stuff being dnl'ed and not dnl'ed.
I have code with the following form: <?php function doSomething{ //Do stuff with MySQL
When I try to develop CRM stuff, using the code below: public static CrmService
Not sure what's going on. I've used the following bit of code to try
is that an ok code? try { /*stuff*/ } catch (Exception e) { /*stuff*/
I am trying to code some basic stuff (listView and the like) in Android.
I do not really use any try/catches in my code ever but I'm trying
I've recently bee seen a number of code examples with stuff like 1 <<
I was trying to clean up some accessability stuff in my code, and inadvertently

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.