Quote:
Originally Posted by Jeff Pahl
Back to your question, I think the answer you may be looking for in terms of cleaning up your code to make it "more professional" may be to see if you can combine IF statement trees into CASE structures, or possibly to re-structure your code into a state machine design pattern. It's kind of hard to really answer your question without seeing code.
And remember, if it works, and is readable, then however you are doing it is "not wrong".
|
This is very good advice. I also program in Labview at work, and it is quite common for me to make some assumptions at the start of a project that may not turn out to be correct, or perhaps some of the requirements get changed over time by the customer. I then end up with code that uses a structure that is not the best fit for what the code really needs to do. At this point it is a good idea to choose the correct design pattern from the Labview examples, and re-do the program mostly from scratch.
This may seem like a bad idea that will waste a lot of time, but it really does not have to. You will always find some key sections to cut and paste. And if you broke complex operations up into sub VIs you probably do not need to touch those. Yes some of the code will need to be redone, but you might be surprised how quickly the work goes the second time through and how much cleaner it looks given what you learned from the first pass. I am sometimes amazed how much work some people will do to avoid starting over even when it is clearly what is needed.
Also, good code has a lot of useful comments. Not trivial comments that basically tell a good programmer the things that they can quickly infer directly from the code and perhaps some well chosen variable names. But more detailed information that describes what you are really doing, why, and how.
Example: This FOR loop sets the motor speed for all four drive motors. Motors 1 and 2 are connected to PWM output 1 and drive the left side of the robot and PWM output 2 controls motors 3 and 4 which are on the right side. All motors are wired such that an output of 254 corresponds to full forward, an output of 127 is neutral, and 0 is full reverse.
Comment 2. The output values to control each motor are calculated in this frame from the analog input values that are .......
You get the idea.