10.6 Working with existing code.
When you need to change code which already exists, there’s good news and there’s
bad news. The good news is that it can save you some work because you don’t have
to start from scratch. The bad news: as long as you don’t understand what’s already
there, you are bound to make mistakes. It can be hard to start working
with code you didn’t write yourself, but here’s a few suggestions to get you
started.
- Be sure to know where to find what. Examine all classes and find out what
they’re supposed to do.
- Check which classes have global objects.
- Find out which class are management classes and what see which classes
they manage.
- Are there any other classes which clearly belong together?
- It doesn’t hurt to take notes about this. A few notes about the existing
classes and how they relate to each other go a long way to gain the needed
insight for understanding existing code.
When you’re working with classes and you feel you’re stuck, ask yourself these
question to get on your way:
- What variables does the class have? What variables do I need to change
in the method I am working on. And what information can be used to
calculate the result? If this information is already available in the class,
just use it. Otherwise you will need to provide one or more arguments to
your method.
- What other methods does the class provide? Can the result of such a
method be useful to the method you’re working on?
- When your class needs to alter an object of another class, what methods
does that class provide to do what you need? If you can’t find any, you
may want to add that method first.
- Will this be a set or get method? In that case you can follow the steps
mentioned above to create it.