LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
Darin.K

Treat Code Snippets as "Copy and Paste" instead of "Create Sub-VI"

Status: New

I really liked the concept of Code Snippets when they were unveiled and experimented with them in the week that I have had LV2009.  As others have noted, there is a fundamental flaw when property nodes or references are involved.  I am surely oversimplifying, but it appears that they are borrowing the code used to generate Sub-VIs from a selection and putting a picture with it.  People who have studied this more than I have say there is a very good reason for doing things this way for sub-VIs and I believe them.

 

Code snippets are a different story.  In Visual Studio, snippets are a lot like macros, I consider them code fragments.  They save me the trouble of opening a file, copying some code, and pasting it into my current document.  I may have to change a few variable names, but it is a real timesaver.  If I want to use a procedure I have already written, I don't copy and paste, I just add the file to my project.  Likewise in LV, if I want to include a subVI, I'll just include it.  However, if I want a fragment of the block diagram I'll copy and paste.  I have never had a problem doing this.   The code snippet should be a way of sharing fragments, and it appears to me the code is mostly in place.  If you copy a portion of your BD and paste in the same or a different VI, you get the LV code (unchanged), paste it in a graphics program you get the image.  Both of the pieces are already in place, we just need a way of changing the contents of the clipboard to a PNG file with the additional metadata.  Dragging a snippet to the BD should be equivalent to adding the LV code metadata to the clipboard and pasting.

 

 

5 Comments
JackDunaway
Trusted Enthusiast

Typically, I would argue against making it easier for a developer to copy and paste code easily from VI to VI. If a piece of code is used in more than a few places, then I would argue it should be adapted into a subVI. Copy and pasting code many times makes for a difficult project to maintain. Comments?

 

Edit: Actually, could you provide a code snippet that is a better candidate for copy-and-pasting rather than encapsulating in a SubVI?

Message Edited by JackDunaway (mechelecengr) on 09-21-2009 03:42 PM
Darin.K
Trusted Enthusiast

A few objects that I cut and paste frequently and would not like to include as a sub-VI:

 

While Loop with Event Structure and Stop button already configured

XY Graph with property node created and commonly used properties selected and controls generated

For Loop with math in place to give me N evenly spaced values between Start and Stop Values

If I were to use local variables, I would like them to remain local variables and not turn into references

 

These are just a few that I tried recently and all but the For Loop failed currently as a Code Snippet based on covnerting to a Sub-VI 

 

I like to use the Thumbnail view in Windows Explorer to organize my snippets and in just a few days I have found that I use Property nodes more than I realize.  I would not want NI to spend a lot of time fixing this, I am suggesting that a relatively easy fix is within reach since cut-and-paste operations work just fine. 

 

Note 1:  Half of the reason I am suggesting this is the potential to improve the forums where minimizing the number of SubVIs is important, and simple code sharing would be nice.  

 

Note 2: I am a scientist, not a software developer/engineer.  My model is that you make LV as simple, powerful and flexible as possible.  Ease of copy/paste is always an advantage in my book.  Abuse by developers is  a distinct possibility but legislating good style via limiting IDE power seems to be a losing battle to me.  Abuse by scientists is a given and we get what we deserve.

 

 

Darin.K
Trusted Enthusiast

 

Message Edited by Darin.K on 09-21-2009 04:35 PM
D Robertson
Member
I am a real fan of the code snippets as they are. I like the fact that they create indicators and controls as that means that the code doesn't have broken wires when I place the code on the BD (and I'm just OCD enough that broken wire REALLY bug me). The code snippets that I have found are most useful are small pieces of code that, in my opinion, don't warrent the effort of creating a subVI with all of the associated steps needed to make it a subVI worth having (good artwork, documentation, descriptive labels, etc.) Note: as with Darin.K I am a scientist and not a software developer, and I prefer simple and easy for me in a single developer environment as opposed to forcing good style needed for a group developer environment.
Darin.K
Trusted Enthusiast
Broken code is a distinct possibility with Copy/Paste, but whether or not the code is broken is completely within my control.  Right now, LV makes changes for Snippets that breaks code, and is completely out of my control.  In a perfect world, Snippets would create controls/indicators as necessary, understand when locals/property nodes etc. are acceptable and when they need to be changed, and the result would be the code I expect.  As I understand it, that is a difficult task, and not likely to be solved to everyone's satisfaction.  As a half-measure, I'd prefer snippets that simply return the code you select and let the user decide when it is appropriate to change it.  You could call them Code Fragments if you'd like.  I think of it as a multiple selection clipboard organized by picture.
Message Edited by Darin.K on 09-22-2009 10:08 AM