Generalise variable usage inside code

Posted by Shirish11 on Programmers See other posts from Programmers or by Shirish11
Published on 2012-06-09T10:32:12Z Indexed on 2012/06/09 10:47 UTC
Read the original article Hit count: 286

I would like to know if it is a good practice to generalize variables (use single variable to store all the values).
Consider simple example

 Strings querycre,queryins,queryup,querydel; 
    querycre = 'Create table XYZ ...';
    execute querycre ;
    queryins = 'Insert into XYZ ...';
    execute queryins ;
    queryup  = 'Update  XYZ set ...';
    execute queryup;
    querydel = 'Delete from XYZ ...';
    execute querydel ;

and

 Strings query; 
    query= 'Create table XYZ ... ';
    execute query ;
    query= 'Insert into XYZ ...';
    execute query ;
    query= 'Update  XYZ set ...';
    execute query ;
    query= 'Delete from XYZ ...';
    execute query ;

In first case I use 4 strings each storing data to perform the actions mentioned in their suffixes.
In second case just 1 variable to store all kinds the data.
Having different variables makes it easier for someone else to read and understand it better. But having too many of them makes it difficult to manage.

Also does having too many variables hamper my performance?

© Programmers or respective owner

Related posts about programming-practices

Related posts about variables