Skip to main content
Share Your Experience: Take the 2024 Developer Survey
42 votes

Why use a special "Name" class (instead of just a string) for representing object names in C++?

from everything I've seen, the Name class has no purpose besides providing a mutex, various asserts and other runtime error checking This is the reason why — it has behavior associated with it, which ...
Greg Burghardt's user avatar
17 votes

Why use a special "Name" class (instead of just a string) for representing object names in C++?

from everything I've seen, the Name class has no purpose besides [a list of various purposes] Sorry if this comes across as facetious but you rolled from claiming there's no purpose into listing the ...
Flater's user avatar
  • 51.7k
12 votes

Why use a special "Name" class (instead of just a string) for representing object names in C++?

the Name class has no purpose besides providing (...) These are good reasons to introduce a seperate class, but even if Name wouldn't have any additional behaviour it is still beneficial to have a ...
freakish's user avatar
  • 973
12 votes

Is updating a macro value in Xcode preprocessors marcos violating open closed principle?

If your requirements have permanently changed, just change the code already. Anything else - including slavish devotion to the open-closed principle - is abstraction for abstraction's sake and does ...
Philip Kendall's user avatar
7 votes

Is updating a macro value in Xcode preprocessors marcos violating open closed principle?

First, make sure you have understood that the OCP is not violated at the time when you change some source code, see my answer here for a detailed explanation. The OCP is followed or violated at the ...
Doc Brown's user avatar
  • 208k
7 votes

Why use a special "Name" class (instead of just a string) for representing object names in C++?

@Flater correctly identified the Primitive Obsession issue, but it may be warranted to explain a bit more why primitive obsession is an issue. What's in a type? Types are used for a variety of ...
Matthieu M.'s user avatar
6 votes

Is it good practice for object APIs to be required to be called in sequence to gather information?

Your coworker is right. Constructors should give you a fully initialized and usable object. I also understand why you don't want parsing to happen in the constructor, because it can throw an exception....
Greg Burghardt's user avatar
5 votes

Do you re-include system headers in the implementation files?

When a header file A.h requires an include for <unordered_map>, and the related cpp file A.cpp (which includes A.h) contains some code dealing with objects of type unordered_map (regardless ...
Doc Brown's user avatar
  • 208k
4 votes

Use case comparison for std::optional vs default argument

In your example, the location in code where the default value "lives" is quite different: in the first case, the printFunction itself (or the code which composes this function) has to know ...
Doc Brown's user avatar
  • 208k
3 votes

Avoiding throw because we are not sure the exceptions will always be caught

Sometimes, when we try to improve an approach to work well for even more use cases, we lose track of the first goal and end up becoming the very thing we swore to destroy. It seems like this has ...
Flater's user avatar
  • 51.7k
2 votes

Why use a special "Name" class (instead of just a string) for representing object names in C++?

Given the description, it appears the code base isn't just (merely) to enable loading/saving as XML; it appears to me that it's designed to faithfully recreate an in-memory representation of the ...
rwong's user avatar
  • 16.9k
2 votes

Why use a special "Name" class (instead of just a string) for representing object names in C++?

If you have a “Name” class instead of string, you can use it to split into family name and given name, salutation, ordering (that’s why I didn’t say “first name” because for some people the family ...
gnasher729's user avatar
  • 45.7k
1 vote

Avoiding throw because we are not sure the exceptions will always be caught

Your company's policy is making simplifying assumptions that lead to wrong decisions. That the user sees a "something goes horribly wrong" message is irrelevant. What's relevant is whether ...
Dreamer's user avatar
  • 179

Only top scored, non community-wiki answers of a minimum length are eligible