Page 2 of 7
Soon, creating a new package for each new Java project becomes second nature. Unfortunately, many Java programmers' understanding
of package's true potential stagnates at this point. But continuing to use Java's package feature in this primitive way is wholly unsatisfactory
in the long term, especially where code repositories grow in size from mere molehills to mountains.
The long-term problem of simply creating a new package for each new project is code duplication. Code duplication is one of the big evils of programming because:
We all know the root of this problem: trademark programmer's laziness. Often we get the feeling we're working on something that we've already done or solved in some distant past, so we hunt down the old solution, copy the appropriate code (logic snippet, entire method, or (hopefully not!) entire classes), and joyfully paste this into our new project. Hence the expression cut-and-paste programming.
If you shiver at the thought of your Java code repository being littered with multiple copies of near-identical bits of logic, methods, or even classes, then you need to unleash the package statement's true power in your day-to-day Java development methodology.
Let's approach the code duplication problem logically: to outlaw and eradicate all code duplication, any nontrivial piece of code should only occur once and once only. This means, among other things, that any and all
should never be declared in an application-specific package.
This key observation leads us to the package organization Golden Rule Number 1:
| Golden Rule Number 1 |
|---|
|
Below your com.company or org.yourorg package level, split your package hierarchy into two fundamentally incompatible branches:
|
Application code always uses generic code (library classes and routines) but never contains such code. The opposite is true also: library code never contains any application-specific code or even application dependencies.
If you have never considered these two fundamentally different kinds of code, then you need to start thinking about this fundamental code dichotomy in your daily programming routine. It is the key to unleashing true code reuse in your organization and banishing code duplication once and for all.
This black-and-white code perspective applied to packages logically requires a topmost-level branching into a generic/reusable package master branch and a nongeneric/nonreusable (i.e., application-specific) master branch.
So for example, for the past five years, I've split my org.lv top-level Java namespace into org.lv.lego and org.lv.apps. (lv stands for nothing more exciting than my initials.) Both these fundamental top-level branches are then further subdivided
into more detailed subpackages. My lego branch, for example, is currently subdivided into the following subpackages:
Looking for a job?By Anonymous on October 22, 2009, 9:22 amWork from home as a freelancer www.onlinefreelancejob.com
Reply | Read entire comment
View all comments