BanMan::IsBannedLevel should return an enum #15929

issue jonnygrant opened this issue on May 1, 2019
  1. jonnygrant commented at 7:38 AM on May 1, 2019: none

    Would a patch be supported to change this function to return enum?

    <!-- Describe the issue -->

    This function is returning magic numbers [0, 1, 2} bitcoin/src/banman.cpp int BanMan::IsBannedLevel(CNetAddr net_addr)

    <!--- What behavior did you expect? -->

    Should use an enum.

    latest github trunk.

  2. MarcoFalke added the label P2P on May 1, 2019
  3. jonnygrant commented at 3:55 PM on May 3, 2019: none

    Magic numbers are often bad. As you can see people had to use the magic number elsewhere. Why are you defending it?

  4. promag commented at 4:12 PM on May 3, 2019: member

    If your suggestion is not replacing int with enum then please open a PR with the actual suggestion and I'll happily review it.

  5. jonnygrant commented at 4:18 PM on May 3, 2019: none

    That's exactly my suggestion. Why do you feel a magic number is better?

  6. jonnygrant commented at 9:40 PM on May 11, 2019: none

    Feels like a difficult project to contribute to, is it really like this? Actually found this little issue in the first file I opened specifically to see if Bitcoin Core team were receptive to improvements.

  7. dakk commented at 2:26 PM on May 13, 2019: none

    if you think it could be useful to migrate the bannedlevel from an int to an enum, I wrote the modification in this branch and I can open a pr: https://github.com/dakk/bitcoin/commits/ban-level-enum

  8. promag commented at 2:36 PM on May 13, 2019: member

    You can see my point in @dakk's commit b57206abfcc003176b093de7e68e2b73135dea41 where the operator> is used. Maybe that line should be

    pnode->m_prefer_evict = bannedlevel != NOT_BANNED;
    
  9. dakk commented at 2:51 PM on May 13, 2019: none

    I changed the code as you proposed, and transformed the enum to an enum class as suggested in the code guidelines

  10. jonnygrant commented at 3:05 PM on May 13, 2019: none

    A C++ Enum in CAPS is bad coding style. Have you seen Cpp Core Guidelines?

    Also there are still bad hard coded numbers in comments, duplicating the code. Those comments I would simply be removed. The code is self explanatory.

    I proposed this simple change. Can I simply implement it please?

    Thanks Jonny

  11. promag commented at 3:10 PM on May 13, 2019: member

    Can I simply implement it please? @jonnygrant you are free to implement and submit whatever you want without asking for permission.

  12. dakk commented at 3:30 PM on May 13, 2019: none

    A C++ Enum in CAPS is bad coding style. Have you seen Cpp Core Guidelines?

    btw in this repo guidelines there are no constraint for this, and most enums use uppercase names (if we consider enum values as constants, this rule will apply "Constant names are all uppercase, and use _ to separate words.") @jonnygrant I already opened a PR for this: https://github.com/bitcoin/bitcoin/pull/16018

  13. jonnygrant commented at 9:31 AM on May 15, 2019: none

    Looks like some duplication of effort if you're making the same changes as me, after I propose a change.

    Const isn't an enum.

    I can submit my patch later. About the enum, it's kind of just common sense. The enum is not a pre-processor macro, they are the only things in all CAPS.

    https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md Enum.5: Don't use ALL_CAPS for enumerators

  14. jonnygrant commented at 9:38 AM on May 15, 2019: none

    @promag Would submit my changes, but saw see someone else duplicating my effort. Can we agree I'll implement my proposed change?

  15. jonnygrant commented at 9:53 AM on May 15, 2019: none

    I was going to make it as GetBannedLevel (not sure why it was IsBannedLevel)

  16. dakk commented at 3:57 PM on May 15, 2019: none

    @jonnygrant I wrote here I was working on it; I've no intention to duplicate work

  17. sdaftuar commented at 12:40 AM on May 17, 2019: member

    I think it's pointless to change this code around purely for style reasons; let's refactor it when the functionality is changed or extended in some way and it makes sense to adjust the implementation.

    Concept NACK.

  18. laanwj closed this on May 17, 2019

  19. 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-17 12:14 UTC

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