Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Ideological Programming Question...

Ideological Programming Question...

Scheduled Pinned Locked Moved The Lounge
questionbusiness
58 Posts 40 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T ToddHileHoffer

    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

    N Offline
    N Offline
    Nagy Vilmos
    wrote on last edited by
    #8

    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.

    M 1 Reply Last reply
    0
    • T ToddHileHoffer

      John Simmons / outlaw programmer wrote:

      Depends on the complexity. I might break it up into discrete functional components. I don't like real long functions, nor files longer than 1000 lines.

      I agree. It is of medium complexity, not too bad. But I think since some of the functionality can be separated that it probably should be.

      I didn't get any requirements for the signature

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #9

      See below, if it breaks up naturally then break it up into its natural parts. Otherwise use CodeTNT[tm].


      Panic, Chaos, Destruction. My work here is done.

      1 Reply Last reply
      0
      • T ToddHileHoffer

        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

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #10

        ToddHileHoffer wrote:

        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?

        Almost certainly yes!

        ToddHileHoffer wrote:

        My boss said he will be responsible for it and if not he'll be out of job...

        Think what that 200-250 lines might look like after he's been at it. Break it up in whatever way seems right to you, before he gets his grubby mits on it.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        1 Reply Last reply
        0
        • T ToddHileHoffer

          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

          D Offline
          D Offline
          Dave Parker
          wrote on last edited by
          #11

          Depends. If there's a part of it that can easily be separated out without requiring too many parameters, which logically makes sense as a sub-section of the main bit with a good name to indicate what it's doing then it seems to make sense. 200-250 lines does sound quite long though, so I'd be surprised if you can't find bits that make sense to separate out to improve the maintainability, even if they're not called from anywhere else. I wouldn't worry about the performance overhead of the method call - my guess is the compiler will inline it anyway and even if it doesn't it'll probably be insignificant.

          1 Reply Last reply
          0
          • T ToddHileHoffer

            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

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #12

            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?

            sure, as long as it's an obvious break. i'm not so picky about function size that i'll spend too much time trying to find ways to break down a function that gets a little long. and if it's a performance-critical function, i'll avoid function calls as best i can.

            image processing toolkits | batch image processing

            S 1 Reply Last reply
            0
            • J John M Drescher

              ToddHileHoffer wrote:

              The program I am working on has one method that will probably be about 200-250 lines of code.

              I doubt there is a single method in any of my code (I have written > 600K lines of C++) that is over 200 lines of code. Although when reading others code that we use in my current project I have seen a single method being 3000 lines of code. When I see a method that is getting over 100 lines I usually hit refactor in Visual Assist... And lately I am making a lot of use of the Pimpl Idiom so that the refactored code would probably go in the private class and not muck up the external interface.

              John

              D Offline
              D Offline
              Dave Parker
              wrote on last edited by
              #13

              I just found a 170 line method of mine. It's not exactly the easiest to maintain one. It's crammed full of anonymous methods, invocations on other threads and message posts to the end of the windows message queue. It's in a control and there are a zillion possible pathways through it depending on how the properties are set up, etc. It used to be a lot longer but I managed to separate some big chucks of it out :)

              1 Reply Last reply
              0
              • T ToddHileHoffer

                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

                R Offline
                R Offline
                Rama Krishna Vavilala
                wrote on last edited by
                #14

                ToddHileHoffer wrote:

                o more then one method just to make it smaller even if the code is not going to be used elsewhere?

                Yes. Also once you partition your code into several pieces, more often than not you can refactor and extract common functionality thereby further reducing the code.

                ToddHileHoffer wrote:

                Is too much for a single method?

                In my book anything more than 20 lines is too much for C#/Java. Note with the good IDE support it is pretty easy to break methods into smaller logical chunks.

                ToddHileHoffer wrote:

                Should I create a couple helper methods just to make it more readable?

                Yes!

                1 Reply Last reply
                0
                • T ToddHileHoffer

                  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

                  T Offline
                  T Offline
                  ToddHileHoffer
                  wrote on last edited by
                  #15

                  I think it is worth a little extra effort to split up the functionality so that it will be easier to maintain.

                  I didn't get any requirements for the signature

                  D 1 Reply Last reply
                  0
                  • T ToddHileHoffer

                    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

                    M Offline
                    M Offline
                    Michael Bookatz
                    wrote on last edited by
                    #16

                    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.

                    1 Reply Last reply
                    0
                    • T ToddHileHoffer

                      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

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #17

                      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.


                      1 Reply Last reply
                      0
                      • N Nagy Vilmos

                        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.

                        M Offline
                        M Offline
                        Michael Bookatz
                        wrote on last edited by
                        #18

                        I think the men in the white coats will be along soon...

                        N B 2 Replies Last reply
                        0
                        • T ToddHileHoffer

                          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

                          P Offline
                          P Offline
                          puromtec1
                          wrote on last edited by
                          #19

                          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.

                          1 Reply Last reply
                          0
                          • T ToddHileHoffer

                            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

                            M Offline
                            M Offline
                            Mustafa Ismail Mustafa
                            wrote on last edited by
                            #20

                            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[^]?

                            1 Reply Last reply
                            0
                            • T ToddHileHoffer

                              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

                              S Offline
                              S Offline
                              Simon Capewell
                              wrote on last edited by
                              #21

                              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.

                              1 Reply Last reply
                              0
                              • K Kevin McFarlane

                                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

                                D Offline
                                D Offline
                                Dan Neely
                                wrote on last edited by
                                #22

                                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

                                K 1 Reply Last reply
                                0
                                • M Michael Bookatz

                                  I think the men in the white coats will be along soon...

                                  N Offline
                                  N Offline
                                  Nagy Vilmos
                                  wrote on last edited by
                                  #23

                                  You write an abstract specification with three syntactical variables then! ;P


                                  Panic, Chaos, Destruction. My work here is done.

                                  1 Reply Last reply
                                  0
                                  • T ToddHileHoffer

                                    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

                                    R Offline
                                    R Offline
                                    Russell Jones
                                    wrote on last edited by
                                    #24

                                    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.

                                    1 Reply Last reply
                                    0
                                    • T ToddHileHoffer

                                      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

                                      P Offline
                                      P Offline
                                      peterchen
                                      wrote on last edited by
                                      #25

                                      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

                                      M 1 Reply Last reply
                                      0
                                      • T ToddHileHoffer

                                        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

                                        A Offline
                                        A Offline
                                        Abu Mami
                                        wrote on last edited by
                                        #26

                                        ... 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.

                                        T 1 Reply Last reply
                                        0
                                        • A Abu Mami

                                          ... 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.

                                          T Offline
                                          T Offline
                                          ToddHileHoffer
                                          wrote on last edited by
                                          #27

                                          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

                                          A 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups