1

I usally code in C and C++ where implementation and declaration are in different files (.c/.h and .cpp/.hpp), but I often code in Haskell/Python/D where this distinction does not exist.

My problem is when my code tends to grow I struggle to have a clear vision of what is inside a file. I miss the "you know what to expect just by looking at the .h" and tend to become overwhelmed by the feeling of mess.

My best attempt to solve this is to put fold into the file, but I would like to know how do you do guys? Do you have some magic solutions that I haven't tried yet? Is that just a set of mind?

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
Erèbe
  • 323
  • 1
  • 2
  • 8
  • Declarations are an outdated concept based around one-pass compilers for no good reason. C/C++ compilation model is bad and modern module-based models are far superior (i.e. all the other languages you've mentioned). – Cat Plus Plus Aug 13 '12 at 09:14
  • @CatPlusPlus: Declarations aren't necessary just because of one-pass compilation. (Not to mention that C++ compilation isn't even single-pass.) You *need* declarations in C++ to avoid certain potential ambiguities in the code, which the compiler might be otherwise unable to resolve. – user541686 Aug 13 '12 at 09:33
  • @Mehrdad: That's only because C++ is terrible. Declarations were mostly inherited from C anyway. – Cat Plus Plus Aug 13 '12 at 09:35
  • I was not talking about compilation's model superiority, but about readability. I'm well aware that C/C++ compilation model is archaïc but as a side effect .h tends to provide easy to read class structure. When you read code the whole day it's great to be able to know everything of a class structure just at a glance. My point was not about who is the better, but how do you do in modern module organization to get back the advantage of .h or to have this quind of readability. – Erèbe Aug 13 '12 at 09:40

4 Answers4

2

I continue to use separate files like you do in C++ and like Java enforces in other languages, and make a lot of use of import/require/etc. Just because it is not enforced by the language does not mean you can't systematically organize your file name and content ^_^

James
  • 2,483
  • 2
  • 24
  • 31
1

I don't think there are magic solutions, but the following tips might work

  • Use classes
  • Describe for each class their resonsibility
    • Write down which data is used in each class
    • Write down which functionality is used in each class
  • Start with small classes, they will grow eventually
  • When classes getting too big, split them.
  • Use one file per class.
  • Split methods/data in public/private (with the use of the convention _)
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • 1
    what do you mean 'with use of the convention _' – thecoshman Aug 13 '12 at 09:36
  • 1
    Thanks, I will try to add at the beginning of each file the description of the class structure. But I fear comments and code will be desynchronize with time. I will see – Erèbe Aug 13 '12 at 09:39
  • Variables/functions prefixed with _ are considered private. It is not a Python language rule though. And about desynchronization with comments: the functionality of classes should not really change much. Except when splitting classes it is needed to change it comments, and you have to think anyway how to split it. – Michel Keijzers Aug 13 '12 at 10:22
1

for Java, the maven structure helps a bit.

src/main/java

for your Main code and

src/test/java

for your Test code.

Also in addition to that I follow this package structure.

All the interfaces which form the core api will be in a package ending with api.

The implementations will be in a package ending with impl.

Ajay George
  • 11,759
  • 1
  • 40
  • 48
  • No offense but I hate the `impl` abbreviation. It really doesn't say anything. +1 for the maven structure though :-) – maba Aug 13 '12 at 09:07
  • well, the Java standard is that an interface is prefixed with a capital 'I' and that if it does not have the prefix, it is an implementation. Why go against the grain? I also find my self requiring helper functions to be shared by many classes, thus a 'Helper' Suffix is often used – thecoshman Aug 13 '12 at 09:38
  • @thecoshman There is no standard in Java that an interface should be prefixed with `I`. That is a C# standard. – maba Aug 13 '12 at 09:41
  • true that. http://stackoverflow.com/questions/2814805/java-interfaces-implementation-naming-convention is an interesting discussion on the same. – Ajay George Aug 13 '12 at 09:49
  • @maba sorry, by 'standard' I meant 'system that nearly everyone has adopted as an unwritten rule' – thecoshman Aug 13 '12 at 10:16
0

I use the Java convention of placing classes and interfaces of a given name in a file with that name as much as possible. I also use Maven which has a default directory structure which is a pain to start with but very useful if you have to look at other people projects.

Do you have some magic solutions that I haven't tryed yet

I suggest the simpler the better. Less to remember. ;)

Eli Acherkan
  • 6,401
  • 2
  • 27
  • 34
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130