Programming style...
Anytime I decide to write a C/C++ program, after about 100 lines
my code just confuses me, even with comments. I name my variables, classes, functions, as well as I can, but then I run out of good names to give them.
Can you guys tell me how you organise your code and make it more readable for yourselves?
Programming style is something that''s developed over time. Your best bet is to start looking at other peoples code and seeing what you like and dislike about it. That''s a starting point. When you get out into the industry (not just gaming but programming in general) you find that most places have diffrent coding standards they expect out of their employees. If you get a contractor job these standards will change once the job is completed. For the most part I think that most coders are still developing their style on a consistant basis. The bottom line is to make the code as understandable as possible. If this means making a variable with a name of "ThisCounterIsUsedInAllMyForLoops" rather than using "i" then go for it. Just realize that your styles going to change as you code more & more.
Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.
January 03, 2001 05:25 PM
If you are running out of variable names it might mean that you are not breaking your code up enough. Also try using two words for most of your names. Have the first word be more specific and the second be more of a type. For example, you might have a class named LinkedList. It is a list that is linked. Now if you want a list of characters you might call it characterList, it is a list with all your characters in it. mapTiles, etc.. it can help you think up better names for your stuff.
The best "rule of thumb" I''ve ever used is that no function should be more than 50 lines. I break this rule often, but my code flows more easily when I follow it.
PS: I hope you meant 1000 lines, not 100 lines. 100 lines is easily a long interview question.
PS: I hope you meant 1000 lines, not 100 lines. 100 lines is easily a long interview question.
Stoffel: He may have meant 100 lines. When you''re first programming, 100 lines can be difficult. I went to school with people who had trouble initially with simple Hello World type programs, who later became decent programmers.
Tyrian:
As said before, style develops over time. I''ve been programming for a few years, and my original style was no style at all. I absolutely hate looking back at my old code (and I was stupid enough to let someone else have the code to one of my programs I don''t support anymore. Talk about embarasing, but I digress)
You''re on the right track if you are trying to give variables descriptive names. You might want to check out The Art of Code Documentation for some good tips too.
Micah
Tyrian:
As said before, style develops over time. I''ve been programming for a few years, and my original style was no style at all. I absolutely hate looking back at my old code (and I was stupid enough to let someone else have the code to one of my programs I don''t support anymore. Talk about embarasing, but I digress)
You''re on the right track if you are trying to give variables descriptive names. You might want to check out The Art of Code Documentation for some good tips too.
Micah
Using some sort of convention will help you get going. Even if you decide to change it later, it acts as a memory aid in the meantime. As an example, here''s what I use:
class/typenames: 1st letter capitalised (example: Mylist, Creature)
variable/object instances: Lower case first (example: theCreature, nextItem)
constant variables: capitalised with underscores(example: PI, SECONDS_PER_HOUR)
function names: 1st letter capitalised (example: DoSomething(), Lookup() )
Other, more abstract concepts that might help you:
Have some sort of consistent conceptual name for boolean functions: for instance, in one of my projects, any function that returns a bool is prefixed with ''Is''. So, some example functions are:
bool IsAvailable()
bool IsLoaded()
bool IsDead()
This is a nice way of doing things, as it represents the return type of the function and still makes sense in English.
Some variable names are ''standard''. For example, ''i'' is a pretty universally accepted loop counter variable in cases where it''s obvious or unimportant what you''re counting.
Also, only declare your variables in the block where you need them. This means that you don''t have to think of original variable names all the time, as a variable declared in 1 block is not known in other blocks, and is therefore available for re-use. I use the variable name ''ch'' (not very descriptive, I know, but it is consistent...) in perhaps a thousand different functions.
I must stress, that the above are only my conventions, that other people will have their own, possibly with more justifiable reasons. But I think everyone will agree that the quicker you find a convention and stick to it, the better off you''ll be.
class/typenames: 1st letter capitalised (example: Mylist, Creature)
variable/object instances: Lower case first (example: theCreature, nextItem)
constant variables: capitalised with underscores(example: PI, SECONDS_PER_HOUR)
function names: 1st letter capitalised (example: DoSomething(), Lookup() )
Other, more abstract concepts that might help you:
Have some sort of consistent conceptual name for boolean functions: for instance, in one of my projects, any function that returns a bool is prefixed with ''Is''. So, some example functions are:
bool IsAvailable()
bool IsLoaded()
bool IsDead()
This is a nice way of doing things, as it represents the return type of the function and still makes sense in English.
Some variable names are ''standard''. For example, ''i'' is a pretty universally accepted loop counter variable in cases where it''s obvious or unimportant what you''re counting.
Also, only declare your variables in the block where you need them. This means that you don''t have to think of original variable names all the time, as a variable declared in 1 block is not known in other blocks, and is therefore available for re-use. I use the variable name ''ch'' (not very descriptive, I know, but it is consistent...) in perhaps a thousand different functions.
I must stress, that the above are only my conventions, that other people will have their own, possibly with more justifiable reasons. But I think everyone will agree that the quicker you find a convention and stick to it, the better off you''ll be.
January 04, 2001 05:28 PM
nice, but I think it is better to make functions have lowercase, just like variables. That way they can''t be confused with constructors. Also functions are kind of like variables, that just haven''t resolved yet.
I think the Java naming convention is one of the best out there; it is quite similar to Kylotan''s way of doing. btw, I also use that "is" stuff, because I find it very useful as well
One thing that might help is when you have a lot of controls to manage, you can use Visual Basic''s controls naming convention, which is quite handy:
txtText - for textboxes (EDIT)
lblLabel - for labels (STATIC)
btnButton - for buttons (BUTTON)
lstList - for listboxes (LISTBOX)
and so on... this works even better when you wrap your controls into classes.
Also, breaking your code into smaller functions might also help. This won''t have a huge impact on performance and can increase your code readability a lot.
One thing that might help is when you have a lot of controls to manage, you can use Visual Basic''s controls naming convention, which is quite handy:
txtText - for textboxes (EDIT)
lblLabel - for labels (STATIC)
btnButton - for buttons (BUTTON)
lstList - for listboxes (LISTBOX)
and so on... this works even better when you wrap your controls into classes.
Also, breaking your code into smaller functions might also help. This won''t have a huge impact on performance and can increase your code readability a lot.
---All you base are belong to us !
quote:
Original post by Lamtd
I think the Java naming convention is one of the best out there; it is quite similar to Kylotan''s way of doing.
Where could I find information on the Java naming convention? Is it in the JDK docs on java.sun.com? I actually haven''t downloaded the JDK yet so just asking.
BeS
It''s Da BOMB Baby!!!
BeSIt's Da BOMB Baby!!!. o O ~ A little nonsense now and then,is relished by the wisest men~ O o .-- Willy Wonka
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement