Domain: refactoring.com
Stories and comments across the archive that link to refactoring.com.
Comments · 57
-
Editors/Debuggers only go so far...The two aspects of development that sped up my coding time (albeit, more than 50% of my time is research and design, still), are automated testing (using junit) and the concept of refactoring.
Automated testing allows developers to let the software decide what's broken and what isn't, so the developer isn't spending 75% of the coding time staring at 20meg log files looking for the line that is wrong, on every run. It also improves design because it requires the programmer to have already decided what the "right" answer for an algorithm should be, thus the algorithm is properly designed to completion.
Refactoring became very important -- its critical for developers to learn how to change and improve what's already working to create code and designs that are cleaner and easier to work with for extensions and new features. I've found refactoring can do wonders in solving problems that would otherwise have had to have been dealt with by Brooks' philosophy of "Throw the first version out". With refactoring, you can save much of the code / algorithms of the first version while still throwing away the faulty architecture that is making extensions difficult; you can learn to not be afraid of changing code that works.
I'd love to give the book to every graduating CS major looking into a programming job, since the first thing they're given is a "hey, fix up this code" job, usually.
I use emacs and JDE (discussed in other comments here) for my own work, including on Windows platforms, but if you go for a commercial IDE, there are plugin extensions for some IDEs (including Borland's JBuilder) that are made to embed JUnit and refactorings into the IDE; you'll see pointers to them on the web pages.
-
IntelliJ IDEA: the best IDE around!I don't work for intellij: I'm just a very satisfied customer.
Check out IntelliJ IDEA at http://www.intellij.com.
IDEA is an excellent fully-integrated IDE. It supports (among many other things):
- Full syntax highlighting of Java and JSPs
- All the smart editor functions you would ever want
- Configurable coding style pretty-printer
- Integrated debugger, CVS, ANT, and extensible with an "External Tools" interface
- Shortcuts galore- you can do everything with the keyboard if you choose.
- The big thing: Built-in support for a whole mess of Refactorings
- And a whole lot more
IDEA is written in Java, so it works on the main platforms (I personally use it on Solaris, Linux, and occasionally Win NT/2000). Despite this, performace is good.
It costs something like $400US and I think it is worth every penny.
Grant
-
Re:Good software takes time
Good design begins with correct spelling.
-
Refactor firstThere is no rule. My current favorite programming guru, Martin Fowler, argues that the best way to deal with legacy code is to start rewriting as soon as possible--that you rewrite it in order to understand it. Fowler argues that the paradigmatic programming activity is refactoring, which he raises to something not far from a methodology. His version draws somewhat on Extreme Programming (XP), mainly in the emphasis on testing and continuous improvement. His choice of examples seems to fit the scenarios I encounter: a mix of manageable and mightmare legacy code with regular start-from-scratch projects.
You might pay particular attention to his habit of writing unit tests before he does anything.
-
Re:You don't program for a living, do you?
Or how about when you have to deal with legacy code written by someone who didn't care (or couldn't do better)? Sure it would be nice if you could rewrite the code to be elegant and such. But... the more code you rewrite, the greater the chance of one of those typos or such. And in the real world, it doesn't fly when you say you introduced a bug while rewriting the code.
So how would your statement above relate to such things as Refactoring which aims to do what you describe in the real world? It's an integral part of Extreme Programming methodology which seems to be somewhat popular amongst the Open Source software developers.
Refactoring has been, according to author Martin Fowler, practiced by the best software engineers for decades (long before XP came around). Are you saying it won't fly in the real world?
-
K&R on optimization; tipsK&R had two simple rules for optimization:
1. Don't do it.
2. (for experts only) Don't do it yet.
With those in mind, there are a number of things you can do to improve Java performance.
- Minimize object creation at all costs. This means, don't use the "+" operator for Strings at all in production code, if possible. Remember that Strings are immutable and that every constant string in your code is another String object. It is far (hundreds of times) cheaper to use StringBuffer.append() instead. (If you don't believe me, use javap -c and check the bytecodes!)
- Do refactor your code. Smaller methods are not only easier to read, maintain, and reuse, but they are also more hospitable to profiling.
- You are using a profiler, right? I've used JProbe in industry, and it's output is a lot more useful than that of java -prof but it's pretty expensive to buy if you're on your own. A profiler is a must, though, because it lets you know what to optimize.
- Avoid unnecessary synchronization. This means (if you're using Java 2), prefer HashMap to Hashtable and ArrayList to Vector. (The newer collections classes are by default not synchronized.)
- Memoize values you use a lot. Actually, memoize any value you use more than once. javac by default won't do a lot of optimizations (like moving loop invariants outside of a loop), and some query methods are expensive. It is also a lot cheaper to access a local variable than an instance or static variable -- so any time you can cache the result of something you're using more than once, do it.
- Don't initialize unnecessarily. This is Java, not C++, and all variables are by default initialized (to 0 for integer and float types, false for booleans, and null for reference types); initializing integers to zero will just make object creation more costly.
- Be careful about using default constructors. If you're creating a lot of Hashtables that don't need to hold a lot of data, you'll be paying for the 1000-item default capacity if you don't specify a size.
- Pay for things once; pay for things all at once. If you need to look up mainly-static values from a db table (for referential integrity), read them all into a map of some kind, rather than doing a SELECT for every one. Also, prefer buffered I/O whenever sensible.
- Most of all, never resort to dirty tricks that make your code unreadable for the sake of a few more cycles. It's just not worth it.
You an find a lot of great performance suggestions in Peter Haggar's excellent Practical Java, a sort of Strunk and White or "Effective C++ for Java".
Good luck.
~wog -
OO is great -- but look at IP and GPThe future of languages lies in the future of methodologies.
Intentional programming is a pretty neat idea. IP basically provides an environment for programmers to develop extensions to a given language and to abstract code into "intentions". (It is a little unfortunate that a technology coming from Charles Simonyi at Microsoft is called "IP".) With an IP environment, you don't edit lines of code; rather, you edit (sort of) the abstract syntax tree, and you can express code in a notation of your own devising (which later gets translated into a target language).
Generative programming (see generative-programming.org is another new wave in SE. The idea behind generative programming is that you create metaprograms which then can generate programs or components, allowing you to build adaptable (meaning you can change their domain) or adaptive (meaning they self-adjust to a new domain) programs fairly easily.
Also look for eXtreme Programming and refactoring to become major new forces in traditional OO SE. XP is a new way to develop software; refactoring is a new way to maintain it. XP has end-users specify the requirements of an application by talking about how they'd like to use it; then developers, working in teams, code the application, designing as they go (except for some Class-Responsibility-Collaboration cards, which they make at the beginning of the development cycle). XP involves rapid development, frequent releases, and stringent unit tests. Refactoring provides a set of rubrics for improving the design of working code without breaking it, making code easier to maintain and understand while also providing better granularity for profiling and other instrumentation.
Out of all of these, I'm currently using XP, some GP, and refactoring -- and my productivity has soared.
~wog