Variable Declaration
-
I just read a post (http://www.codeproject.com/script/comments/forums.asp?msg=1325064&forumid=1640#xx1325064xx[^]) that talks about only declaring variables at the top of the function if you are writing in C What is the basis of this? I do all my declarations at the top of my functions so that they are easy to find structurally. /jason
-
I just read a post (http://www.codeproject.com/script/comments/forums.asp?msg=1325064&forumid=1640#xx1325064xx[^]) that talks about only declaring variables at the top of the function if you are writing in C What is the basis of this? I do all my declarations at the top of my functions so that they are easy to find structurally. /jason
The reason you should do it in C, is because C requires it. The reason not to do it otherwise is because it makes your code harder to read, and harder to debug. You should read 'Code Complete' to get more insight into good code style, it's an excellent book. But as a starter - the code posted created a bunch of uninitialised variables, then proceeded to give them all values. How is that useful ? He could have done the same in half the lines, thus increasing readability, and making sure that no variables existed without being given the values you wanted them to start with. Christian Graus - Microsoft MVP - C++
-
The reason you should do it in C, is because C requires it. The reason not to do it otherwise is because it makes your code harder to read, and harder to debug. You should read 'Code Complete' to get more insight into good code style, it's an excellent book. But as a starter - the code posted created a bunch of uninitialised variables, then proceeded to give them all values. How is that useful ? He could have done the same in half the lines, thus increasing readability, and making sure that no variables existed without being given the values you wanted them to start with. Christian Graus - Microsoft MVP - C++
it wasn't my code ;) I tend to declare and initialise all my variables at the top of my procedure\functions, so that I know where they all are. It really isn't a big issue because I try and keep my procedures pretty small, but I like having them at the top. I can't justify it which is why I found your statement that it shouldn't be done that way interesting Thanks for the book recommendation. I will look into it /jason
-
it wasn't my code ;) I tend to declare and initialise all my variables at the top of my procedure\functions, so that I know where they all are. It really isn't a big issue because I try and keep my procedures pretty small, but I like having them at the top. I can't justify it which is why I found your statement that it shouldn't be done that way interesting Thanks for the book recommendation. I will look into it /jason
Sorry, I worked that out later :-O Keeping your functions small is another recommendation from the book, actually. You'll probably find, as I did, that most things it recommends, you do, but it's worthwhile just to think about the reasons why. Christian Graus - Microsoft MVP - C++