Bill Tillman Posted April 26, 2012 Posted April 26, 2012 Does anyone know of a utility which will count the lines of code in a LISP program? One which would ignore comments and only count actual lines of code. This brings me to my other question. When would you guys consider a LISP program too big or bloated? Some of the modules I've written are approaching about 1,000 lines. I often wonder if I should break them up into separate files. Even the largest one executes in seconds. The total time from clicking the big "GO" button in Excel to having a full drawing is anywhere from instantaneously to about 10 seconds, depending on the users machine. I worry about the code becoming too hard to manage due to all the IF statements needed to cover all the design options. I have a unix (FreeBSD) server which I think I can write some shell scripts using sed to delete comments from the file and then count just the remaining lines. But I was hoping someone may have covered this territory before. Quote
Lt Dan's legs Posted April 26, 2012 Posted April 26, 2012 I would use do something like this to read the file. If there are any comments made by ;| comments |; You will get an inaccurate number. I can modify Later for you if this is a problem. (defun readfile ( filename / _l _line ) (setq filename (open filename "r")) (while (setq _line (read-line filename)) (if (not (or (eq "" _line) (wcmatch _line ";*") ) ) (setq _l (cons _line _l)) ) ) (close filename) (reverse _l) ) (length (readfile "c:\\filename.lsp")) I have a tools.lsp that has 6892 lines and I have no problems Quote
Lee Mac Posted April 26, 2012 Posted April 26, 2012 How about something as simple as: (defun c:CountLISPLines ( / file line lines ) (if (and (setq file (getfiled "" "" "lsp" 16)) (setq file (open file "r")) ) (progn (setq lines 0) (while (setq line (read-line file)) (if (not (or (eq "" (setq line (vl-string-trim "\t " line))) (wcmatch line ";*"))) (setq lines (1+ lines)) ) ) (close file) (princ (strcat "\nFile contains " (itoa lines) " lines of code.")) ) ) (princ) ) Quote
BIGAL Posted April 27, 2012 Posted April 27, 2012 Bill re 1000 lines of code a question each time you start a new program do you repeat the same lines of code over and over ? would 20 lines of code be better as a single line calling a autoloaded library lisp, If you answered yes then it is probabbly time to look at a library, i was involved in a commercial product and the first few lines of code were the same in every program and layer manipulation was always calls to a library function because we had a smart layer manager, no use input or hard coding. Somewhere around 150+ lisps rather than 1 big one. As you say load time has never been a problem. Quote
Bill Tillman Posted April 27, 2012 Author Posted April 27, 2012 Thanks Lee and BigAl, I ran the code and it came up with 2,330 lines on the latest program. Now of course each of us programs in different styles. I always try to keep only one command on each line and only commands like TRIM do I start to spread things out so I can see what I'm doing and debug later. And most of the time I place the last parenthesis for PROGN and IF statements on their own line. I actually allowed the IDE to format my code and wow did it ever spread things out. Looked good but it also became too hard IMHO to debug because it really spread the text over many lines. The code I write is very customized for each product line and while the function like drawing the plan view or drawing the slab sections are similar, there is always enough difference to make it necessary for new code to be written. When I can I try to use variables so that I can pass them to the functions and that has made things somewhat easier. But with all the different options the users are choosing to add or not add, and many times one option can be drawn simply, but if it's selected along with another option, then both sets of code become different. Many if's and's and or's. Still I gotta believe that some of this could be set aside into a separate file which could be called from the program if and only if needed. For example, the products I work with use various springs and shocks to aid in lifting and closing of floor plates and the code for all the different configurations makes these functions the longest ones. Sometimes though, these options are not requested and it would be nice to have these functions left out of the main program completely if not needed. I've applied this principal to the blocks used and now instead of each drawing having all 200+ blocks included, I only load the blocks if they are needed. Again, this doesn't seem to speed it up much but the ending file size is much smaller. Believe it or not, the biggest factor which adds to the file size is the OLE object I copy in from Excel. The users love this though so it's not an option to eliminate. Quote
David Bethel Posted April 27, 2012 Posted April 27, 2012 I still like Ally http://www.wascotech.com/products.html Unfortunitelly, it wont run on a 64 bit machine As to bloat, 100KB always seemed to be about the max for me for laoding and execution. As to readabilty and organization, that more depends on how well the code is commented and formatted. My $0.02 -David Quote
BIGAL Posted April 30, 2012 Posted April 30, 2012 Bill just a question in the 2300 lines how many actual individual programs ie called from say a menu or keyboard. Or is it one only command Quote
MSasu Posted April 30, 2012 Posted April 30, 2012 Not to omit that the result of such counting is heavily influenced by one’s own formatting style. Maybe a better approach for comparisons is to apply the VisualLisp editor’s automated formatting on a copy of the file and then count the lines. Quote
MSasu Posted April 30, 2012 Posted April 30, 2012 time to look at a library Definitely, the way to go! Personally I learned that the hard way many years ago. It will too make your life much easier when it comes to code maintenance. Quote
Bill Tillman Posted April 30, 2012 Author Posted April 30, 2012 Bill just a question in the 2300 lines how many actual individual programs ie called from say a menu or keyboard. Or is it one only command I guess the answer to that is only one. The user starts in an Excel spreadsheet. Some of the users don't have the faintest clue about how to use AutoCAD. But they can enter data into the Excel spreadsheet about the dimensions and features they want for the assemblies. The excel file makes numerous structural calculations and summarizes this on one of it's worksheets. A VBA program in this same excel file writes the user's entries along with a couple more of the calculation results to a text file. It then loads AutoCAD and the LISP program. The only command given, and this again is an automated command, is to execute the LISP program. All operations and commands inside the LISP program MUST, and I will repeat that, MUST happen without user input of any kind. Otherwise, we defeat the whole automated process. We are eventually headed for a website interface with this which will allow on-line users to prepare their drawings without making a request from the sales or engineering team. Quote
Bill Tillman Posted April 30, 2012 Author Posted April 30, 2012 Not to omit that the result of such counting is heavily influenced by one’s own formatting style. Maybe a better approach for comparisons is to apply the VisualLisp editor’s automated formatting on a copy of the file and then count the lines. I go along with that to some degree. I have made a few adjustments in the formatting rules for the IDE, but still I never format the entire code. I think the formatting rules for the IDE (with 2009 at least) are far too liberal in placing parts of the code on separate lines. For example, it takes this: (command "._LEADER" (polar (polar pt3 (dtr 180.0) (- (/ b 2.0) 10.5)) (dtr 90.0) 1.4375) (polar (polar pt3 (dtr 180.0) (- (/ b 2.0) 14)) (dtr 270.0) 11) "@2<0" "A" "LEADER NOTE LINE 1\\PLEADER NOTE LINE 2" "") and turns it into this: (command "._LEADER" (polar (polar pt3 (dtr 180.0) (- (/ b 2.0) 10.5)) (dtr 90.0) 1.4375 ) (polar (polar pt3 (dtr 180.0) (- (/ b 2.0) 14)) (dtr 270.0) 11 ) "@2<0" "A" "LEADER NOTE LINE 1\\LEADER NOTE LINE 2" "" ) Quote
BIGAL Posted May 1, 2012 Posted May 1, 2012 Its just me I always do my calcs outside of the "command" its easier to debug an incorrect point. Also why not (dtr 180) = 1.570796327 = variable pi180, there is generally only 8 dtr used 0 90 180 270 45 135 225 315 so just do in a autoload. Have you had a look at software like strucplus http://www.cad.com.au/downloads/download-strucplus.php sounds like what your trying to do Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.