Live data from Hacker News

C++ Patterns: The Badge

awesomekling.github.io

71–80 of 160 posts

Re: C++ Patterns: The Badge

#71
post #47

While this is a clever little pattern, I'd argue that if you have a method of class X that can only be called from class Y, that is a code smell.

protected and friend are both smelly keywords?

Some people argue exactly that. I don't necessarily agree.

Re: C++ Patterns: The Badge

#72

I used to carefully design my C++ code with public / private / protected / friend (and the pointer-to-impl pattern for "compile time private") but a few years ago I started doing everything all-public ala python (struct instead of class by default) with occasionally an m_ prefix for "intended to be private", and I've never looked back. It's been great, made my programming life much easier. This badge thing is clever,…

In larger codebases with many developers, attention to the API is required in order to be able to maintain code without breaking clients. The main downside of Badge is that it comes with a performance penalty and ABI change for something that should be statically deducible (empty struct is one byte in C++).

Don't you have to have pretty high performance needs for this class to make a performance difference? Unless it's called multiple thousand times per second I don't see it making much of a difference.

Re: C++ Patterns: The Badge

#73
post #45

This is a typical example of a solution to an artificial, self-imposed problem created by C++'s assumptions. As a programmer, I would feel frustrated to have to figure this out instead of spending time on something a user would actually care about.

Which popular programming languages provide proper ACLs (or equivalent functionality) for class members?

[deleted]

Re: C++ Patterns: The Badge

#74
post #67
post #66

Earlier quoted context omitted.

I have to ask... why 'm_'?

I think it is supposed to mean member, but i don’t remember, common enough practice though

Yes it stands for member. The notation had been used in conventions like Hungarian notation (https://en.m.wikipedia.org/wiki/Hungarian_notation) and WinApi.

Re: C++ Patterns: The Badge

#75

Earlier quoted context omitted.

Which popular programming languages provide proper ACLs (or equivalent functionality) for class members?

I'm aware of C++ and Xojo for sure.

Is it possible to have a proper compile time ACL for C++?

Arguably the Badge pattern as presented in the article is just an honor system that provides zero security, since it's trivial to forge a fake Badge: https://repl.it/repls/BountifulQuerulousCron

Re: C++ Patterns: The Badge

#76
post #47

While this is a clever little pattern, I'd argue that if you have a method of class X that can only be called from class Y, that is a code smell.

Sometimes, clever little patterns like that are useful to get stuff done in time. However, it that case it definitely smells a lot. Also, the global singleton stinks.

To me this is a violation of the Interface Segregation Principle. I think the author went the right way to fix it, but not far enough.

He added a parameter to segregate the device registration operations from other operations. It works, but the segregation is artificial : you still need a Device object (which seems to be used by a lot of classes, another smell, god object ?)

Instead of asking for an entire Device object, register_device should ask for a different type, specific to that operation, like « NewDevice », « UnregisteredDevice », etc. This object would depend on very specific information only available at the device creation. Now, if another developer tried to call register_device, he would need to create a weird object that he never heard of, with parameters he cannot even provide.

Thoughts ?

Re: C++ Patterns: The Badge

#78
post #66

I used to carefully design my C++ code with public / private / protected / friend (and the pointer-to-impl pattern for "compile time private") but a few years ago I started doing everything all-public ala python (struct instead of class by default) with occasionally an m_ prefix for "intended to be private", and I've never looked back. It's been great, made my programming life much easier. This badge thing is clever,…

I have to ask... why 'm_'?

It's a simplified form of Hungarian notation: https://en.wikipedia.org/wiki/Hungarian_notation

Re: C++ Patterns: The Badge

#79

Earlier quoted context omitted.

In larger codebases with many developers, attention to the API is required in order to be able to maintain code without breaking clients. The main downside of Badge is that it comes with a performance penalty and ABI change for something that should be statically deducible (empty struct is one byte in C++).

You can make register_device() an inline function that just calls a do_register_device() private function without the badge. That way you don't have to pay for the empty class.

I'm not sure I follow... The motivation for Badge is to have methods on VFS that only Device can call, without exposing VFS's internals to Device. Without Badge, what prevents non-Device functions from calling register_device()?

Re: C++ Patterns: The Badge

#80

I used to carefully design my C++ code with public / private / protected / friend (and the pointer-to-impl pattern for "compile time private") but a few years ago I started doing everything all-public ala python (struct instead of class by default) with occasionally an m_ prefix for "intended to be private", and I've never looked back. It's been great, made my programming life much easier. This badge thing is clever,…

In larger codebases with many developers, attention to the API is required in order to be able to maintain code without breaking clients. The main downside of Badge is that it comes with a performance penalty and ABI change for something that should be statically deducible (empty struct is one byte in C++).

There is no performance penalty in any modern compiler that I am aware of (effectively no code will be emitted).

You can have a look here: https://godbolt.org/z/DmeYL-

Post reply on HN