clean-code-javascript icon indicating copy to clipboard operation
clean-code-javascript copied to clipboard

Make objects have private members better (?) example

Open felipe-augusto opened this issue 8 years ago • 5 comments

I was looking at the example of this section and I think this can be simplified achieving the same results, because if the object has too many functions it will be hard to see which ones are public. Also I got confused, if this is an object (as the title says), it shouldn't have been named with the first capitalized letter? I think it should be something like this:

function Employee(name) {
  this.getName = () => name;
  return {
    getName
  }
}

const employee = Employee('John Doe');
console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe
delete employee.name;
console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe

Or even simplier:

function Employee(name) {
  this.getName = () => name;
}

const employee = new Employee('John Doe');
console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe
delete employee.name;
console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe

felipe-augusto avatar Feb 03 '17 01:02 felipe-augusto

More functional approach to the hashIt function (comments section):

function hashIt(data) {
  const INITIAL_VALUE = 0;
  return data.split('').reduce((previous, current) => {
    return ((previous << 5) - previous) + current.charCodeAt();
  }, INITIAL_VALUE);
}

felipe-augusto avatar Feb 03 '17 01:02 felipe-augusto

@fesnt With the current code you don't have to instantiate using the new keyword.

function makeEmployee(name) {
  return {
    getName() {
      return name;
    },
  };
}

By using this in a function (the code in your comment), I think the risk for introducing bugs will increase. If forgetting to use the new keyword (still valid JavaScript), this will be bound to the global object and not to the Employee.

Also, capitalization is used as a convention for function constructors (i.e. combined with the this keyword) in JavaScript before ES 2015. With ES 2015 class was introduced to make this style of coding easier. The class keyword is syntax sugar for function constructors.

DavidVujic avatar Feb 03 '17 08:02 DavidVujic

@DavidVujic thanks for the reply. I agree with you in the first paragraph, forgetting the new keyword would produce a lot of unnecessary bugs. I also agree with the second one, but since class is better, this example shouldn't use it instead?

felipe-augusto avatar Feb 04 '17 16:02 felipe-augusto

@fesnt My guess is that a class would produce more boilerplate code and also would need to keep state in a name variable. Do you have a code example, or maybe do a Pull Request?

DavidVujic avatar Feb 04 '17 19:02 DavidVujic

@fesnt @DavidVujic Good points from you both. Both ways are valid, but there's some good arguments to be made about avoiding the this keyword when possible. It can cause whole class of issues, no pun intended. This guide uses the this keyword in several places, so at some point I want to make a stand about if we should use this it all. I know the Classes section has to use it, but everywhere else we can avoid it. JavaScript's factory functions, as David shows in the private members example, allows you to not have to use the new keyword and worry about this

https://medium.com/javascript-scene/the-two-pillars-of-javascript-ee6f3281e7f3#.p0c69ielf

ryanmcdermott avatar Feb 05 '17 20:02 ryanmcdermott