Bloomfilter: parameter variables made constant #9750

pull rohundhar wants to merge 2 commits into bitcoin:master from rohundhar:bloomVar changing 2 files +8 −8
  1. rohundhar commented at 5:42 AM on February 13, 2017: none

    Just a suggestion, since I see none of the variables I've transformed into constants are altered in their respective methods. If changes want to implemented later on so they can be altered, it would be an easy fix but I feel the code is cleaner this way.

    let me know your thoughts!

  2. param variables made const 64aa36e203
  3. JeremyRubin commented at 8:00 PM on February 13, 2017: contributor

    utACK 64aa36e

  4. gmaxwell approved
  5. gmaxwell commented at 8:29 PM on February 13, 2017: contributor

    utACK

  6. laanwj commented at 12:22 PM on February 14, 2017: member

    Why would you make non-reference and non-pointer variables const?

    Please first add this and the rationale to doc/developer-notes.md. I don't see a point of doing this out of the blue, sorry.

  7. JeremyRubin commented at 12:33 PM on February 14, 2017: contributor

    I find it useful when reading code if anything that can be const'd is const'd.

    to quote S.O.:

    The const qualifier prevents code inside the function from modifying the parameter itself. When a function is larger than trivial size, such an assurance helps you to quickly read and understand a function. If you know that the value of side won't change, then you don't have to worry about keeping track of its value over time as you read. Under some circumstances, this might even help the compiler generate better code.

    A non-trivial number of people do this as a matter of course, considering it generally good style.

    http://stackoverflow.com/questions/8714250/isnt-const-redundant-when-passing-by-value

    There is no harm to making this change, and at best, there is the gain that the above sorts of bug won't later be introduced. Also, in theory such an annotation could help the optimizer better inline such a function.

  8. fanquake added the label Refactoring on Feb 14, 2017
  9. laanwj commented at 1:41 PM on February 14, 2017: member

    I agree. However we don't follow everything that is "generally considered good style" by random people on the internet in this project. We most notably don't do this structurally anywhere else, so why start in the bloom filter code specifically?

    If you want to introduce this rule, that's fine, I have no reason to be against it, but that needs to be discussed first in a PR adding that recommendation to the developer guide, then merged. Then you can start changing existing code to do that.

  10. jtimon commented at 5:09 PM on February 14, 2017: contributor

    utACK 64aa36e . Although as @laanwj points out, we're not doing this anywhere and we shouldn't go crazy trying to change it everywhere at once. If we prefer not to do this for whatever reason, I have no strong opinion.

  11. rohundhar commented at 4:55 AM on February 15, 2017: none

    Thanks for the discussion. It's true that this standard isn't enforced anywhere else, so i can see how it seems out of the blue. A recent project of mine used bloom filters and so I was interested to check out bitcoin's filter and thought it wouldn't hurt to apply some of the standards i used which I thought were helpful.

    I agree, it is kind of random so I'll look more into if I can find a more reasonable argument for the change and then make an attempt on the developer notes.

  12. kallewoof commented at 11:18 AM on February 15, 2017: member

    I agree that making things const that can be made const is a good idea, taking the virality property as mentioned in Google's C++ styleguide (Use of const) into appropriate consideration.

    I also agree that the developer guidelines should include this direction.

  13. laanwj commented at 12:37 PM on February 15, 2017: member

    One thing I don't like about making by-value parameters const is that something that is private and internal to the function - how their copy of the parameter is handled - needs to be specified on the interface, even though it makes no difference to the ABI. That's a minor problem though and if it's done consistently everywhere I have no problem with it.

    Edit: something else that we need to be clarify in the coding style is the use of "override". E.g. I had to add a few in #9764 to prevent gcc from complaining: it doesn't like when it's used inconsistently within a class.

  14. kallewoof commented at 2:14 AM on February 16, 2017: member

    I didn't even know "override" existed. As for how parameters are handled, I'm of the opinion that function/method parameters should never be changed ever, unless they are explicitly marked as "output parameters", even for primitive types like ints and such, where it doesn't matter.

    The primary reason is that it's much easier to read code where the input parameters always stay the same no matter what. I even prefer the following

    void foo(const int inX) {
       int x = inX ?: computeStartingX();
       // ...
    }
    

    over

    void foo(int x) {
       if (!x) x = computeStartingX();
       // ...
    }
    

    which is one of the few "ugly" cases with this approach.

  15. merge with bitcoin core d60d54ddb3
  16. laanwj merged this on May 18, 2017
  17. laanwj closed this on May 18, 2017

  18. laanwj referenced this in commit 2acface32a on May 18, 2017
  19. PastaPastaPasta referenced this in commit 00846ac946 on Jun 10, 2019
  20. PastaPastaPasta referenced this in commit a8e53e57c7 on Jun 11, 2019
  21. PastaPastaPasta referenced this in commit 45e6e562cf on Jun 11, 2019
  22. PastaPastaPasta referenced this in commit 95852f6776 on Jun 15, 2019
  23. PastaPastaPasta referenced this in commit a445e230ca on Jun 19, 2019
  24. PastaPastaPasta referenced this in commit 227a869ca1 on Jun 19, 2019
  25. PastaPastaPasta referenced this in commit ef87d7e8c3 on Jun 19, 2019
  26. PastaPastaPasta referenced this in commit 378da47ab6 on Jun 19, 2019
  27. PastaPastaPasta referenced this in commit 0cd7b3b536 on Jun 19, 2019
  28. PastaPastaPasta referenced this in commit ef653499db on Jun 20, 2019
  29. PastaPastaPasta referenced this in commit f4dc41ded1 on Jun 22, 2019
  30. PastaPastaPasta referenced this in commit e5d86cd857 on Jun 22, 2019
  31. PastaPastaPasta referenced this in commit c8cc659af1 on Jun 22, 2019
  32. PastaPastaPasta referenced this in commit 8593c1125a on Jun 22, 2019
  33. PastaPastaPasta referenced this in commit c3196eda8b on Jun 22, 2019
  34. PastaPastaPasta referenced this in commit 895c95cd90 on Jun 24, 2019
  35. jasonbcox referenced this in commit 9f9585ec29 on Jun 28, 2019
  36. jtoomim referenced this in commit ed010bf6ee on Jun 29, 2019
  37. jonspock referenced this in commit 159aeaaff4 on Jul 6, 2019
  38. jonspock referenced this in commit 0d0788c456 on Jul 7, 2019
  39. jonspock referenced this in commit 68a6912d8d on Jul 7, 2019
  40. proteanx referenced this in commit c2324f52e5 on Jul 7, 2019
  41. jonspock referenced this in commit 28b1a99901 on Jul 9, 2019
  42. barrystyle referenced this in commit e58adb2cfe on Jan 22, 2020
  43. zkbot referenced this in commit be459619a8 on Mar 5, 2021
  44. zkbot referenced this in commit 78de0cdf46 on Apr 15, 2021
  45. DrahtBot locked this on Sep 8, 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 06:15 UTC

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