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!
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.
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.
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"
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.
Comments 13
Reply
Reply
Reply
Reply
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
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