Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created September 14, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanplopes/f0b3743a1ebfcca48293 to your computer and use it in GitHub Desktop.
Save juanplopes/f0b3743a1ebfcca48293 to your computer and use it in GitHub Desktop.

Remember

This is ok:

for(int i = 1; i <= floor(sqrt(n)); i++) {
    //code
}

This is good:

for(int i = 1; i * i <= n; i++) {
    //code
}

This is better:

int limit = floor(sqrt(n));
for(int i = 1; i <= limit; i++) {
    //code
}
@pbalduino
Copy link

This is a little better

const int limit = floor(sqrt(n));
for(int i = 1; i <= limit; i++) {
    //code
}

@juanplopes
Copy link
Author

@pbalduino Not sure if I agree. I mean, it's rather easy for the compiler to figure out which local variables change and which don't. I think const should only be used to ensure const-correctness in interactions. For local variables, the only interaction I see is passing it by reference to another method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment