doc: Tidy up shadowing section #16461

pull promag wants to merge 1 commits into bitcoin:master from promag:2019-07-fix-devnotes changing 1 files +2 −16
  1. promag commented at 3:48 PM on July 25, 2019: member

    Removes the example because it violates the code format.

  2. in doc/developer-notes.md:632 in 5bc447b924 outdated
     633 | +    Mode mode;
     634 | +    int index;
     635 | +
     636 | +    AddressBookPage(Mode _mode, int _index)
     637 | +        : mode(_mode)
     638 | +        , index(_index)
    


    promag commented at 3:49 PM on July 25, 2019:

    @MarcoFalke :trollface:

  3. in doc/developer-notes.md:625 in 5bc447b924 outdated
     621 | @@ -622,13 +622,16 @@ E.g. in member initializers, prepend `_` to the argument name shadowing the
     622 |  member name:
     623 |  
     624 |  ```c++
     625 | -class AddressBookPage
     626 | +struct AddressBookPage
    


    MarcoFalke commented at 3:54 PM on July 25, 2019:

    According to clang-format, the { comes on the same line

  4. in doc/developer-notes.md:627 in 5bc447b924 outdated
     621 | @@ -622,13 +622,16 @@ E.g. in member initializers, prepend `_` to the argument name shadowing the
     622 |  member name:
     623 |  
     624 |  ```c++
     625 | -class AddressBookPage
     626 | +struct AddressBookPage
     627 |  {
     628 | -    Mode m_mode;
    


    MarcoFalke commented at 3:54 PM on July 25, 2019:

    members should start with m_ according to the developer notes


    promag commented at 4:40 PM on July 25, 2019:
    • Class member variables have a m_ prefix.

    This is now a struct.


    sipa commented at 4:43 PM on July 25, 2019:

    Structs and classes are the same thing in C++. Only default visibility is different.

  5. MarcoFalke commented at 3:55 PM on July 25, 2019: member

    Not sure what is being "fixed" here

  6. DrahtBot added the label Docs on Jul 25, 2019
  7. promag commented at 4:20 PM on July 25, 2019: member

    @MarcoFalke in the example the constructor argument wouldn't shadow the member.

  8. promag force-pushed on Jul 25, 2019
  9. in doc/developer-notes.md:632 in 22b08eab54 outdated
     630 |  
     631 | -AddressBookPage::AddressBookPage(Mode _mode)
     632 | -    : m_mode(_mode)
     633 | +    AddressBookPage(Mode _m_mode, int _m_index)
     634 | +        : m_mode(_m_mode)
     635 | +        , m_index(_m_index)
    


    MarcoFalke commented at 4:57 PM on July 25, 2019:

    promag commented at 5:11 PM on July 25, 2019:

    😢

  10. promag force-pushed on Jul 25, 2019
  11. promag force-pushed on Jul 26, 2019
  12. laanwj commented at 1:23 PM on July 29, 2019: member

    I still don't get it … can you please keep changes contained to what you're actually fixing, and not change code structure at the same time

  13. promag commented at 1:40 PM on July 29, 2019: member

    can you please keep changes contained to what you're actually fixing, and not change code structure at the same time

    done

    I still don't get it

    the example is wrong considering the comment:

    E.g. in member initializers, prepend _ to the argument name shadowing the member name:

    so in order to be correct the argument must be the same as the member..

  14. MarcoFalke commented at 1:47 PM on July 29, 2019: member
    E.g. in member initializers, prepend _ to the argument name shadowing the member name:

    This is wrong. We prepend members with m_ and pass arguments without m_. There should never be a shadow.

  15. promag commented at 1:52 PM on July 29, 2019: member

    @MarcoFalke right, that's why initially I had

    struct AddressBookPage
    {
       Mode mode
    

    Do you prefer to drop this case then?

  16. MarcoFalke commented at 2:03 PM on July 29, 2019: member

    Do you prefer to drop this case then?

    Yes, if there is an incorrect statement in the notes, it should be removed.

  17. ryanofsky commented at 3:14 PM on July 29, 2019: member

    This is wrong. We prepend members with m_ and pass arguments without m_. There should never be a shadow.

    FWIW (feel free to ignore this comment), I don't think it makes sense to prefix struct members with m_. The point of m_ is to make member function code more readable, so readers will not confuse member variables with argument variables and local variables. In python, you wouldn't prefix members with m_ because you would say self. instead. In a struct, you shouldn't prefix members with m_ because there shouldn't be any non-trivial code using implicit this. Structs shouldn't any nontrivial member functions. But they could have simple helpers methods like SerializationOp, operator<, or convenience constructors where shadowing could happen, so it'd be reasonable to have convention for shadowing.

    Anyway, I don't think we need to have a whole discussion about code style. I'm fine with any change you want to make. I'm just responding to the "this is wrong" claim above, which I don't think is factual, just expressing a different preference.

  18. MarcoFalke commented at 3:20 PM on July 29, 2019: member

    Yeah, I assumed struct is treated the same as class when it comes to the m_ prefix

  19. promag commented at 3:24 PM on July 29, 2019: member

    I just want to fix the example which is obviously wrong in regards to shadowing.

  20. ryanofsky commented at 3:32 PM on July 29, 2019: member

    I just want to fix the example which is obviously wrong in regards to shadowing.

    Could drop the example and comment about shadowing since it should come up much more rarely now. Or use:

    struct AddressBookPage {
        AddressBookPage(Mode _mode) : mode(_mode) {}
        Mode mode;
    };
    

    or

    class AddressBookPage
    {
    public:
        AddressBookPage(Mode mode) : m_mode(mode) {}
    private:
        Mode m_mode;
    };
    
  21. sipa commented at 4:51 PM on July 29, 2019: member

    I think I always interpreted that section (and intended it) to mean structs and classes both get the m_ treatment. However, I do see that isn't very obvious from the text, and #16461 (comment) gives a pretty reasonable argument why it may be preferable not to apply it to structs.

    So I guess we should clarify it, but I don't have a strong opinion either way.

  22. promag commented at 7:52 AM on July 30, 2019: member

    I think we could have POD members without m_ prefix.

  23. laanwj commented at 2:01 PM on July 31, 2019: member

    I think we could have POD members without m_ prefix.

    Was about to say, that discussion is about POD versus OOP encapsulation, not about class versus struct per se—although there's definitely overlap between those distinctions how they're used normally

  24. ryanofsky commented at 2:09 PM on July 31, 2019: member

    I think PODness is orthogonal here. Take a struct like InitInterfaces. It is definitely not a POD. But it has no methods and makes no use of implicit *this, so there is little reason to prefix its members with m_.

    [EDIT] Other examples: WalletAddress, WalletTx:

    https://github.com/bitcoin/bitcoin/blob/3f288a1c05ebcadd7d7709f81c77921ff9e27ba2/src/interfaces/wallet.h#L296-L308

    https://github.com/bitcoin/bitcoin/blob/3f288a1c05ebcadd7d7709f81c77921ff9e27ba2/src/interfaces/wallet.h#L330-L344

  25. MarcoFalke commented at 3:21 PM on July 31, 2019: member

    Agree that whether something is integer or not should not influence the m_ style

  26. promag commented at 11:46 PM on July 31, 2019: member

    Right, POD members must be POD too.

    makes no use of implicit *this

    Yup, should be enough to not require m_. The problem is that later on methods could be added which would screw existing member names.

    I'll hold on with this until we clarify the m_ treatment, otherwise I'm tempted to either remove the example of just close this.

  27. laanwj commented at 1:06 PM on August 1, 2019: member

    I think PODness is orthogonal here

    OK, yes, POD is the not the right name either. It depends on whether the fields are supposed to be accessed externally or in methods.

    otherwise I'm tempted to either remove the example of just close this.

    I'd say let's just remove this example.

  28. fanquake added the label Waiting for author on Aug 14, 2019
  29. fanquake commented at 9:52 AM on August 14, 2019: member

    @promag Did you want to follow up here and remove the example?

  30. promag commented at 10:39 AM on August 14, 2019: member

    Yes, will do.

  31. promag force-pushed on Aug 28, 2019
  32. promag renamed this:
    doc: Fix code example in developer notes
    doc: Tidy up shadowing section
    on Aug 28, 2019
  33. promag commented at 5:44 PM on August 28, 2019: member

    @fanquake done.

  34. doc: Tidy up shadowing section 9452802480
  35. promag force-pushed on Aug 28, 2019
  36. MarcoFalke commented at 5:59 PM on August 28, 2019: member

    unsigned ACK 9452802480bd154e23771230bbdfebde1dbaa941

  37. ryanofsky approved
  38. ryanofsky commented at 7:25 PM on August 28, 2019: member

    ACK 9452802480bd154e23771230bbdfebde1dbaa941

  39. fanquake approved
  40. fanquake commented at 12:16 AM on August 29, 2019: member

    ACK 9452802480bd154e23771230bbdfebde1dbaa941 - Thanks for following up.

  41. fanquake removed the label Waiting for author on Aug 29, 2019
  42. fanquake referenced this in commit 085fe76299 on Aug 29, 2019
  43. fanquake merged this on Aug 29, 2019
  44. fanquake closed this on Aug 29, 2019

  45. PastaPastaPasta referenced this in commit 5afb8b7e67 on Jun 27, 2021
  46. PastaPastaPasta referenced this in commit be0b004a47 on Jun 28, 2021
  47. PastaPastaPasta referenced this in commit f74cacdfcf on Jun 29, 2021
  48. PastaPastaPasta referenced this in commit 7d3cb7e375 on Jul 1, 2021
  49. PastaPastaPasta referenced this in commit 463a535bab on Jul 1, 2021
  50. PastaPastaPasta referenced this in commit d02d7f0eec on Jul 12, 2021
  51. PastaPastaPasta referenced this in commit ee46c9545e on Jul 13, 2021
  52. DrahtBot locked this on Dec 16, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-22 00:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me