Jump to content
  • Advertisement
Sign in to follow this  
moeen k

best way to write clean code

This topic is 937 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi.

maybe you say its a question about general programming but i think it mostly happens for gameplay programing.

sometimes i want to implement a gameplay. after a while i find out that there is better way to do that. sometimes i save a part of code and write new ones. some times i want to keep old code and add my new codings and....

after a while i find a code that works well but its dirty. i dont know really my gameplay working with which part of code and there is no way to manage it.

 

i want to know what is best way to work in this kind of programming and keep things clean.

 

thank you forhelping

Share this post


Link to post
Share on other sites
Advertisement


The less clever but wiser engineer instead spends 10 minutes to just make the function:
 
int calc_final_damage(int dmg, Player const& state) {
if (state.IsPoisoned())
return dmg * 2;
else if (state.HasArmor())
return dmg / 2;
else
return dmg;
And if the design changes more than a couple times, the wise engineer just moves that out into a Lua hook so the designers can change it all on their very own.

 

Brilliant. I am ashamed to say I would be guilty of implementing the elaborate system. Sometimes thinking ahead can lead you down the wrong path.

Share this post


Link to post
Share on other sites

Sorry for my bad English.


Your English is fine, as far as I can tell, except for this:

2. If I would like to comment a code I delete it.


I would have said "If I would like to comment out some code, I delete it."

"Commenting" means writing comments in your code and, if done judiciously, can be a good thing. "Commenting out" is what you meant.

Share this post


Link to post
Share on other sites

sometimes i save a part of code and write new ones. some times i want to keep old code and add my new codings and....
after a while i find a code that works well but its dirty. i dont know really my gameplay working with which part of code and there is no way to manage it.

 

 

i use a file called junk.cpp

 

when commented out code starts to fill up the source file, i cut and paste it to junk.cpp.  so i still have it if i need it, but its no longer cluttering up the source file.

 

right now, caveman.cpp has 118,000 lines of code.

 

and the junk.cpp file for caveman has an additional 32,000 lines of commented out code.

 

its been a while since i cleaned out commented code from caveman.cpp, so perhaps 10% is also turned off, IE another 11,000 lines or so of code.

 

based on that, it would seem that i only use about 70% of the code i write, and the other 30% gets deprecated.

 

 

 

 

 

other things to do: (already mentioned by others)

 

modularity

 

generic low level routine vs 2 high level routines with copy/paste/edit code.   easier to maintain - perhaps a bit more work upfront though.

 

always pseudocode first (at least in your head). you should not even touch the editor until you know what to write. design first - then coding become nothing more that what it actually is, grunt work data entry / glorified word processing.

 

 

 


The less clever but wiser engineer instead spends 10 minutes to just make the function:

 

come on Sean! <g> how about more like ten seconds for something trivial like that.  and please tell me the newbies don't build "systems" for this kind of trivial stuff!  <g>

 

 

 


Sometimes thinking ahead can lead you down the wrong path.

 

way wrong!

 

thinking ahead can prevent you going down the wrong path.

 

premature over-engineering is what can lead you astray.

 

remember: a little knowledge is a dangerous thing. never undertake a task with a little knowledge.  get the full knowledge first.   you'll find things are easier, simpler, and cleaner, and you re-design and refactor less, cause you're creating more-correct code from the get-go.

 

and of course, the K.I.S.S. principle:   Keep It Stupid Simple.

 

always start with the easiest to implement algo that might be fast enough. only get fancy as needed.  

 

the coding equivalent of Occam's Razor can also be used:   given two algos that both work, always choose the simpler.

 

another thing that can help keep code organized is to split the code into modules based on function, such as graphics, audio, gameplay (main loop etc), AI, and so forth. 

you may still be dealing with spaghetti code, but now at least its a smaller plate of spaghetti.

 

from your overall statements, it seems you implement systems without first researching all possible methods then simply implementing the best one, and getting on with life. it also seems you tend to over-engineer things. you may also be implementing systems without full knowledge of how they work - thus the code seems alien and confusing. and it seems you have a ton of deprecated code cluttering things up. the combo of the above would certainly lead to the circumstances you describe.

Edited by Norman Barrows

Share this post


Link to post
Share on other sites

The ability to craft solid designs and architecture is something you gain with experience.

 

Do the best you can at the time you do it. Learn from it. Also read about how others solved similar problems. Next time you will remember, "I did it this way and it didn't work out well, I should study up on alternatives to this", or "I did it that way and it worked really well for me, I will use that pattern again."

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!