Good programming practice
I am programming a little game this afternoon. I''m quite a newbie and had a little question.
My temptation whilst writing a program is to put everything into functions so that my main is easy to read and is actually quite small. Is this good programming practice?
For example want to do as follows
void main()
{
welcomeplayer();
showrules();
checkcredit();
getrandomnumbers();
getuserinput();
checkinput();
calculatewinnings();
etc.
You guys get the idea. Now, the programs works absolutely fine - but is it good programming to do this?
Yes; in terms of good-looking, easy-to-maintain code, the more you break down complex functions into many simpler ones, the better.
Make sure, however, that you have a good data path. Is getuserinput() going to have to store the user input in a global variable for checkinput() to read? If so, that''s bad, and you should figure something else out.
Also note that in the case of inner loops which need to be tightly optimized, it may be better not to break things out into different functions, or at least to inline them.
Don''t listen to me. I''ve had too much coffee.
Make sure, however, that you have a good data path. Is getuserinput() going to have to store the user input in a global variable for checkinput() to read? If so, that''s bad, and you should figure something else out.
Also note that in the case of inner loops which need to be tightly optimized, it may be better not to break things out into different functions, or at least to inline them.
Don''t listen to me. I''ve had too much coffee.
Hiya,
Yes, indeed....
Putting code into nice tight maneagable functions makes code easier to maintain and to debug.
Sometimes you may have a function that is very very small and/simple, in which case you can write it as a macro instead. This helps to speed up the code a little whilst keeping it readable. Note this can be a very small speed increase...it won''t automatically turn your code into a super optimised program.
For now though I wouldn''t worry too much about macros, I just thought I''d make you aware of them in this scenario. Whilst learning it''s definately a good practice to keep things organised and clean and functions are excellent for this.
You can always look into techniques such as macro''s when you know how the language actually works in greater depth.
Hope that helps.
GCoder
Yes, indeed....
Putting code into nice tight maneagable functions makes code easier to maintain and to debug.
Sometimes you may have a function that is very very small and/simple, in which case you can write it as a macro instead. This helps to speed up the code a little whilst keeping it readable. Note this can be a very small speed increase...it won''t automatically turn your code into a super optimised program.
For now though I wouldn''t worry too much about macros, I just thought I''d make you aware of them in this scenario. Whilst learning it''s definately a good practice to keep things organised and clean and functions are excellent for this.
You can always look into techniques such as macro''s when you know how the language actually works in greater depth.
Hope that helps.
GCoder
GCoder
Sneftel - that is a good point. I am using in the program my newly found love for pointers. I am declaring the variables in main and passing by parameter where they don''t need changing and by pointer when they do need changing. Is that ok?
if you plan on passing by value and NOT changing the data, make sure to declare the parameters to be const, so in case you do change the data, the compiler will catch the error for you.
There''s nothing really wrong with breaking your code into functions for readability, except as Sneftel pointed out in tight inner loops, but remember that this is not really the reason for having functions. More important reasons for having them are encapsulation and eliminating duplicate/redundant code.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement