Skip to content

Conversation

@kcalvinalvin
Copy link
Contributor

@kcalvinalvin kcalvinalvin commented May 18, 2020

Rationale: Addresses #19000
Some functions should be returning std::string instead of const char*.
This commit changes that.

Main benefits/reasoning:

  1. The functions never return nullptr, so returning a string makes code at call sites easier to review (reviewers don't have to read the source code to verify that a nullptr is never returned)
  2. All call sites convert to string anyway

@maflcko
Copy link
Member

maflcko commented May 18, 2020

Concept ACK. It would be nice to motivate the change in the opening post a bit:

  • The functions never return nullptr, so returning a string makes code at call sites easier to review (reviewers don't have to read the source code to verify that a nullptr is never returned)
  • All call sites convert to string anyway

@kcalvinalvin
Copy link
Contributor Author

Concept ACK. It would be nice to motivate the change in the opening post a bit:

* The functions never return nullptr, so returning a string makes code at call sites easier to review (reviewers don't have to read the source code to verify that a nullptr is never returned)

* All call sites convert to string anyway

Thanks for the comment! Added those bits in the post

@maflcko
Copy link
Member

maflcko commented May 18, 2020

Also you can leave out the listing of files that you changed and what changes you made to those files. This is clear from reading the diff/looking at the changed files.

@kcalvinalvin
Copy link
Contributor Author

Also you can leave out the listing of files that you changed and what changes you made to those files. This is clear from reading the diff/looking at the changed files.

Ok. Should I take those out or just note for future PRs?

@maflcko
Copy link
Member

maflcko commented May 18, 2020

Both a suggestion for future PRs as well as this one, because the pull request description will end up in the merge commit, if this is merged.

@kcalvinalvin
Copy link
Contributor Author

Done :)

Copy link
Contributor

@practicalswift practicalswift left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK modulo nits

Welcome as a contributor @kcalvinalvin :)

@hebasto
Copy link
Member

hebasto commented May 18, 2020

Concept ACK.

@kcalvinalvin kcalvinalvin force-pushed the replace-char-with-string branch from 81d847c to a0da974 Compare May 18, 2020 16:04
@kcalvinalvin
Copy link
Contributor Author

Addressed GetTxnOutputType() in the new commit as requested here

@kcalvinalvin kcalvinalvin force-pushed the replace-char-with-string branch from a0da974 to 23610aa Compare May 18, 2020 16:48
@sipa
Copy link
Member

sipa commented May 18, 2020

It's unfortunate to make these functions return an std::string by value, as it implies a string copy every time they are called. This could be avoided by having a static const std::string inside the function for every value, and returning them as const std::string&.

@maflcko
Copy link
Member

maflcko commented May 18, 2020

All callers put the return value into a string anyway. So return value optimization (or whatever it is called) should take care of this and make it a no-op, no?

Going forward, with C++17 they can be made constexpr string views if performance is really a concern.

@tryphe
Copy link
Contributor

tryphe commented May 18, 2020

Concept ACK

@sipa
Copy link
Member

sipa commented May 18, 2020

@MarcoFalke That's a good point - this PR isn't making anything worse in that case. It could be improved later, but no need add extra complexity for that now.

@kcalvinalvin kcalvinalvin force-pushed the replace-char-with-string branch from 23610aa to bdd9cb6 Compare May 18, 2020 18:32
Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK bdd9cb6a9bc314400ad655a45ee32e03756468d1, compiled on Linux Mint 19.3 with GCC and Clang.

@maflcko
Copy link
Member

maflcko commented May 20, 2020

ACK bdd9cb6a9bc314400ad655a45ee32e03756468d1 🥑

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK bdd9cb6a9bc314400ad655a45ee32e03756468d1 🥑
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUhkggv/dG77SPx7h89oenzVpqi337dajwuIwkr5AzDinyxCC+s+ZuWqRXM84r5Y
Kp2dppe4rvo5XnR6EYPgdGsICBt1tEarppmtEExnYrUfB7UB2PSyqfZ+A1juurnj
BTzRo019nO/RLavDVqEq/aqSa8fW8mkTu2gO95XLXsWFONojQAe0qRrMCwAAiviB
r54j6ApH2mJ+nllZqiI9yQxztUVFbIVWqZai+GIcy/wLMS6b6ssTMGH4y6NT1Ob4
O1t7eNENIZLv4RnEddCckA24rxVDOPE49ArD62E/yZn/9uzHfC+ZaFcCTyZ9eH12
h3bexZOOcnQes1nutjLYNVvG6+HAmaC3EjZuZhnX6wFvMUvrUzpCvbTe0WqRDy0u
GVER/1LkC0o3I+/xUADGzOutgKbviv8nqP1/fjdfkRXSbEMJ0KUknT6GYxbEADBu
NYf1BZdrK1hOmBQlO4N4upbt/UsnldngjW1xUPnCMtp9Ao+0le6qwzzZGC25qav6
vMt9IqV2
=535T
-----END PGP SIGNATURE-----

Timestamp of file with hash 59bb0b7e1b6d4de434d0c1c688938d18335cc4075bc7302fd2f2754cdc746917 -

@laanwj
Copy link
Member

laanwj commented May 21, 2020

ACK on the changes.

Can you please reformat your commit message though to put an empty line between the title and the body? Tools such as git log --pretty=oneline currently merge the entire description into the title, which is messy:

bdd9cb6a9bc314400ad655a45ee32e03756468d1 (pull/19004/head) script: Replace const char* to std::string Some functions should be returning std::string instead of const char*. This commit changes that.

@hebasto
Copy link
Member

hebasto commented May 21, 2020

Can you please reformat your commit message though to put an empty line between the title and the body?

... and replace script: prefix with refactor: ?

Some functions should be returning std::string instead of const char*.
This commit changes that.
@kcalvinalvin kcalvinalvin force-pushed the replace-char-with-string branch from bdd9cb6 to c57f03c Compare May 21, 2020 16:41
@kcalvinalvin kcalvinalvin changed the title script: Replace const char* to std::string refactor: Replace const char* to std::string May 21, 2020
@kcalvinalvin
Copy link
Contributor Author

ACK on the changes.

Can you please reformat your commit message though to put an empty line between the title and the body?

Done :)

... and replace script: prefix with refactor: ?

Also Done :)

Also changed the GitHub PR title from script: prefix to refactor:

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK c57f03c

@maflcko
Copy link
Member

maflcko commented May 21, 2020

re-ACK c57f03c (no changes since previous review) 🚃

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

re-ACK c57f03ce17 (no changes since previous review) 🚃
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUg5Ugv/RLY5cmEYDu4MGRx80v7YSnQnpIxJ2l2+ls+NQLYL6wCj5oOG/g67mSvW
UCmzq51vj0FRhZJ+Kz54WUpHlizyWaettFcpytCug+O5u+7iWU5xwusrZTZxIIMq
hySm3NdHELk47dI2BTYAYtosJfEizyee/FimSovaWbokufmybwxXsjkMwz+NfeGv
D5qn0dh04Vs9y9GiN+9eSLMZn2b6tt7Wv5ryNjb+pNI7DzOfL2wiRqwGGRp+dAmO
cNcRzVxqmDXj+a23kiqMB7v9QujylftRsNnJ9gRXhNUDMxdGFRMG+tqcFKSWs/cU
3SI/n5y2ostgpVdFKaeKP56fiut0YUxKSrvlCZah3QIiEKMPiafILz1q8naN+Z5J
FxwIV0gpUBewkvBNv8Mmbi3c7bHAgNXHDr9XpWYq2N0wVgMjzv+bf2p55PlLi6HC
oU1IGMAMBSfLAzjxbhMQH1GBSdRX2NKqADGip4YlaXYiX4gYg8QQ/F9RPrVK1xZS
ohz+lirt
=GHzP
-----END PGP SIGNATURE-----

Timestamp of file with hash d852b864158d4b33654116fe600d925926dc1f39c2c521efb25067754e5f918d -

@Empact
Copy link
Contributor

Empact commented May 26, 2020

~0 I like all of these except GetOpName, which could return nullptr in place of "OP_UNKNOWN" - seems reasonable to minimize processing in the unknown case:
master...Empact:replace-char-with-string

@maflcko
Copy link
Member

maflcko commented May 26, 2020

Given that none of the call sites checked for nullptr of the char array, I'd find it very dangerous to re-introduce this sharp edge. Also for optional return values, we generally use Optional. In any case, I think this can be discussed in a follow-up pull request.

@Empact
Copy link
Contributor

Empact commented May 26, 2020

Fair enough, Code Review ACK c57f03c

@practicalswift
Copy link
Contributor

ACK c57f03c -- patch looks correct

@maflcko maflcko merged commit 9ccaee1 into bitcoin:master May 27, 2020
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request May 27, 2020
c57f03c refactor: Replace const char* to std::string (Calvin Kim)

Pull request description:

  Rationale: Addresses bitcoin#19000
  Some functions should be returning std::string instead of const char*.
  This commit changes that.

  Main benefits/reasoning:

  1.  The functions never return nullptr, so returning a string makes code at call sites easier to review (reviewers don't have to read the source code to verify that a nullptr is never returned)
  2. All call sites convert to string anyway

ACKs for top commit:
  MarcoFalke:
    re-ACK c57f03c (no changes since previous review) 🚃
  Empact:
    Fair enough, Code Review ACK bitcoin@c57f03c
  practicalswift:
    ACK c57f03c -- patch looks correct
  hebasto:
    re-ACK c57f03c

Tree-SHA512: 9ce99bb38fe399b54844315048204cafce0f27fd8f24cae357fa7ac6f5d8094d57bbf5f5c1f5878a65f2d35e4a3f95d527eb17f49250b690c591c0df86ca84fd
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Feb 2, 2021
Summary:
```
Some functions should be returning std::string instead of const char*.
This commit changes that.
```

Backport of core [[bitcoin/bitcoin#19004 | PR19004]].

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, PiRK

Reviewed By: #bitcoin_abc, PiRK

Differential Revision: https://reviews.bitcoinabc.org/D9124
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
@kcalvinalvin kcalvinalvin deleted the replace-char-with-string branch July 13, 2022 07:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate where std::string should be returned instead of "const char*"

9 participants