Coding practices

Nov 07, 2005 18:47

Chime in...I am interested on your thoughts...

Which coding method do you prefer? Put every little logical routine into a sub function (example a), or write code into the main (example b)

Examples )

coding

Leave a comment

Comments 13

jlipeles November 7 2005, 23:45:38 UTC
As I code more and more, especially in Java, I do A. In fact, now I always do A. Had to get away from the PHP (scripting) mentality. Hahaha... Greg's gonna come after me for that one. Muhahahaha!

Reply


toddito November 8 2005, 05:57:15 UTC
Just to be a troublemaker, I'm gonna go with B.

Reply


arichi November 8 2005, 09:32:54 UTC
A. It's not only easier to debug and read, it's also easier to write - if you need a logical subroutine, you just assume it's there and go back to fill it in. If you forget to do so, the compiler will remind you of your obligation.

Reply

clayemore November 8 2005, 09:51:54 UTC
I actually write the code as B...then I take what I just wrote, cut and paste and create the subroutine. Usually, every once ina while I know to make the routine before writing what it actually does.

Reply

arichi November 8 2005, 09:53:26 UTC
This works too. At least you go through the trouble of making it a different routine. On behalf of anyone following you on the project, thank you.

I guess a PSA to anyone reading this - if you find yourself copying and pasting code, possibly changing one or two things, this screams "make it a function"

Reply


maniakes November 8 2005, 22:18:28 UTC
I rarely put any serious code directly in main unless the entire program is only a few dozen lines.

In real-world production code, the most common model I've seen is to use main only to instantiate a few objects. The constructors of the object perform initialization tasks, and wire the objects up to receive input.

If you're asking a more general question about how fine to chop up functions, I'll echo arichi's comment about the copy-and-paste rule of thumb.

Reply


Leave a comment

Up