Skip to content

Avoid multiple for-next expressions.#1041

Merged
iphydf merged 1 commit into
TokTok:masterfrom
iphydf:avoid-multiple-for-next-exprs
Aug 4, 2018
Merged

Avoid multiple for-next expressions.#1041
iphydf merged 1 commit into
TokTok:masterfrom
iphydf:avoid-multiple-for-next-exprs

Conversation

@iphydf

@iphydf iphydf commented Aug 2, 2018

Copy link
Copy Markdown
Member

All for-loops in toxcore are of the form

for (<for-init>; <for-cond>; <for-next>) { <body> }

for-init can be a variable declaration (like int i = 0), an
assignment (like i = 0), or empty.

for-cond can be any expression.

for-next can be an assignment or a single increment/decrement
expression (like ++i or --i).

No other forms are allowed, so e.g. comma expressions in any of these are
not allowed (so no for (i = 0, j = n; ...; ++i, --j)).


This change is Reviewable

@iphydf iphydf added this to the v0.2.x milestone Aug 2, 2018

@sudden6 sudden6 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed 1 of 2 files at r1.
Reviewable status: 0 of 1 LGTMs obtained


toxcore/DHT.c, line 1709 at r1 (raw file):

        IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4 };

        for (uint32_t j = 0; j < 2; ++j) {

could use sizeof(assocs) instead of hardcoding 2 here, but it comes with it's own drawbacks, what do you think? Or move to the same style as in the other file?

@iphydf iphydf left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reviewable status: 0 of 1 LGTMs obtained


toxcore/DHT.c, line 1709 at r1 (raw file):

Previously, sudden6 wrote…

could use sizeof(assocs) instead of hardcoding 2 here, but it comes with it's own drawbacks, what do you think? Or move to the same style as in the other file?

It's sizeof(assocs) / sizeof(assocs[0]), which is a bit wordy. This is the only case throughout toxcore where we need this form of iteration over assocs, (though a pointer subtraction could replace j), because the value (0 or 1) is actually used. The formula below looks a bit convoluted and there may be a better way, but I don't want to figure that one out right now.

@sudden6 sudden6 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewable status: 0 of 1 LGTMs obtained


toxcore/DHT.c, line 1709 at r1 (raw file):

Previously, iphydf wrote…

It's sizeof(assocs) / sizeof(assocs[0]), which is a bit wordy. This is the only case throughout toxcore where we need this form of iteration over assocs, (though a pointer subtraction could replace j), because the value (0 or 1) is actually used. The formula below looks a bit convoluted and there may be a better way, but I don't want to figure that one out right now.

could be extracted to a constant const size_t assocs_len = ... which would be clear and only one additional line, I really don't like hardcoding that number here

@iphydf
iphydf force-pushed the avoid-multiple-for-next-exprs branch from 2640447 to 41f924b Compare August 3, 2018 09:35

@iphydf iphydf left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reviewable status: 0 of 1 LGTMs obtained


toxcore/DHT.c, line 1709 at r1 (raw file):

Previously, sudden6 wrote…

could be extracted to a constant const size_t assocs_len = ... which would be clear and only one additional line, I really don't like hardcoding that number here

Done.

@sudden6 sudden6 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

:lgtm_strong:

Reviewable status: :shipit: complete! 1 of 1 LGTMs obtained

All for-loops in toxcore are of the form

    for (<for-init>; <for-cond>; <for-next>) { <body> }

`for-init` can be a variable declaration (like `int i = 0`), an
assignment (like `i = 0`), or empty.

`for-cond` can be any expression.

`for-next` can be an assignment or a single increment/decrement
expression (like `++i` or `--i`).

No other forms are allowed, so e.g. comma expressions in any of these are
not allowed (so no `for (i = 0, j = n; ...; ++i, --j)`).
@iphydf
iphydf force-pushed the avoid-multiple-for-next-exprs branch from 41f924b to 7f8b29c Compare August 4, 2018 09:04
@iphydf
iphydf merged commit 7f8b29c into TokTok:master Aug 4, 2018
@iphydf
iphydf deleted the avoid-multiple-for-next-exprs branch August 4, 2018 09:28
@iphydf iphydf modified the milestones: v0.2.x, v0.2.5 Aug 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants