Many a times I have to deal with code files which are quite long – about 5 screen pages at the minimum. Almost always there is something towards the top/bottom that I need to refer when I’m at the bottom/top (respectively). It’s a mammoth overhead to constantly scroll back-n-forth between the file parts.
It’s a cognitive overload to just remember each and everything throughout the file and it’s quite necessary to be able to refer to parts of the file.
Yes, the Outline view in some editors like Eclipse/NetBeans helps, but sometimes they will work well only for parts recognizable by that parser. Sometimes outline views don’t show up e.g. when coding in CoffeeScript or JavaScript using closures/namespaced functions etc. The fallback is then just manual scroll overhead.
So as programmers what are some of the techniques/practices that can be employed to reduce this ridiculous overhead?
Yes, I can shorten the files, but specifically javascript libraries like Backbone.js etc. are all as one file since you really can’t split them into multiple file due to lack of Java-like packages. So under the constraints of inability of splitting a file and the outline views not working, what are some of the techniques I can use to navigate through long source files? I work a lot on Eclipse/Netbeans and Aptana and sometimes Vi (but seldom in production environments). It’d be great to be able to split a window into two and see the same file on one side and code in the other. Not doable with Eclipse/NB/Aptana IMO. Vi, yes but can’t use it all the time.
PS: As of now, I split the file among multiple editors e.g. Eclipse and Aptana. It’d be great to have it at one place, but is that the best that we can do? 🙂
UPDATE – Seems Eclipse has way for doing this and can be found here
7
So as programmers what are some of the techniques/practices that can be employed to reduce this ridiculous overhead?
under the constraints of inability of splitting a file and the outline views not working, what are some of the techniques I can use to navigate through long source files?
Open the file in several different editor windows (or different editors) – have each section of interest in a different window. If you, like many developers, have two or more monitors, you can put each window on a different monitor for reference.
I don’t know about Eclipse/NB/Apatana, but Visual Studio has a split bar – this allows you to see several different portions of a file in one window. Perhaps your editor of choice has this feature as well.
1
The most important thing to reduce this problem is to provide good abstractions for your methods. If you need to look at what Foo
does everytime, then that is a good reason to refactor the code, to either rename the method, or break up the method into different ones. (Breaking up is useful when you have a complex control flow based on the input)
If you don’t want to refactor, typically in visual studio I use goto definition and the back button on my mouse to navigate around to the various points. You might be able to use similar shortcuts in your IDE to avoid having to manually look around so much.
4
Here’s the rules which allows keeping files of 10kloc in size with 100 classes in each file in c++ code:
- Use emacs + esc-< + C-s for going to beginning of file and then searching a class by search words “class MyClass” and “::mymemberfunction”. It requires remembering the name of the class or member function you want to find.
- notepad has the same feature in C-f
- other ides probably have ability to find text from single file which you have already open; going to beginning of file is more difficult to find…
I develop in Object Pascal/Delphi and once I came up with an idea (I called it Wident) for simplifying navigation in the long files. Basically it’s about having a quick two-direction search of the identifiers at caret (I used Ctrl-Alt-[arrows]). You press for example Ctrl-Alt-Down, the tool detects the keyword at the caret pos and goes to the next mentioning of the keyword. Since after the jump your caret is placed at the same word only at different position, the next pressing will jump to the next reference (Ctrl-Alt is held so you just repeat pressing Down key). Although at this point you can be far away from your starting point, you can return to it easily. The caret is still at the name, so Ctrl-Alt-Up pressed twice will do the magic.
And this is just an example. Actually it’s like quickly creating new dimensions in navigation while keeping full control of where you are. This works because the names of variables, functions, types and their references is what connects pieces of software semantically.
Although I use the tool developed for Delphi IDE, in other editors similar functionality can be implemented just by assigning new keys to the corresponding functions. For example, in notepad++ they are “Find (Volatile) Next” and “Find (Volatile) Previous”.
Personally I rely on my memory. I remember the scroll bar position where I am looking at some important code. Ofcourse, you can only scroll to approximate location, but in most cases a PgUp / PgDown gets you right there. After a while, it becomes a habit.
You can easily remember about 4-5 scroll locations for a file. It would work great for files upto apprx. 2-3kloc. After that, the scroll bar would reach its minimum size and start losing its precision.