Slashdot Mirror


Organizing Source Code, Regardless of Language?

og_sh0x queries: "I'm looking for a source of information dedicated to organizing source code. I see a lot of books and other resources covering syntax and various syntax-related philosophies, but I can never seem to find a good resource for organizing source code in general. For instance, at what point do you split that massive source file into multiple files? At what point do two functions approaching similar functionality need to be merged, despite the cost of digging through the source and making changes to call the new function? These are problems that plague many programming languages. Are there such resources that cover these issues?"

2 of 59 comments (clear)

  1. If you have good tools... by splattertrousers · · Score: 2, Interesting
    For instance, at what point do you split that massive source file into multiple files?

    You do it as soon as you notice the problem. If you have good tools, it will be simple and fun (yes, fun).

    A refactoring browser like IDEA from IntelliJ makes it simple. Hilight a few lines of code, choose "Extract Method" from a menu, and the code is extracted into a new method with all the necessary parameters created and passed in and the necessary return type and assignment created. For example:

    1: int a = 12, b = 9;
    2: a += 43 * b + 12 / 4;
    Hilight the expression afther the "+=" online 2 and extract method, calling it "foo":
    1: int a = 12, b = 9;
    2: a += foo( b );

    3: private int foo( int c ) {
    4: return 43 * c + 12 / 4;
    5: }

    At what point do two functions approaching similar functionality need to be merged, despite the cost of digging through the source and making changes to call the new function?

    It also has a rename feature which will rename a method or variable and change all references to it, but doesn't change references to different variables or methods that happen to have the same name.

    It has lots mroe features, but you can read about them for yourself and download the program and play it.

    There are other refactoring browsers out there too, like the free Eclipse from IBM. With the right tools, you can easily make your code less messy.

  2. Database normalisation rules. by oliverthered · · Score: 3, Interesting

    Databases and code should be designed in a similar way, for more or less the same reasons. If all the refactoring book people have been recommending seem a bit extreme (even the word refactoring sounds extreme to me, a bit like downsizing grrrr....).

    Try getting a simple DB design book that goes through a normalisation process, it should make for a lighter read.

    Then think about how to apply the process to software(a bit of light thinking)

    The first couple of steps are something like

    separate everything out into discrete chunks

    look at 'keys' and 'indexes' (in source code they are design patterns, data structures the things that tie the chicks together).

    You don't need a 1000 page bible, you need a ten pages of guide lines and good practices and a bit of brain power.

    --
    thank God the internet isn't a human right.