coding convention for length of method / class

This is a discussion on coding convention for length of method / class within the General Discussions forums, part of the Community Boards category; Hi, Since the project I'm working on is the largest project I have ever done (it is for my dissertation), ...

  1. #1
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459

    coding convention for length of method / class

    Hi,

    Since the project I'm working on is the largest project I have ever done (it is for my dissertation), I have often revisited sections of code or analyses where I ask myself "what the hell was I doing here?" after months at at time.

    So, to help alleviate this stress, I decided to finally write some code which gives me useful statistics on my own coding habits, like average function length, file size, and so on.

    I have heard conventions on this before, but never bothered to follow them when I was knee deep in code because I wanted to flush the ideas out. Any advice on the topic?

    ---

    For the record, there are 25848 lines of code as of today (I've been working on this project for four years), and that is from 58 source files. Not including the matlab I've done on the side... and it's giving me a headache.

    It turns out the function lengths, in terms of lines, follow a long-tail distribution, just like a lot of other stuff in language like word frequency... at least from my sample, and the sample in the java code in my "program files" directory.
    hasafraggin shizigishin oppashigger...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    As a guide, I go with 50 lines and a max of 3 levels of indentation for any code doing anything remotely complicated. More than this, and it gets messy in a hurry to debug, test, maintain etc etc.

    Sure there are exceptions.
    Message decoders as big switch statements can be hundreds of lines long, but each individual case should either call a function or be dead simple to implement as inline code (no more than a handful of lines).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    Yeah I'm starting to feel flustered... I never gave it much though, but I think I should adopt a "function per screen" rule of thumb... and I like the 3 scope rule.

    But here's a related question... In my view, the purpose of exporting code to another function is that you call it twice, or you call it in several other circumstances within the scope of a class. However, if you only call it once, then the only purpose of writing seperate functions is to chunk a larger function into smaller functions. This does limit scope, so that in the seperate subsections, they don't accidentally share variable names and such, which is an advantage.

    I'm having trouble because the way I see it, my code has two types of code on it:

    (1) classes which are instantiated with data, upon which operations are done
    (2) classes which have static methods, and which are never instantiated becuase they are, at root, procedural and only contain static procedures...

    How can I reorganize things so that they are clearer? It seems creating a class just to have procedures sort of violates some sort of OOP principle, in that we were supposed to create objects with it?

    Btw, for the record, this is all in java... thanks Salem, I started in CProg at 16, and now I'm 26... you've always been a great help!
    hasafraggin shizigishin oppashigger...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by doubleanti
    It seems creating a class just to have procedures sort of violates some sort of OOP principle, in that we were supposed to create objects with it?
    Not really. It is basically just using a class as a namespace.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    So then, how would you organize youir source code's directories? According to classes which are instantiated, and the procedural classes?
    hasafraggin shizigishin oppashigger...

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Alphabetically.

  7. #7
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    What I have done so far is to place a character in front of related source files in terms of function.

    So for example, for data processing classes which mostly contain procedures to be called, I put a 'p' in front of it, and for data classes which are instantiated, I put a 'd' in front of it. That way my editor organizes the 'process' sources and the 'data' sources when alphabetizing.

    But it quickly becomes unwieldy when you're looking at 50+ source code files....
    hasafraggin shizigishin oppashigger...

  8. #8
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Quote Originally Posted by doubleanti View Post
    What I have done so far is to place a character in front of related source files in terms of function.

    So for example, for data processing classes which mostly contain procedures to be called, I put a 'p' in front of it, and for data classes which are instantiated, I put a 'd' in front of it. That way my editor organizes the 'process' sources and the 'data' sources when alphabetizing.

    But it quickly becomes unwieldy when you're looking at 50+ source code files....
    Your "process" and "data" sources sound like a classic case of a model and a controller.

    In my case, I have my model in one project and my controller in another project. It also happens to be that I have another project which is just interfaces for the model, and the controller can only see the interfaces, not the actual classes themselves...but I won't get into the whole reasons of why I'm doing it this way...that's how I'm doing it, and it's working very well.
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call a method from another to a class
    By vipur in forum Tech Board
    Replies: 7
    Last Post: 11-15-2009, 07:39 AM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  3. calling a class method within different class method
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2009, 10:56 AM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21