Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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)

Having written C, C++, Java and Python, I find that the formality of Java and C++ can help understand API vs. implementation.

The physically separate .h file is a compiler hack that doesn't seem necessary. Java doesn't have it and those folks seem to be coping well. Python doesn't have it and things work out quite well.

Does the strict file name to class name scheme of Java help maintainability, or is it unnecessary structure for structure's sake?

Having moved from Java and C++ to Python, I find that the Java convention is of no value at all. Indeed, the Python approach of creating modules in a single file seems to help clarify dependency and reusability.

What would you propose to promote good API/Implementation separation and project maintenance, how would you prefer to do it?

Code walkthroughs.

Physical packaging can't lead to good design. Lousy design in C++ or Python is equally possible.

The only thing that leads to good design is clarity, precision and critical thinking. Code walkthroughs are a compelling way to get to good design.

share|improve this answer
    
+1 for redundant .h files, that really irritate me and stating clearly that one class = one file provides no real value. –  gruszczy Jan 10 '11 at 15:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.