abd_ka's blog

By abd_ka, history, 5 months ago, In English

Are block scopes a good practice in CP ?

What i mean by block scope:

I am not sure if this is the correct naming it, but i will try to explain what i mean by block scope.

A block scope is a scope which isn't created for a function of a loop (for/while), rather a scope which is created by just typing {...} and then code inside it.

int main() { FAST
    int n = 10;
    { // this is a block scope
        int n = 100; 
        cout << n; //this will output "100"
    }
    cout << n; // this will output "10"
}

Why would you ever use this ?

a good example which comes to my mind (which i have used many times before) is when you need to perform two bfs/Dijkstra.

so instead of defining 2 queues, you just define one inside a block scope, and the other inside another block scope. this makes the implementation a bit easier.

what i am asking about

. Does this makes the code slower for any reason ?

. Why doesn't advanced CP'ers use this ?

i am keen to hear what you think.

thank you for reading.

Full text and comments »

  • Vote: I like it
  • +31
  • Vote: I do not like it