Make a Person: question about getter methods and variables: which option is better

In my solution I store first, last and full name all separately, 1 variable declared for each of them.
That way, when getter method is called, it’ll simply return variable, no addititonal moves needed.

const Person = function(firstAndLast) {
  // Only change code below this line
  // Complete the method below and implement the others similarly  
  
  
  //set props as local variables
  let firstName = arguments[0].split(' ')[0];
  let lastName = arguments[0].split(' ')[1];
  let fullName = firstAndLast;
  
  //getters
  this.getFirstName = function() {
    return firstName;
  };
  this.getLastName = function() {
    return lastName;
  };
  this.getFullName = function() {
    return fullName;
  };
  
  //setters
  this.setFirstName = function(newFirst) {
    firstName = newFirst;
    fullName = newFirst + ' ' + lastName;
  }
  this.setLastName = function(newSecond) {
    lastName = newSecond;
    fullName = firstName + ' ' + newSecond;
  }
  this.setFullName = function(newFull) {
    firstName = newFull.split(' ')[0];
    lastName = newFull.split(' ')[1];
    fullName = firstName + ' ' + lastName;
  }
};

const bob = new Person('Bob Ross');
bob.getFullName();

In solution from guide, they are using only one variable to store full name, but do some splitting when getters are called.
So the question: which of these two options is more appropriate.

var Person = function(firstAndLast) {
  let fullName = firstAndLast;

  this.getFirstName = function() {
    return fullName.split(" ")[0];
  };

  this.getLastName = function() {
    return fullName.split(" ")[1];
  };

  this.getFullName = function() {
    return fullName;
  };

  this.setFirstName = function(name) {
    fullName = name + " " + fullName.split(" ")[1];
  };

  this.setLastName = function(name) {
    fullName = fullName.split(" ")[0] + " " + name;
  };

  this.setFullName = function(name) {
    fullName = name;
  };
};

var bob = new Person("Bob Ross");
bob.getFullName();

My instinct is that storing the name separately makes sense. But what if you expect the full name to be used 99% of the time? If you separate the two, what does “first” mean in some cultures? What about people that have two last names? What about names with suffixes? Etc.

I know that is beyond the scope of this, but those are things to consider.

In this case. I don’t think it matters much. And one of the nice things about hiding those details in a class is that the “outside the class” doesn’t know or care. It could be one or the other and you can change it later and they won’t know or care. That sort of things happens all the time - you get asked to do something and make some assumptions and then something changes and you need to make changes.

2 Likes

Personally, I’m a big fan of not duplicating information when I store things. Why say the same thing twice? You’re obligated to update it twice when you do that.

2 Likes

Well, I didn’t expect that someone will mention some localization problems here, but this makes sense for sure.

From your words I’m making main conclusion that answer to my question is strongly depends on the context/circumstances.

OK, just to clarify, I misread the original and thought you were asking if it should be stored as full name or as split apart. You were asking about storing both. I agree with @JeremyLT that it is bad practice to store the data twice. I suppose there might be some edge case, but my instinct would be to enter and store the parts of the name separately. But again, in this problem, with just two parts of the name put together simply, either way works.

The other issue with storing the same data twice in different ways is that you no longer have a single source of truth. What if it’s a complex system and your conversion algorithm is off? Maybe getting the data one way will yield a different result than another. Or now that data is stored and “off”.

1 Like

Got it, great thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.