What source code organization approach helps improve modularity and API/Implementation separation?
Posted
by
Berin Loritsch
on Programmers
See other posts from Programmers
or by Berin Loritsch
Published on 2011-01-10T14:48:16Z
Indexed on
2011/01/10
14:58 UTC
Read the original article
Hit count: 365
Few languages are as restrictive as Java with file naming standards and project structure. In that language, the file name must match the public class declared in the file, and the file must live in a directory structure matching the class package. I have mixed feelings about that approach. While I never have to guess where a file lives, there's still a lot of empty directories and artificial constraints.
There's several languages that define everything about a class in one file, at least by convention. C#, Python (I think), Ruby, Erlang, etc. The commonality in most these languages is that they are object oriented, although that statement can probably be rebuffed (there is one non-OO language in the list already).
Finally, there's quite a few languages mostly in the C family that have a separate header and implementation file. For C I think this makes sense, because it is one of the few ways to separate the API interface from implementations. With C it seems that feature is used to promote modularity. Yet, with C++ the way header and implementation files are split seems rather forced. You don't get the same clean API separation that you do with C, and you are forced to include some private details in the header you would rather keep only in the implementation.
There's quite a few languages that have a concept that overlaps with interfaces like Java, C#, Go, etc. Some languages use what feels like a hack to provide the same concept like C# using pure virtual abstract classes. Still others don't really have an interface concept and rely on "duck" typing--for example Ruby. Ruby has modules, but those are more along the lines of mixing in behaviors to a class than they are for defining how to interact with a class. In OO terms, interfaces are a powerful way to provide separation between an API client and an API implementation.
So to hurry up and ask the question, from a personal experience point of view:
- Does separation of header and implementation help you write more modular code, or does it get in the way? (it helps to specify the language you are referring to)
- Does the strict file name to class name scheme of Java help maintainability, or is it unnecessary structure for structure's sake?
- What would you propose to promote good API/Implementation separation and project maintenance, how would you prefer to do it?
© Programmers or respective owner