LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

What is your strategy to prevent repetition of the same code? (Please read caption below)

Solved!
Go to solution

Imagine you have the attached code. By looking at the attached, notice the code in series 1a, 2a, 3a, 4a, are repeated in series 1b, 2b, 3b, 4b.  Only the codes for groups are different.

How do you prevent repetition of the same code by keeping the functionality of the code. Imagine you have 10 groups (Group A to Group J)  and each group has the same exact series. How would you solve it? I personally would try to find a shortcut. If I can't find a shortcut, then I'll do it as follow: Group A >series 1a > series 2a > series 3a > series 4a > Group B > series 1b > series 2b > series 3b > series 4b > Group C > series 1c > series 2c > series 3c > series 4c > Group D > series 1D > series 2D > series 3D> series 4D>  Group E > series 1E > series 2E > series 3E > series 4E> Group F > series 1f > series 2f > series 3f > series 4f > Group H > series 1h > series 2h > series 3h > series 4h .............Group J > series 1j > series 2j > series 3j > series 4j > Group A (Back to group A).

 

 

How can you guys solve this? Can you share your code. 

Feel free to make up a code just for demonstration purpose. You don't have to share your project if you don't want to.

0 Kudos
Message 1 of 22
(2,239 Views)

Make your code into subVI's.

Message 2 of 22
(2,195 Views)

I am not entire sure what the attached code has to do with the question, but you would just keep an additional state variable for group a vs. b (and c, d, e, f ... ad infinitum!)

 

Again, instead of explaining HOW you want to do it, explain WHAT you want to do. I am sure there is a solution that is orders of magnitude simpler!

 

Your code is again completely silly, example:

 

altenbach_0-1652221968541.png

 

Then you are using a postcard full of code to convert seconds into whole seconds, doing lots of math and jumping from DBL to string and immediately back to DBL.. What's the point!!!!

 

altenbach_1-1652222036608.png

 

The following would do exactly the same, arguably with less code.:

 

altenbach_2-1652222516705.png

 

 

None of your enums are typedefs, making any maintenance a nightmare.

 

Your start button has the wrong mechanical action.

 

There is no need to spin the loop millions of times per second to see if you pressed the button.

 

0 Kudos
Message 3 of 22
(2,180 Views)

I think even with subVi you will have to keep repeating the same code.

So your code will be Group a > subvi > Group B > subvi > Group C > subvi > Group D > subvi and so on. 

Do you see the repetition of the subvis?

0 Kudos
Message 4 of 22
(2,115 Views)

So if we ignore the  block diagram I attached, my question would be what is your method of preventing repetition of a piece of code. 

Assuming the code follows the following flow in a state machine:

Main Group A > code 1 > code 2 > code 3 > code 4 >>Main Group B > code 1 > code 2 > code 3 > code 4

 

So how do you avoid the repetition of  code 1 > code 2 > code 3 > code 4?

Assuming you have Main Group A to Z?

I wanted to see an example so I can learn from it.

 

So this is what I was asking.

0 Kudos
Message 5 of 22
(2,110 Views)

@GRCK5000 wrote:

...

So how do you avoid the repetition of  code 1 > code 2 > code 3 > code 4?

...


You make a sub-VI of it.

Certified LabVIEW Architect
0 Kudos
Message 6 of 22
(2,103 Views)

But then if you make a subvi, you code will be Main Group A > subvi > Main Group B > subi and so on. I think subvis only prevent your code from being extremely large. 

0 Kudos
Message 7 of 22
(2,094 Views)

@GRCK5000 wrote:

I think subvis only prevent your code from being extremely large. 


Think again...

 

Sometimes is is impossible to not repeat code. In fact every time you use the same LabVIEW VI (Like VISA Write) you ARE repeating code.

 

Sub-vi's are analogous to subroutines in a text based language. While it may look like you are just repeating the same code every time you use the sub-vi. I believe the compiler only makes one instance of that sub-vi in the complied code and calls the same code chunk over and over like a subroutine or user defined function in a text based language.

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 8 of 22
(2,080 Views)

If you repeat the subVI, it is significantly different than repeating the code on the diagram. Even if you now call that subVI the same number of times, you only have one copy of that code and if you notice a mistake, all you need is edit the subVI to correct the bug in all other places at the same time.

 

Currently, you need to make the same correction in N different places on the diagram and if you accidentally forget to fix them all, you have a bug that's very hard to find. A subVI can be debugged separately from the main code and there are significantly less places for bugs to hide. 

0 Kudos
Message 9 of 22
(2,074 Views)

@GRCK5000 wrote:

So if we ignore the  block diagram I attached, my question would be what is your method of preventing repetition of a piece of code. 

Assuming the code follows the following flow in a state machine:

Main Group A > code 1 > code 2 > code 3 > code 4 >>Main Group B > code 1 > code 2 > code 3 > code 4

 

So how do you avoid the repetition of  code 1 > code 2 > code 3 > code 4?

Assuming you have Main Group A to Z?

I wanted to see an example so I can learn from it.

 

So this is what I was asking.


You would make an enum with four entries (code 1, ...code 4) or just simply count from 0..3!. To cycle through groups, you can just start with uppercase A (or the U8 ASCII equivalent) and increment after each group cycle. No need for group enums (A, B, C, etc.).

0 Kudos
Message 10 of 22
(2,068 Views)