<< Alexander Stepanov: Notes On Programming | Home | The Amazon.com Spam Inference Engine >>

The Number One Ill Of Java ...

... is its stupid "directory structure reflecting the package hierarchy" (mis)feature.

I was "helping" 20-year programming veterans to "understand" this (mis)feature in 1998. I'm still "helping" people to understand it now.

"I have a directory full of Java sources right here. Why do I have to tell javac or my IDE that the sources are three directories up?"

There is no good answer to this question. Sure, you can get use to it. You can even be proud that you are among the 5% programmers who persevered in understanding it. But it doesn't make it right!

You can label the on switch "off" and the off switch "on". People will get used to it. But that doesn't make it right.

It causes unnecessary problems. And its another barrier to true understanding.

That, and zero-based array indexing, have got to go.

Tags :


Re: The Number One Ill Of Java ...

I think it works fine, especially since the advent of Eclipse. Granted, javac long ago should have made use of all the redundant package declarations in each source file to orient itself.

Re: The Number One Ill Of Java ...

I find the package/directory structure very useful as it provides a way to structure my sources and to not have a directory full of source files.

There are many nice tools out there that do help you to manage all your sources and packages. Use Netbeans and you don't have any problems in creating/managing packages.

I think it is very important for all good programmers to understand packages and why and how to use them. It is very easy to write software but it is very hard to write good software...

And for the zero-based arrays: it is intuitive and helpful in many ways. For example does it help you in using modulo for ringbuffers or something like that.

Re: The Number One Ill Of Java ...

I'm going to have to side with the other readers here; this just doesn't bother me. Perhaps if I were using less project-aware tools (e.g. javac from the command-line without an IDE or a build tool), I might? But ... I almost never do that. I don't compile my code, Java and Maven/Ant do it for me, and they know where my code is, and how to find it to compile it.

Re: The Number One Ill Of Java ...

I have no problem with package structures, or the classpath. It took all of 10 minutes to learn that when I first began learning Java programming. Zero-based indexing is use in C, C++, and Java. You can't fault Java with keeping consistent with some of the most popular langauges. It's not that big of a deal anyway.

Re: The Number One Ill Of Java ...

All I hear is that you guys are used to it and it doesn't bother you any more.

Like I said. Doesn't make it right.

Re: The Number One Ill Of Java ...

Of course we are used to, but why aren't you?

the only thing you are saying is that you are not used to, is the same, you have not given any real argument but I am not used to

on the other hand the readers have said interesting stuff for example the feature of the packages, it is a lot easier to read, to search into files, have you tried C#? there have a really (mis) feature that your package can be in any folder and it does not matter, I found it difficult.

The array starting from zero, well I might give you that one, nevertheless I was used to from C and C++ so it was pretty smooth for me, anyhow sometimes you ask yourself should I be using 0 or 1, e.g. resultset.getString(0) or resultset.getString(1), obviously you ought to use 1, that is sort of dumb maybe, ain't standard but hey don't be mad at a language

Re: The Number One Ill Of Java ...

I think the Java package/directory structure is a wonderful constraint. It forces everything to be nice and orderly and you know exactly where to find everything.

C#, on the otherhand, allows you to declare namespaces in any arbitrary file in and any directory (this is necessary for them to implement the "partial class" functionality). I deal with large sources trees in Java and C#, and I much prefer navigating the Java structure. In C# I usually end up hunting for classes all over the place since I have no idea where to look.

Maybe you haven't dealt with lots of large OO source file structures? The Java structure is a breath of fresh air.

Re: The Number One Ill Of Java ...

Although I have to say I like the partial class feature of .net. The typical use is to add your own functionality to generated classes, which is nice if you do domain driven design. No stunts with inheriting from generated base classes required.

A similar feature for java would be helpful, maybe by compiling sources from multiple source trees (e.g., from src/my/package/TheClass.java and generated/my/package/TheClass.java) into a single class file.

- Patrick

Re: The Number One Ill Of Java ...

I have to reiterate the other posters comments. The directory/package structure helps clarify your project. On that point, wouldn't be a worse idea to allow classes to have packages/namespaces that were completely arbitrary? Maybe I'll put my com.util.Array in a directory called something/else. Talk about confusion. Also, are you coming from a VB background. The only people I've every heard in 20 years of programming complain about zero based arrays come from a VB background. Maybe you'd like a feature where you can flip back and forth between 0 and 1 based arrays, just like VB. Now that's a cool feature.

Re: The Number One Ill Of Java ...

The directory structure is great for organize the source files . it's helpful because is the standard (ok, constrained) way to do it.You dont need to learn a company/team 's convention on source structure that change with every project

Re: The Number One Ill Of Java ...

What a very intriguing post...

What IDE are you working with ? Could you detail the steps involved in "tell[ing] [your] IDE that the sources are three directories up?".

I mostly use eclipse and I've never had to tell it more than base source directory. As for opening files, I mainly use the Ctrl+Shift+T and Ctrl+Shift+R shortcuts which prevent you from navigating the whole tree. I suppose other modern IDEs provide similar features.

The same goes for javac. It takes a sourcepath argument that specifies where to find input source files. You give it the base directory and voilà. Again, when and how do you have to "tell javac [...] that the sources are three directories up?"

If people have problems understanding that, learning the rest of Java must really be painful for them.

Well, at least I'm in the upper 5% of something :-)

Re: The Number One Ill Of Java ...

Whilst I'll admit that I found it strange at first - and that the package statement seems to duplicate the infromation that is inherent in the directiry structure, I feel that it does provide some level of order in what could otherwise be a whole mess of files with no logical way of locating the source for a given class. It would seen feasible for the compiler to read the package statement to identify the intended 'logical' path for the generated code, but I'm sure that there are other issues involved, related to the way that classloaders locate the generated files. There may be better (other?) ways of organising source files - and we are open to your suggestions - but I, and many of the 95%, will continue working without giving package structure much of a thought (Apart from when considering inter-class visibility, of course) The old adage holds well. 'You can' doesn't necessarily mean 'You should' Overall, I feel that there are much more annoying things about Java -like why the hell does the Date class number Months from zero, but days from 1?

Re: The Number One Ill Of Java ...

You posture a position that is so inane that it will most likely be considered a non-issue (closures? world peace?) by most. Then when other's try to bring their knowledge and experience to the discussion your answer is "All I hear is that you guys are used to it" which suggests that they lack the capacity for thought and reason and their posts are akin to a "would you like cheese with that" mentality, dismissing any further discussion. I suspect you're mind is made up and you don't want to be confused with facts.

Re: The Number One Ill Of Java ...

You want a discussion. Let's have a discussion.

Give me one reason why putting the class com.weiqigao.discussion.Topic in com/weiqigao/discussion/Topic.java is better than putting it in just discussion/Topic.java.

"It's better because that's the way it works." doesn't count as a good reason.

Re: The Number One Ill Of Java ...

Mmm, what about this one : so that it's not in the same directory as the source file for org.anotherguy.discussion.Topic

Re: The Number One Ill Of Java ...

Why would I want to have both a com.weiqigao.discussion.Topic and a com.anotherguy.discussion.Topic in the same source tree? Even if I have both, I can use some other directory structure to segregate them.

Re: The Number One Ill Of Java ...

A package structure defines a "namespace" within which a class definition can be unique. You are only thinking in terms of what happens to exist in your source tree. However, there are sure to be many, many "Topic" classes when you start including other Jar files in your classpath. When you start using other, third-party libraries you will find that there are common type names used for classes that would be awkward at best if everyone had dumped their classes in a "discussion" directory. I was just coding up different versions of the same "widget" class because one is a reflection of the domain model and the other is the XML schema version of that same widget. Using a package structure with a single, intuitive name is much more preferable to phonying up some namespace-specific class names to help differentiate each instance (e.g., TopicWithThreadedHistory, TopicWithCommentary,...). After you spend some time using any of the large number of third-party XML libraries, I think that you will start to see the value of the package structure. Btw, using 'discussion' is in fact a use of the Java packaging structure, albeit a limited one. So, if you're going to be creating a namespace for your classes, why not use the inverse domain name structure of the internet? If you want a class definition of Topic that is specific you, what better package structure than com.weiqigao??? If you ever start sharing your code with others, you'll be amazed at the number of other people who have also named their class "Topic!" For a good introduction into packages, see "Core Java, Volume 1" chapter 4 on "Packages."

Re: The Number One Ill Of Java ...

Sure. The Topic of discussion, as I understand it (and I may not) is the requirements of location of the java source files in the file system relative to it's package statement. The simple answer, one that I just found out (and if I had known earlier or had more coffee I might not have been so snotty in my reply, sorry) is that there isn't one. In other words you can have a directory /home/me/discussion with the file Topic.java in it. That java file can have the package statement package com.weiqigao.discussion; in it. If you are in the discussion directory and do a javac Topic.java it will put the Topic.class file right next to the Topic.java file. Now for the not so simple part. You won't be able to run it (at least as far as I know) from the class file directory. You can however compile it with javac -d . Topic.java and run it but that will change the Topic of Discussion to the location of the compiler generated files in relation to their packages when the jvm wants to run them.

Re: The Number One Ill Of Java ...

Woops, forgot to change Anonymous to Bill.

Re: The Number One Ill Of Java ...

Because I might have another class like com.whatever.dude.discussion.Topic and then you tell me where I put that. Now, you might say "discussion/DudeTopic.java", and then its suddenly clear why the java way is better - there is no guess-work, no ambiguity, and no hunting for files.

Dude, by posting this you are asking to get flamed. Actually, I take that back. The guy who posted a link to this on TSS, wanted you to get flamed.

Re: The Number One Ill Of Java ...

This is the stupidest blog post I've seen in a long time. Java's directory/package structure is a feature, not a misfeature. Have you ever worked on a project with several thousand classes? I have. I can't imagine keeping it organized without this feature.

Re: The Number One Ill Of Java ...

No imagination is necessary. There are plenty of projects written in OO (or other) languages without this feature. Most of the successful ones are well organized.

Re: The Number One Ill Of Java ...

You can't possibly be serious. You're saying the package structure is more difficult to understand than, say, Date or Calendar? Or why you have to be careful using toString when extending BigDecimal? Or maybe the obnoxious use of checked exceptions in JDBC? Like all languages, Java is not without it's faults... but package structure is *not* one of them. Explaining it is very easy... replace the dots with path separators and you have the location of the source file one the filesystem. If you are running into programmers who can't understand this, perhaps you should be advising them to take up another profession... or take up Visual Basic.

Re: The Number One Ill Of Java ...

The principle of least astonishment.

Because Java picked a structure, there are not thousands of different conventions (one for each project out there). This makes it dramatically easier to navigate java source code, regardless of whether you're familiar with it or not. It removes endless arguments about what the structure should be. It lets every Java programmer find sources (and classes) quickly and know that what they are looking at is what they want to look at.

I bet this has saved literally thousands of man-years when measured across just the span of all developers working in Java during it's lifetime so far.

Re: The Number One Ill Of Java ...

But doesn't it violates the Don't Repeat Yourself (DRY) principle?

It also robs the programmer of a valuable tool for source code organization—the directory structure.

It leads to excessive and convoluted directory hierarchies in most Java projects. Most big projects that have the concept of separately compiled "modules" are forced to use another set of directories outside the package-induced directories:

  server/
    src/
      com/
        acme/
          server/
            Server.java
  common/
    src/
      com/
        acme/
          common/
            Common.java
  client/
    src/
      com/
        acme/
          client/
            Client.java

Re: The Number One Ill Of Java ...

Obviously you missed my reply that said you don't need to have this structure. Goes toward my point that you won't listen.

Re: The Number One Ill Of Java ...

It violates DRY in the same way that having separate files named something violates DRY. If we put everything in one directory and named things com.foo.MyClass.java it would violate DRY. Heck, naming the file MyClass.java violates DRY. As long as there is an accessible filesystem underneath the source code, we will end up violating DRY. The question now gets back to least astonishment. Thousands of files in a single directory is astonishing. Organizational-structure-per-project is astonishing. Directory hierarchies that match package naming is not astonishing and easy to match up.

With regard to organization, I don't think it eliminates the ability to organize code. Your example demonstrates how to segregate the code. What organizational opportunities in particular do you think are being lost?

Re: The Number One Ill Of Java ...

In my last example, the "/com/acme" portion of the directory structure is arbitrary and excessive and wasteful and irritating and serves no useful purpose what-so-ever, given that every thing in the project falls into the com.acme.* package hierarchy.

I'm not advocating putting everything into one directory. I'm not advocating organizational structure per project. I'm advocating a freer and more reasonable use of directories as an organizational tool.

Re: The Number One Ill Of Java ...

In my last example, the "/com/acme" portion of the directory structure is arbitrary and excessive and wasteful and irritating and serves no useful purpose what-so-ever, given that every thing in the project falls into the com.acme.* package hierarchy. I'm not advocating putting everything into one directory. I'm not advocating organizational structure per project. I'm advocating a freer and more reasonable use of directories as an organizational tool.

Every thing in THAT project falls into the com.acme.* package hierarchy AS IT IS RIGHT NOW.

What if you make a package that is not in com.acme? What about net? Or org?

Maybe a lot of people would find your "freer" and "reasonable" way "arbitrary and excessive and wasteful."

Your complaint is subjective and you have not specified an alternative. This issue does not take long to understand. If you are "helping" people understand it, they are stupid.

Re: The Number One Ill Of Java ...

I agree with Weiqi: yes, Java's directory structure namespace is redundant and overkill. For those of you trying to show off their large project experience, I worked on projects having more than 3000 developer-years (yes, years!) of work in it. You do the math yourself, and see how many classes it may have. I still think the directory structure requirement is stupid. Every time I looked at the com/mycompany/project/realclass structure, with normally the first 2 layers of directories contain nothing more than another single directory, I just couldn't help thinking why it is so stupid. It is the logical namespace that matters, not the physical formality. Some of you do have a good point: it avoids file hunting. However, with the maturity in today's IDEs, should the directory structure not a requirement, you think the IDE will not help you find your files easily?

Re: The Number One Ill Of Java ...

First I thought you are just ... well I skip that. But know I start to get your point (although I still don't agree). You want your stuff organized in a way you decide on. You can have that rather easily: write a little script to move your java files from your structur in the directory structure javac prefers. And if you want to you also can move the java.class files back to the structure you like and write a custom classloader to find them there. So you get - a little flexibility - the satisfaction of owning a project even if trivial nobody will understand - this includes any tool like IDEs build tools and the like. - no possibitlity to include third party files without some extra work. compared from the directory structure so maliciously enforced by javac, where you tipically have to do about 2 extra clicks to get through the common com.mycompany. part of the packagestructure. Which you never do, since I hope you use a decent IDE and know the shortcut for incremental search for resource by heart. enjoy

Re: The Number One Ill Of Java ...

Ooops, now we are at the heart of the problem : that the use of period . as a path separator is making you have a lot of emtpy directories.
I think that if you could have a single directory called com.acme.product.client that would be less wasteful and still be easy to navigate.
Seems to me that a request for improvement in Java 7 or 8 is needed :D

Re: The Number One Ill Of Java ...

Utterly, utterly clueless. 20 years experience? lollercoaster...

Re: The Number One Ill Of Java ...

God help you try to teach such idiots maven. They'll spontaneously combust.

Re: The Number One Ill Of Java ...

I feel tremendously stupid for even pointing this out, because I'm sure it must be in the comment trail somewhere, but just because the classes end up in a directory tree structure doesn't mean the source files have to. You could put the Foo.java file containing the class com.wizbang.Foo inside src/java directly. Now, IDEs don't like to do this, but there's nothing to prevent it. Anyhow, I'm sure everyone realizes this, but I didn't see it explicitly mentioned.

Re: The Number One Ill Of Java ...

You don't see this explicitly mentioned because "directory==package" is ingrained in peoples head for so long and so hard that they don't even think about the differences any more.

Re: The Number One Ill Of Java ...

The real story: The package-per-directory structure is not a language feature. You might have a look at the spec: From the very beginning, the JLS always allowed different ways of organizing compilation units (section 7.2 "Host Support for Packages"). Package-per-directory is just one way, which has been widely adopted because of it's simplicity and cleanliness. I'm coaching "veterans" with non-Java background on a regular basis, and my experience is just the opposite: They all understand it immediately, and - after some familiarization - they all like it...

Re: The Number One Ill Of Java ...

What a silly blog post! You believe this is a Java ill, and there is no good reason for package/directory structure in Java! Okay, suppose we agree (I don't agree, but just suppose that)! then what is wrong with it? why it bothers you?

Re: The Number One Ill Of Java ...

This is silly, seriously. Have you ever asked yourself "Why am I still working with Java?" I truely think that it is your illness NOT Java. You know why you are ill? Because you are dump. It's good to have critical thinking but NOT stupid thinking like you. BTW are you Weigi Gao from Green Island logically? Oppps.. sorry wrong namespace !!!!

Re: The Number One Ill Of Java ...

I'm with some of the previous posters. Have you actually been on a large project before? I'd rather have a directory tree nicely organised packages and classes of multiple related projects than one huge directory of thousands of .java files. 1-based arrays? I take it you started coding from VB? And in your own words, I can get used to labeling the first index a 1, but that doesn't make it right. I'm seriously beginning to wonder who you actually taught that had so much trouble understanding java packages...

Re: The Number One Ill Of Java ...

Weiqi, I'm among those 95% who does not understand your problem, even when you would have posted this in 1998. Can you give us a better solution for storing sources? Maybe you think about navigation and versioning. What about security, where you would have profiles to prevent malicious clients to execute code at a package level.

Re: The Number One Ill Of Java ...

All that I'm saying, people, is that when you work in the Marketing devision for the Mexico City branch of the GUI Research subsidiary of the International Widget and Gadget company, which uses the internationalwidgetandgadget.com domain name, and you are writing a billing application, where all your classes must live in the (I put some spaces in the package name so that it will wrap instead of going all the way to the east coast) package

com. internationalwidgetandgadget. guiresearch. mexicocity. billing
It doesn't make sense to put everything in a
com/
  internationalwidgetandgadget/
    guiresearch/
      mexicocity/
        billing
directory hierarchy. I still haven't heard one good argument that convinced me that this directory structure is necessary.

Re: The Number One Ill Of Java ...

Fanboy, yes, that says all. http://www.theserverside.com/news/thread.tss?thread_id=46430#237472

Re: The Number One Ill Of Java ...

I'm beginning to see your argument now...
It doesn't make sense to put everything in a
com/
  internationalwidgetandgadget/
    guiresearch/
      mexicocity/
        billing
directory hierarchy.
So you'd rather just have...
billing
and then have some way of telling the compiler to prepend...
com/
  internationalwidgetandgadget/
    guiresearch/
      mexicocity/
...for you. Then why didn't you say this in the first place. I can see that this is a semi-reasonable request and as such should be suggested to the people who are working on the next versions of Java instead of just griping about it.

I'm not sure that such a scheme would be without its problems (Maybe someone could point them out)

Personally, I don't mind creating the extra directories - I simply consider it to be specifically stating my intent, rather than leaving other developers to guess what I meant.

Re: The Number One Ill Of Java ...

I'm very glad you are seeing my point. However I don't think your "Then why didn't you say this in the first place?" comment is fair to me. Just look at my original post:

"I have a directory full of Java sources right here. Why do I have to tell javac or my IDE that the sources are three directories up?"

What else need I say?

Re: The Number One Ill Of Java ...

Java's isomorphic package and file structure are an example of coding by convention.

Don't Repeat Yourself (DRY) and coding by convention are compliments.

DRY is a heuristic, not an ideology.

With all design, we engineers have to balance competing constraints and goals. Hence the conflict. Though, I have to admit, that it would have never occurred to me to question/challenges Java's obviously correct design choice to have isomorphic packages and file structure. This innovation has been an unqualified good.

Perhaps these spurious tangents are productive, if only to remind us why things are the way they are, so that we don't take it all for granted.

Cheers, Jason

Re: The Number One Ill Of Java ...

If you "you work in the Marketing devision for the Mexico City branch of the GUI Research subsidiary of the International Widget and Gadget company, which uses the internationalwidgetandgadget.com domain name, and you are writing a billing application", then you can put all your source code in src/billing. Then you use javac -d to compile, jar to package the generated classes (specifying in the manifest the main class), and java -jar Mybillig.jar to execute it. There is nothing preventing you to do that EXCEPT is you use a IDE that like it's sources in a nicely-organized layout.

Add a comment Send a TrackBack