Ideological Programming Question...
-
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
I'm inclined to say break it up it to logical components. you never know when you end up with code reuse and if you need to refactor easier in smaller blocks.
-
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
probably. probably. maybe. go for maximum readability. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Just as a class should do one thing and do it well, a method should do one thing and do it well. I would in all probability, break it down into seperate steps. Have a look at what it does and write it down: - Colate the woo-woos, - Re-order based on wibble, - Write the output to hatstand That would be three methods (maybe four with a method to read in a woo-woo.
Panic, Chaos, Destruction. My work here is done.
I think the men in the white coats will be along soon...
-
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
I have two comments not made yet... 1. I prefer to see function size where it can be printed in its entirety on not more than two sheets of paer with the page settings of landscape and 2-pages per sheet. (below is my view of the .net CLR from experience that is NOT peer reviewed, so read it with caution) 2. What I am about to say has little bearing on your situation because you are dealing with hand-written code, but if you are doing some code-generation it is very relevant. Function size matters for performance! The CLR will use dynamic loading like any windows application or any recent OS (I believe) where each functions binary gets loaded into memory as it is executed. So, if you can break up the code-generated functions and avoid massive sized functions, you will see a performance improvement because the functions loaded could be significantly smaller than the sum total of the original un-factored function.
-
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
Well, the general rule I follow is logical clumping. I don't like long methods, even if its something as simple as populating control from a class member, I'll break that method into several methods if I have to so long as there are reasonable sized clumps of logic wrapped together. I might end up with 3 methods instead of 1 but instead of a whopping 200 line method I have 2 that are less than 50 and one that's about 100. Much more readable that way. But that's just me. I'm very OC when it comes to code readability and structure (I'm the sort that uses #region/endregion quite a bit)
If the post was helpful, please vote! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?
-
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
Clean Code[^] covers this rather nicely: Split up large methods into well named smaller methods. It is somewhat Java centric, but is still a good read.
-
ToddHileHoffer wrote:
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere?
Yes. Avoiding code duplication is not the only reason for introducing methods. Conceptual abstraction is another. 200-250 lines is almost certainly too much for a single method. The OO philosophy is to have fairly short methods (even in procedural code methods should not be too long). However, the key measure is complexity, e.g., cyclomatic complexity. Do you have any utilities that can measure this? If you're doing .NET then try running it against Reflector + code metrics plugin (or using VS's code metrics feature if you're in Team Systsem). Anyway you should aim for a cyclomatic complexity measure of 10 or less. If your method is largely linear code with few control structures you might be able to have a relatively long method with low complexity but this is unusual.
Kevin
Kevin McFarlane wrote:
If you're doing .NET then try running it against Reflector + code metrics plugin
I'll have to take a look at this... I didn't even know reflector had a plugin engine. Are there any others I should take a look at?
It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains. -- Pride and Prejudice and Zombies
-
I think the men in the white coats will be along soon...
You write an abstract specification with three syntactical variables then! ;P
Panic, Chaos, Destruction. My work here is done.
-
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
I would only let a method get this long if it's a big long sequence of simple statements such as initialising all the controls on a form to their default states or setting a huge list of properties based on user input or a return value from a soap request or equivalent. If a function has loads of ifs and loops etc i would break it down into private / protected functions that carry out discreet chunks of functionality regardless of whether they will be reused or not and call them from one single method.
-
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
ToddHileHoffer wrote:
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere?
Yes.
ToddHileHoffer wrote:
The program I am working on has one method that will probably be about 200-250 lines of code.
I have bigger.
ToddHileHoffer wrote:
Is too much for a single method? Should I create a couple helper methods just to make it more readable?
Yes.
Don't attribute to stupidity what can be equally well explained by buerocracy.
My latest article | Linkify!| FoldWithUs! | sighist -
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
... without being flamed. Of course it's an "Ideological Programming Question".. pretty clever way to sneak a programming question into the Lounge :) And IMHO, always break thing up into small chunks - easier to read, and even if reuse doesn't seem apparent now, it may someday.
-
... without being flamed. Of course it's an "Ideological Programming Question".. pretty clever way to sneak a programming question into the Lounge :) And IMHO, always break thing up into small chunks - easier to read, and even if reuse doesn't seem apparent now, it may someday.
Well, it is language agnostic. I guess I could have posted the question in Design and Architecture but I figured not that many people would read it. I also thought most people reading the lounge would have an opinion and it would be interested in the topic.
I didn't get any requirements for the signature
-
Kevin McFarlane wrote:
If you're doing .NET then try running it against Reflector + code metrics plugin
I'll have to take a look at this... I didn't even know reflector had a plugin engine. Are there any others I should take a look at?
It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains. -- Pride and Prejudice and Zombies
See .NET Reflector Add-Ins[^] I've also used Graph. But for code metrics I mostly use the real-time tool that comes with CodeRush/Refactor! Pro (updates complexity graphs dynamically as you edit the code) and serves as a visual cue to when you should refactor.
Kevin
-
ToddHileHoffer wrote:
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere?
Yes.
ToddHileHoffer wrote:
The program I am working on has one method that will probably be about 200-250 lines of code.
I have bigger.
ToddHileHoffer wrote:
Is too much for a single method? Should I create a couple helper methods just to make it more readable?
Yes.
Don't attribute to stupidity what can be equally well explained by buerocracy.
My latest article | Linkify!| FoldWithUs! | sighistpeterchen wrote:
I have bigger.
Programmer's "Method Envy"?
If the post was helpful, please vote! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?
-
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
-
peterchen wrote:
I have bigger.
Programmer's "Method Envy"?
If the post was helpful, please vote! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?
Envy? Pride! ;P
Don't attribute to stupidity what can be equally well explained by buerocracy.
My latest article | Linkify!| FoldWithUs! | sighist -
But then you haven't really done anything. Everything should be in one function that is called by
main()
. "Look at all this program does with just one line of code." Now we're getting somewhere!BDF People don't mind being mean; but they never want to be ridiculous. -- Moliere
-
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
-
Well, it is language agnostic. I guess I could have posted the question in Design and Architecture but I figured not that many people would read it. I also thought most people reading the lounge would have an opinion and it would be interested in the topic.
I didn't get any requirements for the signature
ToddHileHoffer wrote:
I guess I could have posted the question in Design and Architecture
Nah - I'm cool, no complaints here. I just thought it funny that the Lounge states clearly "No programming questions", and of course, the Subject of your post was exactly that - err, with the addition of Ideological. I think you're correct about the Design and Architecture forum - I personally have never visited. Take care
-
Would you break up a method into more then one method just to make it smaller even if the code is not going to be used elsewhere? The program I am working on has one method that will probably be about 200-250 lines of code. Is too much for a single method? Should I create a couple helper methods just to make it more readable?
I didn't get any requirements for the signature
IMHO 200 lines of code is at least 10 times the size a method should be if you want it to be testable. Make it testable, and maintainable will follow quite happily. :)
Anna :rose: Having a bad bug day? Tech Blog | Anna's Place | Tears and Laughter "If mushy peas are the food of the devil, the stotty cake is the frisbee of God"