While that backs the theory of these keywords I have seen other posts where they recommend to not use [[nondiscard]] because it makes the header file so loaded |
You might want your code to be nice, but you're not paid to produce pretty code. Instead, you're paid to produce
correct software, and do so on time and on budget. This is the measure by which you need to evaluate decisions. None of your users know or care what your source code looks like: it's immaterial to your bottom line.
Broad user experience shows that
nodiscard locates software defects. This crucial and direct benefit comes at the trivial cost of... typing a little more?
Rejecting
nodiscard is bad engineering.
There are probably reasons someone would want to discard the value of the call. You just haven't thought of them. |
In that unlikely case, the caller may silence the warning by converting the result to
void.
static_cast<void>(get_x())
or in this special case, even a C-style conversion is harmless
(void)get_x()
Noexception because a simple return of a private variable can not cause an exception |
The standard library's usage rationale regarding noexcept is a good case study.
http://www.cplusplus.com/forum/general/256009/#msg1121235
However, your code is not the standard library. Take this into account.
Get and Set functions [...] |
Don't write these trivial getters and setters. The usual excuse for writing do-nothing functions is to "increase flexibility". However, there are
very few changes you can make to
Foo (other than additions) that
don't require you to review every location the interface is used.
You gain nothing from trivial getters and setters until you make such a change. In exchange for a potential future benefit that rarely manifests, engineers must look up
GetA in order to ensure its preconditions (if any exist) are met at the call-site, to verify properties like thread-safety, to check its exception-safety guarantees, and to make sure they respect any special caveats involving its use.
Just say no. Quit wasting time and use the plain struct
struct point { int x_, y_; };
Exceptions to this guideline can involve interfaces with representations that might change very frequently, or interfaces with a stronger requirement for stability. Most classes, most code, only sees local use within a unit or subsystem of a larger program. Such code needs minimal encapsulation, because it will be easier to adjust the uses of that interface than trying to design the interface to allow the change without breaking existing code.
Another question I have is: Do Set and Get functions still need to get defined in the cpp file? I have seen other (production) code where they are directly defined in the header and these functions are just the obvious one liners. Wouldn't defining it in the header encourage bloating? |
Functions inside a class definition are implicitly inline. The contents of inline functions are part of the ABI.
noexcept is also part of the ABI.
I might be massively overthinking here but I just want to get it right. |
Nah, this is a good question.