Results 1 to 4 of 4

Thread: is this a bad programming practice related to program architecture ?

  1. #1
    Join Date
    Aug 2012
    Beans
    623

    is this a bad programming practice related to program architecture ?

    Hello,
    I observed this kind of architecture in a project I'm working on.

    Scenario
    Assume sourcefile1.c calls a function defined in sourcefile2.c,

    What I would do to build the project is:
    gcc -o finalexe sourcefile1.c sourcefile2.c

    What I observe in the project is:
    Within sourcefile1.c, they do a #include "sourcefile2.c"

    Problem
    This makes it difficult for me to understand code flow(while working on a bigger project), because by looking at the makefile, I can't figure out how the files are linked. I would kinda have to go inside each file to see the #include's and understand how they are linked to each other.

    No worries, I have got it using some grep'ping skills, but just want to know if this is bad architecture.

    Please advise.
    Thanks.

  2. #2
    Join Date
    Dec 2012
    Beans
    24

    Re: is this a bad programming practice related to program architecture ?

    Usually you use header files ( myfunctionaliy.h ) to declare things which are implemented in the source files ( myfunctionality.c ). This makes scanning the code quickly a lot easier.

    Apart from that I can only really recommend that you choose your names very carefully.

  3. #3

    Re: is this a bad programming practice related to program architecture ?

    Yes, it is bad architecture, but maybe not for the reason you're thinking.

    For starters, it makes sourcefile1.c depend on sourcefile2.c: they can't be compiled independently. For two files this might not make much difference, but for large projects you don't want to have to re-compile the whole directory structure just because you made one change. Furthermore, it means if you use any of the functions in sourcefile2.c from any other file, you have to recompile it in its entirety as many times as it is #included.

    In a well-designed project, the .c files don't "depend" on each other; they can be compiled independently into .o files which can then be linked together to create an executable file. Whether fileA calls functions defined in fileB or the other way around (or both) is irrelevant. What's more, you can create a drop-in replacement for fileB (fileB_r2.c), compile it by itself, and link it instead of the original fileB without changing anything but the makefile. That is, assuming you have a fileB.h header file and use it correctly.

  4. #4
    Join Date
    Aug 2012
    Beans
    623

    Re: is this a bad programming practice related to program architecture ?

    Quote Originally Posted by trent.josephsen View Post
    Yes, it is bad architecture, but maybe not for the reason you're thinking.
    Thanks a lot trent, that was very insightful. That clears my question.

    Thanks ehrt74.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •