Process Topics + CVu Journal Vol 29, #4 - September 2017
Browse in : All > Topics > Process
All > Journals > CVu > 294
Any of these categories - All of these categories

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: Navigating a Route

Author: Bob Schmidt

Date: 09 September 2017 16:44:33 +01:00 or Sat, 09 September 2017 16:44:33 +01:00

Summary: Pete Goodliffe helps us work with unfamiliar code.

Body: 

...the Investigation of difficult Things by the Method of Analysis ought ever to precede the Method of Composition.
~ Sir Isaac Newton

A new recruit joined my development team. Our project, whilst not vast, was relatively large and contained a number of different areas.

There was a lot to learn before he could become effective. How could he plot a route into the code? From a standing start, how could he rapidly become productive?

It’s a common situation; one which we all face from time to time. If you don’t, then you need to see more code and move on to new projects more often. (It’s important not to get stale from working on one codebase with one team forever.)

Coming into any large existing codebase is hard. You have to rapidly:

You need to learn this quickly, as you don’t want your first changes to be too embarrassing, accidentally duplicate existing work, or break something elsewhere.

A little help from my friends

My new colleague had a wonderful head start in this learning process. He joined an office with people who already knew the code, who could answer innumerable small questions about it, and point out where existing functionality could be found.  This kind of help is simply invaluable. 

If you are able to work alongside someone already versed in the code, then exploit this. Don’t be afraid to ask questions. If you can, take opportunities to pair program and/or to get your changes reviewed. 

Your best route into code is to be led by someone who already knows the terrain. Don’t be afraid to ask for help!

If you can’t pester people nearby, don’t fear; there may still be helpful people further afield. Look for online forums or mailing lists that contain helpful information and helpful people. There is often a healthy community that grows around popular open source projects.

The trick when asking for help is to always be polite, and to be grateful. Ask sensible, appropriate questions. “Can you do my homework for me?” is never going to get a good response. Always be prepared to help others out with information in return.

Employ common sense: make sure that you’ve Googled for an answer to your question first. It’s simple politeness to not ask foolish questions that you could easily research yourself. You won’t endear yourself to anyone if you continually ask basic questions and waste people’s precious time. Like the boy who cried wolf and failed to get help when he really needed it, a series of mind-numbingly dumb questions will make you less likely to receive more complex help when you need it.

Look for clues

If you are rooting in the murky depths of a software system without a personal guide, then you need to look for the clues that will orient you around the code.

These are good indicators:

Learn by doing

A woman needs a man like a fish needs a bicycle.
~ Irina Dunn

You can read as many books as you like about the theory of riding a bicycle. You can study bicycles, take them apart, reassemble them, investigate the physics and engineering behind them. But you may as well be learning to ride a fish.  Until you get on a bicycle, put your feet on the pedals and try to ride it for real, you’ll never advance. You’ll learn more by falling off a few times than from days of reading about how to balance. 

It’s the same with code.

Reading code will only get you so far. You can only really learn a codebase by getting on it, by trying to ride it, by making mistakes and falling off. Don’t let inactivity prevent you from moving on. Don’t erect an intellectual barrier to prevent you from working on the code.

I’ve seen plenty of great programmers initially paralysed through their own lack of confidence in their understanding.

Stuff that. Jump in. Boldly. Modify the code.

The best way to learn code is to modify it. Then learn from your mistakes.

So what should you modify?

As you are learning the code, look for places where you can immediately make a benefit, but that will minimise the chances you’ll break something (or write embarrassing code).

Aim for anything that will take you around the system. 

Low-hanging fruit

Try some simple, small things, like tracking down a minor bug that has a very direct correlation to an event you can start hunting from (e.g., a GUI activity). Start with a small, repeatable, low-risk fault report, rather than a meaty intermittent nightmare.

Inspect the code

Run the codebase through some code validators (like Lint, Fortify, Cppcheck, FxCop, ReSharper, or the like). Look to see if compiler warnings have been disabled; re-enable them, and fix the messages. This will teach you the code structure and give you a clue about the code quality.

Fixing this kind of thing is often not tricky, but very worthwhile; a great introduction. It often gets you around most of the code quickly. This kind of nonfunctional code change teaches you how things fit together and about what lives where. It gives you a great feel for the diligence of the existing developers, and highlights which parts of the code are the most worrisome and will require extra care.

Study, then act

Study a small piece of code. Critique it. Determine if there are weak spots. Refactor it. Mercilessly. Name variables correctly. Turn sprawling code sections into smaller well-named functions.

A few such exercises will give you a good feel for how malleable the code is and how yielding to fixes and modifications. (I’ve seen codebases that really fought back against refactoring).

Be wary: writing code is easier than reading it. Many programmers, rather than putting in the effort to read and understand existing code, prefer to say “it’s ugly” and rewrite it. This certainly helps them get a deep understanding of the code, but at the expense of lots of unnecessary code churn, wasted time, and in all likelihood, new bugs.

Test first

Look at the tests. Work out how to add a new unit test, and how to add a new test file to the suite. How do the tests get run?

A great trick is to try adding a single, one-line, failing test. Does the test suite immediately fail? This smoke test proves that the tests are not actively being ignored.

Do the tests serve to illustrate how each component works? Do they illustrate the interface points well?

Housekeeping

Do some spit-and-polish on the user interface. Make some simple UI improvements that don’t affect core functionality, but do make the app more pleasant to use. 

Tidy the source files: correct the directory hierarchy. Make it match the organisation in the IDE or project files.

Document what you find

Does the code have any kind of top-level README documentation file explaining how to start working on it? If not, create one and include the things that you have learned so far.

Ask one of the more experienced programmers to review it. This will show how correct your knowledge is, and also help future newbies.

As you gain understanding of the system, maintain a layer diagram of the main sections of code. Keep it up-to-date as you learn. Do you discover that the system is well layered, with clear interfaces between each layer and no unnecessary coupling? Or do you find the sections of code are needlessly interconnected? Look for ways of introducing interfaces to bring about separation without changing the existing functionality.

If there are no architectural descriptions so far, yours can serve as the documentation that will lead the new recruit into the system.

Conclusion

The more you exercise, the less pain you feel and the greater the benefit you receive. Coding is no different. The more you work on new codebases, the more you are able to pick up new code effectively.

Questions

Notes

[1] A single, automatic build step means your build can be placed into a CI harness and run automatically.

Notes: 

More fields may be available via dynamicdata ..