views:

171

answers:

5

Following my previous question regarding the rationale behind extremely long functions, I would like to present a specific question regarding a piece of code I am studying for my research. It's a function from the Linux Kernel which is quite long (412 lines) and complicated (an MCC index of 133). Basically, it's a long and nested switch statement

Frankly, I can't think of any way to improve this mess. A dispatch table seems both huge and inefficient, and any subroutine call would require an inconceivable number of arguments in order to cover a large-enough segment of code.

Do you think of any way this function can be rewritten in a more readable way, without losing efficiency? If not, does the code seem readable to you?

Needless to say, any answer that will appear in my research will be given full credit - both here and in the submitted paper.

Link to the function in an online source browser

A: 

I don't know much about kernels or about how re-factoring them might work.

The main thing that comes to my mind is taking that switch statement and breaking each sub step in to a separate function with a name that describes what the section is doing. Basically, more descriptive names.

But, I don't think this optimizes the function any more. It just breaks it in to smaller functions of which might be helpful... I don't know.

That is my 2 cents.

Frank V
The function call overhead would be prohibitive. That looks like part of an interrupt handler - needs to get in and get out.
John Saunders
+4  A: 

I don't think that function is a mess. I've had to write such a mess before.

That function is the translation into code of a table from a microprocessor manufacturer. It's very low-level stuff, copying the appropriate hardware registers for the particular interrupt or error reason. In this kind of code, you often can't touch registers which have not been filled in by the hardware - that can cause bus errors. This prevents the use of code that is more general (like copying all registers).

I did see what appeared to be some code duplication. However, at this level (operating at interrupt level), speed is more important. I wouldn't use Extract Method on the common code unless I knew that the extracted method would be inlined.


BTW, while you're in there (the kernel), be sure to capture the change history of this code. I have a suspicion that you'll find there have not been very many changes in here, since it's tied to hardware. The nature of the changes over time of this sort of code will be quite different from the nature of the changes experienced by most user-mode code.

This is the sort of thing that will change, for instance, when a new consolidated IO chip is implemented. In that case, the change is likely to be copy and paste and change the new copy, rather than to modify the existing code to accommodate the changed registers.

John Saunders
Thanks, that was informative and useful.
Adam Matan
+1  A: 

I'd start by defining constants for the various classes. Coming into this code cold, it's a mystery what the switching is for; if the switching was against named constants, I'd have a starting point.

Update: You can get rid of about 70 lines where the cases return MAJOR_0C_EXCP; simply let them fall through to the end of the routine. Since this is kernel code I'll mention that there might be some performance issues with that, particularly if the case order has already been optimized, but it would at least reduce the amount of code you need to deal with.

Carl Manaster
+1  A: 

There's a kind of regularity here, I suspect that for a domain expert this actually feels very coherent.

Also having the variations in close proximty allows immediate visual inspection.

I don't see a need to refactor this code.

djna
+1  A: 

Utterly horrible, IMHO. The obvious first-order fix is to make each case in the switch a call to a function. And before anyone starts mumbling about efficiency, let me just say one word - "inlining".

Edit: Is this code part of the Linux FPU emulator by any chance? If so this is very old code that was a hack to get linux to work on Intel chips like the 386 which didn't have an FPU. If it is, it's probably not a suitable study for academics, except for historians!

anon
The only con I can think of is the unreasonably-long number of arguments needed to be passed.
Adam Matan
Neil: more words: it's Linux. It probably needs to build on a variety of compilers. It's also very likely that nobody looks at this code except for people doing research.
John Saunders
@Udi As it appears to be something to do with the FPU, passing around some sort of FPU structure would seem to be in order.
anon
@John I agree - see my edit.
anon