Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code in example/ #97

Merged
merged 9 commits into from
Jun 4, 2023

Conversation

gruenich
Copy link
Contributor

Fix warnings.
Make variables more local, e.g., just inside for loops.
Add missing headers.
Add return statements for int main(..)

@xiaoyeli
Copy link
Owner

xiaoyeli commented Jun 1, 2023

Why is is the following better?
"Make variables more local, e.g., just inside for loops."

@gruenich
Copy link
Contributor Author

gruenich commented Jun 3, 2023

Why is is the following better?
"Make variables more local, e.g., just inside for loops."

I am glad you ask this question!

  1. In the past the advice was different. Pascal, Ada, C (from 1989) all forced to declare variables at the very beginning. This has changed in Java, Python and C (since 1999) to have localized variables as much as possible.
  2. It prevents silly mistakes likes mixing up j with i, as only the right variable is defined and the compiler will complain.
  3. It is easier to read, as the variable declaration (and the type of the variable) is close to its use.

Two sources to support my claim with different arguments:

@xiaoyeli
Copy link
Owner

xiaoyeli commented Jun 4, 2023

ok, declaring variable inside the loop was considered bad in the old style. But now as long as the compiler knows not to repeatedly generate new variables, it would be fine.

@xiaoyeli xiaoyeli merged commit 9e5cf07 into xiaoyeli:master Jun 4, 2023
@gruenich gruenich deleted the feature/cleanup-examples branch June 5, 2023 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants