Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Amen! Having programmed in C++ for close to 20 years now, I converged to using struct a while ago and yet to run into a case where it would create problems (some conditions apply, see below).

You want to make a part of a struct private?

  /* private */
Need a stronger deterrent?

  /* private, don't touch */
Need to protect against random people? Sure, only then -

  private:
But if you are working in the confines of a smaller team of people who read comments and generally abide by coding conventions that aren't enforced in the code, then it works beautifully and results in a slender and more readable code.


> You want to make a part of a struct private? > /* private * /

Thought this was a joke first, but it's not? Is it possible you have worked in the same place or with the same people a long time? I just can't imagine this would work for us.

And I thought about it quite some time, but I really cannot see why you would prefer a comment (ie ignored by compiler, same keyword but surrounded with a few extra keystrokes) by a true first citizen keyword that does the same and enforces it? That would be the same as comments saying "hey this is const" instead of using proper const-correctness, no?


If someone really wants to touch a private field, there's more than one way to do that. So "private" merely serves as a safeguard against honest mistakes. In this capacity it is a close sibling of that "if (0 == x)" contraption. It essentially says that I don't trust myself or others to not make dumb mistakes, so here's a safeguard for that. Not that it's a bad thing to have, but I can easily see that it can be deemed redundant to just having strong coding ethics.


"Coding ethics" stop buffer overflows too, right? ;-)


It's "discipline", not "ethics". I just had a brain spasm.

And, yes, it surely stops them better than some flimsy member access restrictions.


As opposed to a private array and a public iterator API?


Naughty hands will find a way to break a container/iterator metaphor just the same as a simple buffer. Point being is that it's much better to work with competent developers than to needing to rely an idiot-proof coding style.


Sure, and if you have a variable being accessed from different threads, just slap a comment on it:

/* SHARED BETWEEN THREADS! careful. */

I'm sure people will read it. And then write safe, reliable code.


Run-time concurrency issues are exactly the same as those of code design. Sure thing. Got anything else from where this came from?


I have worked on codebases (in javascript) that did essentially this.

I tweaked private members from outside the class.

I never regretted it.

There's probably a moral here, but I'm not sure what it is.


don't forget the ever-menacing leading underscore!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: