I've been working at my job for about a year. I primarily do work in our GUI interface which uses methods from a C backend, but I generally don't have to deal with them except for return values. Our GUI is structured pretty reasonably, given our limitations.
I've been tasked with adding a function to the command line portion of the program. Most of these functions are like 300 lines long and difficult to use. I'm trying to gather pieces of them to get at specific alarm information, and I'm having trouble keeping organized. I know that I'm making my testing more complicated by doing it in a single long function.
Should I just keep everything in a huge function as per the style of the existing functions, or should I encapsulate the alarms in their own functions?
I'm not sure if its appropriate to go against the current coding conventions or whether I should just bite the bullet and make the code a little more confusing for myself to write.
In summary, I'm comparing
showAlarms(){
// tons of code
}
against
showAlarms(){
alarm1();
alarm2();
return;
}
alarm1(){
...
printf(...);
return;
}
EDIT: Thanks for the advice everyone, I decided that I'm going to design my code factored, and then ask what they want, and if they want it all in one I can just cut from my factored code and turn it back into 1 big function. This should allow me to write it and test it more easily even if they want it all the code in a single definition.
UPDATE: They ended up being happy with the factored code and more than one person has thanked me for setting this precedent.
N
lines", a better benchmark for your function may be "doesN
task(s)", whereN
is usually1
(unless there's a very good reason for it to do multiple tasks). Then, analyse each function's assigned task(s), breaking it (them) down into sub-tasks and determine which belong in their own functions. If done well, the result is that each function is as long as it needs to be to do its assigned task(s), yet (usually) easy to understand; each function's length will also reflect the complexity of its assigned task(s), which is a useful quality. – Justin Time - Reinstate Monica Dec 13 '16 at 22:39breakdance()
andbalance_budget()
to be a lot more complex thantake_a_step()
andadd()
, respectively. – Justin Time - Reinstate Monica Dec 13 '16 at 22:39N
distinct interrupts, I would consider the perfect length to be exactlyN + 4
orN + 5
lines, depending on coding style: 1 line for the function's signature, 1 line for aswitch (get_interrupt_code())
, (optional) 1 line for opening brace, 2 lines for closing braces, and 1 line for each interrupt (which consists of thecase
statement, followed by either// Falls through.
orfunction_name(); break;
Allbreak
s are lined up, as are all function calls. – Justin Time - Reinstate Monica Dec 13 '16 at 23:08