Thread: [Dev-C++] Programming Form/Style
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Trevor C. <TCc...@ya...> - 2001-02-25 22:03:26
|
Hi, I am new to this list and very new to C++ and programming in general, and I am wondering: My friend, who is also learning C++, told me that it is bad programming form to declare class variables and functions public, which I can understand for the sake of reusability and simplicity. However, he also told me it was bad form to let Main() do too much work and that you should send as many tasks as you can to different functions... Is this so? Why? I can see why for more complex functions, but what about really simple things. For example, should I even write simple output functions to keep any "cout" lines out of main()? Thanks for any help, Trevor Cook |
|
From: Ioannis V. <no...@ya...> - 2001-02-25 22:43:45
|
main() is best to be used as the driver piece of the program with the rest things implemented in functions. Now if you have many couts (e.g. a menu) it is best implemented in a separate function. If you have 1-2 couts to test the functionality of the program place them in main(). Ioannis * Ioannis Vranos * Programming pages: http://members.nbci.com/noicys * Alternative URL: http://run.to/noicys > -----Original Message----- > From: dev...@li... > [mailto:dev...@li...]On Behalf Of Trevor Cook > Sent: Monday, February 26, 2001 12:10 AM > To: dev...@li... > Subject: [Dev-C++] Programming Form/Style > > > Hi, > > I am new to this list and very new to C++ and programming > in general, and I am wondering: > > My friend, who is also learning C++, told me that it is bad > programming form to declare class variables and functions public, > which I can understand for the sake of reusability and > simplicity. However, he also > told me it was bad form to let Main() do too much work and that > you should send as many tasks as you can to different > functions... Is this so? Why? I can see why for more complex > functions, but what about really > simple things. For example, should I even write simple output > functions to keep any "cout" lines out of main()? > > > Thanks for any help, > Trevor Cook > > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Jared E. <ja...@vo...> - 2001-02-25 23:05:12
|
Your friend is steering you in the right direction. You do not need to go overboard and have everything in functions. As the programmer, it is up to you to decide if a function can better suit the situation. The overall concept to think about is code reuse. For example, think about a function that adds 2 numbers. You could declare this function as a void and just have it cout the result, or you could declare the function as an int and have it return the result and do the necessary cout in the main. - Jared At 03:10 PM 2/25/2001 -0700, you wrote: >Hi, > > I am new to this list and very new to C++ and programming in > general, and I am wondering: > > My friend, who is also learning C++, told me that it is bad > programming form to declare class variables and functions public, which I > can understand for the sake of reusability and simplicity. However, he also >told me it was bad form to let Main() do too much work and that you should >send as many tasks as you can to different functions... Is this >so? Why? I can see why for more complex functions, but what about really >simple things. For example, should I even write simple output functions >to keep any "cout" lines out of main()? > > > Thanks for any help, > Trevor Cook > > >_______________________________________________ >Dev-cpp-users mailing list >Dev...@li... >http://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
|
From: Vassili <v.v...@bt...> - 2001-02-26 20:21:25
|
I'm just finishing a course on 'C' before I move onto 'C++' and I've found that collecting functions into a library helps me stop repeating the same functions over and over again. As an example, lets say you create a function to uppercase the letters of a string, you really don't want to keep writing the same function over and over again in different programs. Not only that, it's easier to debug and read your code, changing things in the main() could cause other errors, but change the code in a function and you only affect the code in that function. So I would say listen to your friend, he's come across these problems like I have and is talking from experiance. ----- Original Message ----- From: "Jared Eckersley" <ja...@vo...> To: <dev...@li...> Sent: Sunday, February 25, 2001 11:03 PM Subject: Re: [Dev-C++] Programming Form/Style > Your friend is steering you in the right direction. You do not need to go > overboard and have everything in functions. As the programmer, it is up to > you to decide if a function can better suit the situation. The overall > concept to think about is code reuse. For example, think about a function > that adds 2 numbers. You could declare this function as a void and just > have it cout the result, or you could declare the function as an int and > have it return the result and do the necessary cout in the main. > - Jared > > At 03:10 PM 2/25/2001 -0700, you wrote: > >Hi, > > > > I am new to this list and very new to C++ and programming in > > general, and I am wondering: > > > > My friend, who is also learning C++, told me that it is bad > > programming form to declare class variables and functions public, which I > > can understand for the sake of reusability and simplicity. However, he also > >told me it was bad form to let Main() do too much work and that you should > >send as many tasks as you can to different functions... Is this > >so? Why? I can see why for more complex functions, but what about really > >simple things. For example, should I even write simple output functions > >to keep any "cout" lines out of main()? > > > > > > Thanks for any help, > > Trevor Cook > > > > > >_______________________________________________ > >Dev-cpp-users mailing list > >Dev...@li... > >http://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |