Skip to content

Conversation

@bytting
Copy link
Contributor

@bytting bytting commented Jul 21, 2017

Replace traditional for with ranged for in block and transaction primitives to improve readability

for (unsigned int i = 0; i < vtx.size(); i++)
{
s << " " << vtx[i]->ToString() << "\n";
for (const auto &tx : vtx) {
Copy link

Choose a reason for hiding this comment

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

Nit: const auto& tx seems to be the normal way to format it.

Copy link
Contributor Author

@bytting bytting Jul 24, 2017

Choose a reason for hiding this comment

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

Yeah, I was unsure about that. It seems counter intuitive to me since it is the variable that is a reference, not the type.

Consider this case:

int *a, *b, &c = value;

I have seen both being used in this code base, so I think I'll leave it unless there is more protest.

Thanks though

Copy link

Choose a reason for hiding this comment

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

I did check, auto& is much more common in the codebase (60 times vs. sth. like 5 times).

It seems counter intuitive to me since it is the variable that is a reference, not the type.

It's actually the type. It's apparent if you look at function signatures.

void foo(const std::string&); is a valid function signature (first argument is a reference type).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, when I look at your last patch #10916 you actually do the same.

first argument is a reference type

I would call that an unnamed reference to type std::string :trollface:

I guess it's not that bad for arguments and return types though, I'll look into it.

Copy link

Choose a reason for hiding this comment

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

Hm, when I look at your last patch #10916 you actually do the same.

You mean using const auto& var, not const auto &var? Makes sense that I use what I advocate :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for (const CKeyID &keyid : GetKeys()) {

Copy link

Choose a reason for hiding this comment

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

Good catch! In my defense, that line was there like that before and is one of the 5 or so instances like that I refered to earlier. I'll fix it in a few hours when I am back home.

Copy link
Contributor Author

@bytting bytting Jul 24, 2017

Choose a reason for hiding this comment

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

Okay, your argument that a reference is a distinct type is consistent with the C++ standard, so I give up. GJ

@benma
Copy link

benma commented Jul 24, 2017

utACK 72f0060

@sipa
Copy link
Member

sipa commented Jul 24, 2017 via email

@benma
Copy link

benma commented Jul 24, 2017

I guess you're right that it doesn't matter, I just thought it made sense to stick with the prelevant style in the code. Personally I put it with the type because of things like std::vector<Foo*> and func signatures.

@ryanofsky
Copy link
Contributor

Currently we have PointerAlignment: Left in our clang-format file. This puts pointers and reference symbols next to the type instead of the variable. Should probably remove the setting or change it if this style isn't actually preferred.

@sipa
Copy link
Member

sipa commented Jul 24, 2017

utACK

@laanwj laanwj merged commit 72f0060 into bitcoin:master Jul 27, 2017
laanwj added a commit that referenced this pull request Jul 27, 2017
…ansaction primitives

72f0060 Replace traditional for with ranged for in primitives (Dag Robole)

Pull request description:

  Replace traditional for with ranged for in block and transaction primitives to improve readability

Tree-SHA512: c0fff603d2939149ca48b6aa72b59738a3658d49bd58b2d4ffbc85bdb774d8d5bb808fe526fe22bb9eb214de632834d373e2aab44f6019a83c0b09440cea6528
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Aug 2, 2019
… and transaction primitives

72f0060 Replace traditional for with ranged for in primitives (Dag Robole)

Pull request description:

  Replace traditional for with ranged for in block and transaction primitives to improve readability

Tree-SHA512: c0fff603d2939149ca48b6aa72b59738a3658d49bd58b2d4ffbc85bdb774d8d5bb808fe526fe22bb9eb214de632834d373e2aab44f6019a83c0b09440cea6528
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
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.

7 participants