moberdacker Posted May 19 Posted May 19 What is your opinion on the best way, or any way really, to learn to optimize your code? Or maybe a better way to state that I'm looking for a way to learn how to write cleaner, more streamlined code. Short of posting what I write here and asking for suggestions is there a decent way to learn more efficient coding strategies, or do you just muddle along visit forums like this one and stumble on approaches you can implement and try to apply them to the code you've written? I hardly write code, and I am not a coder but I find it interesting and a love to learn and am excited about using it to make the rest of my work life, and possibly anyone else's who finds it useful, better. Quote
CyberAngel Posted May 19 Posted May 19 (edited) Like you, I don't write much code, and when I do, it's to solve a problem and move on to the next project. I tend to use the brute force approach, that is, write statements to emulate the steps I'd follow to accomplish the task. Even if that's not the most efficient way to use the language. If I wanted to learn more about writing AutoLISP, I'd start by going through Lee Mac's awesome repository of code. Edit: To reinforce what BigAl said, it's a good idea to break down a big task into smaller ones. A job that seems overwhelming at first will get easier once you've broken it into manageable blocks. Edited May 20 by CyberAngel 1 Quote
GLAVCVS Posted May 19 Posted May 19 (edited) It's difficult to answer that question. There will likely be differing opinions on this. In my opinion, the most important thing is to come up with a good algorithm or approach. If the approach goes around in circles too much before solving the problem, the code won't be excellent enough. And, as for the code, yes, in my opinion: "That wich is good, if brief, it's twice as good" Edited May 19 by GLAVCVS Quote
BIGAL Posted May 20 Posted May 20 (edited) Code can be 3 lines or a 1000 lines so writing good code just comes with experience, using preset values can be good like pi/2 (setq pi2 (/ pi 2.0)) so an angle is always set. RTD DTR always there. In a big program of mine that is basically 5 separate functions then that is how the code was developed a function for each task. get one to work then move onto next. In one task needed to draw rectangs maybe 50 of then so have a defun to do it (drawrect X Y len ht) the final object was a window frame. using loops for rows and columns. So yes think about the task plan it out, I get a piece of paper and often draw stuff with point numbers so I don't lose where is a point. For angle "Others have a habit of answering" Sin Cos Tan. OH AH OA. Re Command, Vl, Entmake is experience and sometime just simplicity ie Command if speed not a problem. The big program above took 32 minutes to complete changing 5000 objects using command, current version is 2 minutes using VL and entmake, manual task was 3 hours. Look for patterns in code repeated lines of code used multiple times that becomes a defun. I just finished something and it has 8 defuns I am sure it could be rewritten with one defun but passing all the correct variables around 10. It was easier to write it that way if I had time would redo. Lstly I am still learning from others and taking on board there hints. So just write some code plenty here to look at it and make suggestions. Edited May 20 by BIGAL Quote
SLW210 Posted May 20 Posted May 20 I still usually write down what I want a code to accomplish, then write the steps that need to be done (these often will be added as comments reminding me why I have that code), etc. I have some (math, trig, etc.) defuns to use as needed. Like mentioned @GLAVCVS, just "come up with a good algorithm or approach" to start. I might do some quick pseudo code if I think I need to, other than that I look for code I already have that might be useful. Like others, if the code does what I need and isn't drastically slower than I think it should be, it's good enough for me. I generally have to start and stop a lot, since programming isn't my job, I have drawings to get out the door as well. One thing I do when I have time, I'll look at some of the requests on CADTutor and try my take on solving it, then whether I post a solution or not just see what the other more brilliant members have posted and how they went about it. I also go through a lot of tutorials as time allows, Autodesk has most of the Autodesk University presentations, as well as "The Garden Path" tutorial you get with the developers guide. I also look up a lot of the different things I might need to use in the AutoCAD API | Autodesk Platform Services (APS) I started long ago, mostly following the Cadalyst (it was a magazine) and other CAD/AutoCAD related magazines. I am trying to do more C#, Python, etc., as my time allows. Those require a lot of notes, organization and optimizing. Quote
GLAVCVS Posted May 20 Posted May 20 This is truly, in my opinion, a fascinating topic: how do you get a code to solve a problem? Much could be written about this. In my opinion, several variables come into play. I think someone who is eloquent, who knows how to convey what they mean, probably has the qualities necessary to be a good programmer. Because: what is a good program? Isn't it an explanation, more or less eloquent, of what a machine should do? And: won't the program be all the better the better it explains the task to be performed? That said, I think the most appropriate first approach when you set out to unravel a problem is to be aware, from the very beginning, that every problem, no matter how large, is solved by breaking it down into smaller problems. And once the small problems are solved, the big problem is solved, with the appropriate combination of solutions to the small problems. Quote
CyberAngel Posted May 20 Posted May 20 3 minutes ago, GLAVCVS said: ... I think someone who is eloquent, who knows how to convey what they mean, probably has the qualities necessary to be a good programmer. Because: what is a good program? Isn't it an explanation, more or less eloquent, of what a machine should do? And: won't the program be all the better the better it explains the task to be performed?... To expand on this a bit, you would think that a good mathematician would make a good programmer. I've found that that's not always so. There are some transferable skills, though, like logic and problem solving. It helps if you can visualize where the various parts belong, such as the scope of a local variable. And yes, eloquence is important, because good code needs good documentation. Despite what programmers will tell you, the code does not "speak for itself." You expend a lot of effort to untranslate someone else's code and figure out how it's doing what it's doing. On the other end, you have to see your code the way someone else will see it. None of these skills are easy. I think that's why most of us are daunted before we even get started. I've been writing code here and there since the 70s, and I still dread the possibility of a brick wall that I can't break through. But don't give up, don't ever give up. Oh yeah, those are other qualities you need: persistence and patience. Quote
GLAVCVS Posted May 20 Posted May 20 2 hours ago, CyberAngel said: To expand on this a bit, you would think that a good mathematician would make a good programmer. I've found that that's not always so. There are some transferable skills, though, like logic and problem solving. It helps if you can visualize where the various parts belong, such as the scope of a local variable. Yes. It makes sense at first glance. But perhaps mathematics and linguistics aren't so closely related. People with autism, for example, often have special talents for mathematics yet limited communication skills. Furthermore, these are activities that take place in different areas of the brain. Perhaps the brain of a good programmer has to give up some of its mathematical and linguistic abilities to find the necessary balance. 1 Quote
Nikon Posted May 20 Posted May 20 3 hours ago, CyberAngel said: To expand on this a bit, you would think that a good mathematician would make a good programmer. I've found that that's not always so I know excellent mathematicians who don't have programming ability. A real creative programmer is like an artist, you need talent in this field. Yes, you can understand some of the functions and capabilities of Lisp programming, but not everyone can become a talented programmer. 1 Quote
SLW210 Posted May 20 Posted May 20 Great programmers possess strong technical skills, a positive attitude, excellent communication abilities, and a commitment to continuous learning. They are also effective problem solvers, team players, and have focus on their projects. Among others, people really good in math and sciences in general have those same qualities, just not all of them. Then again those that aren't great programmers may actually just not have enthusiasm for programming. Here's a good take... 7 Qualities of a Great vs a Good Programmer | Scalable Path® Several of the coding learning sites have similar lists. Quote
Danielm103 Posted May 21 Posted May 21 Good code is readable, reasonably fast, and safe. You can also add modifiable as once a code base gets to a certain size, it’s usually hard to modify. Readable variable and functions names have meaning, not (setq a) Optimized in lisp is mostly in the algorithm space. Use the best algorithm for stuff that has time complexity (Big O) One thing I like to do is read company guidelines for coding styles, there’s not many for lisp. Google has one for common lisp https://google.github.io/styleguide/lispguide.xml interesting section “Do not abuse lists”. Anyway, companies like google usually have good reasons to follow their guidelines. Quote
BIGAL Posted May 21 Posted May 21 One thing not mentioned is using common sense naming in the code, here when you look at examples the code will have a variable "SS" for "selection set", "ang" for angle, pt1 pt2 and so on for points. Obviously if programming in non English these variable names will suit that language. Using VL commands have a more built in description of what that function is doing, when compared to using DXF codes. One thing to look out for is variable names that are built in so can cause errors if you use that name, one example is MIN where your after the minimum value. @moberdacker it is your turn now to comment. 1 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.