how often do you opt for new implementation vs re-using someone’s code lazarus / 8-Other ---------------------------------------------------------------------- Weiss · 2024-07-09 21:23 Kind of a broad question on code-writing in general. I find myself writing my own implementations of well-known math and numerical routines. I have “code recipes in Pascal” but stopped using it. My go- to book now is numerical analysis, with no code, where I have to build it from math itself. There are various reasons for that. For one, I am still foggy on most things related to licensing the published code. I also still struggle with my ability to read and fix someone’s else errors. But mostly, I have fun. Now, I am not going to change that, but feel that this isn’t normal. I also see that I would solve problems faster, and roll on to next ones, if I was more comfortable re-using existing code more efficiently. Or is it a norm, for each to build his own suite of coded solutions? How it works in real world? ---------------------------------------------------------------------- 440bx · 2024-07-09 21:47 Quote from: Weiss on July 09, 2024, 11:23:53 pmOr is it a norm, for each to build his own suite of coded solutions? How it works in real world? I think the "norm", if there is one, is to customize as necessary if and when necessary. For instance, I normally use the sort and search routines found in ntdll and find that there is no reason for me to write my own sort routine when the one in ntdll is quite good (to say the least.)  (that said, for small tables with a small number of elements I have my own shell sort but I stopped using it because ntdll's qsort does well on small number of elements too.) the binary search routine is a different ballgame.  the regular bsearch returns nil if the target element isn't found in the table but, there are occasions when it is convenient to have it return a pointer to the element that is _closest_ to the target if the target was not found.  That required a minor change in the standard algorithm. OTH, for other algorithms I use Sedgewick's Algorithms and I don't think I've ever used one of those algorithms without thoroughly customizing it (it's fair to say, in the great majority of cases, a complete rewrite.) It also depends on what my ultimate goal is, if the code is proof of concept or throw away code, I'll likely use whatever is available with an absolute minimum of changes. OTH, if the code is for a "real app" I intend to use long term then, I'll re-invent the wheel as many times as necessary until I'm satisfied I got the wheel I want. ---------------------------------------------------------------------- Thaddy · 2024-07-10 05:26 I try use as many things from the libraries in the standard distribution as possible. If I can find high quality code available for my task at hand I will use that. If what I can find is of sub-standard quality it may inspire me to do a rewrite If I can't find it - like the ternary and fuzzy logics I published, hashes, PRNG's, audio filters, LFO's - I do a complete implementation from scratch and add it to my tool kit. There are some things that I re-implement all the time because the algorithms are etched in my brains and I am too lazy to look it up, like singly and doubly linked lists and several types of trees. And sometimes I forgot that I already implemented something and one of the forum members alerts me that I wrote code for something myself years ago. So basically, it depends. But, a bit like you, I also tend to implement certain things from the math itself, especially if it is about data analyses or logic. Professionally I wrote lot's of code in other languages, C(++), java, fortran, cobol, ada, basic, some of that not available, but implementable in Pascal, so nowadays I sometimes do that if I am bored. 47 years of code is a rich resource. I still do some COBOL consultancy in the financial services industry and sometimes I might find some gems that I could probably do in Pascal. side note : COBOL proficiency is a bit of a gold mine, so if you are there are many very well payed jobs available. There is not only a shortage, but outright starvation. You can basically ask what you want. Or if the industry knows you know COBOL you get offered ridiculous hourly rates. ( am now at a project with a major bank where I am the youngest at near 67..oldest is 75.. in the team.. so be versed in geriatrics) ---------------------------------------------------------------------- Eugene Loza · 2024-07-10 07:27 After MathCad issue back in 2002-2003 when it was yielding "real part of imaginary root" as a result of cubic root instead of apparent real number I have serious trust issues with third-party code when doing math modeling - so always prefer to know every bit of code I'm working with (aka write it myself so I know who to blame if the calculation results don't make sense and more importantly - to know the assumptions I've been making when writing the code). However, in a less important context I'm all up for reusing some code. Like using a Lazarus UI. Or whole subsystems of a game engine. In my gamedev experience I've came across many plugins, but I must admit that I don't remember a single one which I didn't have any problems with and sometimes I estimated that it would have taken me less time to write my own implementation than to struggle with bugs of the plugin. And... I guess I just love reinventing the bicycle. ---------------------------------------------------------------------- gidesa · 2024-07-10 11:11 Hello, writing from zero some math algorithm can be interesting and fun. But there are tons of libraries, not written in Pascal, that concentrate decades of improvements and experience. For example, Lapack for matrices. So, if you are doing professional/sensitive coding, the choice depends on what is your task: a simple ("school") implementation from zero without any special performance/optimized code;   or using an external "de facto standard" library, almost always not written in Pascal. ---------------------------------------------------------------------- gidesa · 2024-07-10 11:16 Quote from: Thaddy on July 10, 2024, 07:26:33 am( am now at a project with a major bank where I am the youngest at near 67..oldest is 75.. in the team.. so be versed in geriatrics) I know a Cobol consultant of 80, regularly working in office every morning :-) Yes, there are still many active Cobol programs around (question for another thread: why this?), and less and less experts. ---------------------------------------------------------------------- Joanna · 2024-07-10 11:37 I think it’s admirable that you want to redo code instead of using libraries. You can look at code in library and improve it and it’s a good way to practice programming. ---------------------------------------------------------------------- VisualLab · 2024-07-10 11:47 Quote from: Weiss on July 09, 2024, 11:23:53 pmNow, I am not going to change that, but feel that this isn’t normal. I also see that I would solve problems faster, and roll on to next ones, if I was more comfortable re-using existing code more efficiently. Or is it a norm, for each to build his own suite of coded solutions? How it works in real world? This is certainly normal behavior. At least that's what I did (or, more precisely, I was forced to do). And I will certainly do it more than once, out of necessity. The example with numerical libraries is absolutely appropriate here. FPC does not have large, convenient and well-equipped numerical libraries that would be part of the package (Lazarus). What I mean here is that there are various GUI data containers in FCL, that LCL includes support for basic controls, etc. However, the standard Lazarus equipment does not include extensive numerical libraries* (matrices, functions, differentiation, etc.). This is completely understandable, because: - few programs use it (only engineering and scientific ones), - it is difficult and time-consuming to implement and test. There are "external" numerical libraries, you have to install them additionally. Besides, in the case of C, C++ it is similar, only there are slightly more of these libraries (including commercial ones). And now if someone just needs, let's say, implement an approximation, sometimes it's worth implementing it yourself. And it's similar in other areas, e.g. handling the downloading (capturing) of static images and video recordings from cameras or recording and processing sound. The library doesn't always fit well into the project being developed. Then you have to adapt it to your needs. This also affects the growth of your own experience. After all, those who wrote libraries considered professional also had to gain this experience at some point. ---------------------------------------------------------------------- wp · 2024-07-10 12:00 Quote from: VisualLab on July 10, 2024, 01:47:51 pmFPC does not have large, convenient and well-equipped numerical libraries I guess you have not seen NumLib which comes with fpc? Matrices, vectors, determinants, (simple) fitting, root finding, special functions - most of what you need. A disadvantage is its awkward syntax. But it can be managed, and I was able to write a wiki for it: https://wiki.lazarus.freepascal.org/NumLib_Documentation ---------------------------------------------------------------------- Thaddy · 2024-07-10 13:33 Yes, and it is very good indeed. It only shows it is written by academics and for academics. Most of that can be "solved" by aliasing the function names. It is also one of the oldest fpc libraries, which means a lot: the code is tried and tested. Thanks for the wiki entry, one of the better ones! ---------------------------------------------------------------------- Curt Carpenter · 2024-07-10 15:36 I suspect that the ability to reuse code without feeling too uncomfortable about it is the mark of a professional that is working in a team with budget and time constraints.   Otherwise, I recall a line from Numerical Recipes somewhere that begins "If you hate black boxes as much as we do..." ---------------------------------------------------------------------- Joanna · 2024-07-10 15:43 I wonder how much malware gets put into software by companies trying to scrimp on hiring developers. Why bother hacking when people will voluntarily download malware to save money? ---------------------------------------------------------------------- Thaddy · 2024-07-10 17:47 You examine the code, only then you use the code. If you see a "black box" you are basically blind. If you do not understand the code, leave it alone. When sources are available and you compile from source, the "black box" is you, never the code. ---------------------------------------------------------------------- Weiss · 2024-07-10 19:38 thank you all, interesting take, echoes my own in most cases. I did not mention trust issues and blackboxes, but those are part of reasons for re-inventing the wheel. Also, 440 mentioned custom implementations for “real app”. Which made me think, new as I am, everything I make is a “real app”. Like for teenager, everything is important and real. I imagine things are different when working in a team with budget and time pressure. I work on a ship which was highly digitized like that, on a budget and under time constraints. 4 years into service, we are still dealing with some dramatic issues with software that runs the ship. Soft written in C, slapped together to make things work. I am not complaining, in these 4  years ships of this class have carried millions of passengers from one shore to the other, so business-wise it is a plausible strategy, just different. ---------------------------------------------------------------------- Curt Carpenter · 2024-07-10 20:15 Sounds like an interesting life! One of my first jobs long ago (interlude before the military) was working a few months on a Supervisory Control System for the nuclear power industry.  People should be terrified at how a lot of software gets made. ---------------------------------------------------------------------- silvercoder70 · 2024-07-10 22:07 All that I can say is ... it depends. And I hate the thought of re- inventing the wheel. The pros and cons of each are easy approach are easy enough to work out. And perhaps 2 examples: 1. I have used 3rd party software libraries (well known and purchased) that had issues and having to wait for a resolution. 2. JCL has functions for reading and writing to the Windows registry. A few years back a co-worker decided to write own Delphi functions to do the same vs (re-)using the code provided in the JCL. ---------------------------------------------------------------------- Marc · 2024-07-11 07:45 At my job, the simple things like list management, network stuff, graphics and async communications are put in libraries and used in all our projects. If something "complex" is available as third party source, we'll use that to give us a jumpstart. In case of the SIP stack we bought, it was a fast start, but through the years there are so many things changed and added that you won't find a piece of the original code. Recently I had the same with a 3rd party CORBA stack. It helped us interfacing, but it was bulky, complex and hard to understand. It had issues but we could work around it until early this year. It appeared to report wrong errors when a connection dropped (this was critical to us). So the lib was dropped and I rewrote a new, simpler implementation based on our own network layer. When we needed SRTP, we could implement that ourselves, but encryption is hard to debug. Cisco had a nice simple, understandable C lib source, so we decided to use that as external lib. Sometimes there is no lib available, only C sample code. In that case I had a choice to make it a C lib and use it, or pascalize the source. It took me, at that time, 2 weeks to have a working ACELP audio codec. Later I decided to do the same for a AMR-WB codec. This one looked similar, but took more than a month to debug it. Now I'm at a point where an OPUS codec is needed. It can be used as lib which would mean we have to distribute it, or I can convert it to pascal so we have it internally... (i think I go for the lib version) Marc ---------------------------------------------------------------------- Weiss · 2024-07-11 14:47 Quote from: wp on July 10, 2024, 02:00:03 pmQuote from: VisualLab on July 10, 2024, 01:47:51 pmFPC does not have large, convenient and well-equipped numerical libraries I guess you have not seen NumLib which comes with fpc? ...(simple) fitting, root finding, special functions - most of what you need. A disadvantage is its awkward syntax. But it can be managed, and I was able to write a wiki for it: https://wiki.lazarus.freepascal.org/NumLib_Documentation numlib is ok, but like you said, only simple - rudimental really - fitting. I needed cubic splines, trigonometric fit, Besier, FFT etc. Couldn't find anything free that would not say something about educational use only. About numlib syntax, I am thinking, maybe it is worth of building an interface, a translator with more digestible names and conversion of everything that starts with "arb". ---------------------------------------------------------------------- Dzandaa · 2024-07-11 15:16 Hi, I write my own programs,  it's a hobby now as I am retired. But as SilverCoder says: QuoteI hate the thought of re-inventing the wheel. And with Lazarus, you can always look at the sources, it's a great plus. @VisualLab: I assume you have tested "LMath" https://sourceforge.net/projects/lmath-library/ What is missing is a Library to make wavelets. B-> ---------------------------------------------------------------------- Thaddy · 2024-07-11 15:47 Quote from: Weiss on July 11, 2024, 04:47:49 pmI needed cubic splines, trigonometric fit, Besier, FFT etc. Couldn't find anything free that would not say something about educational use only.All of that is available in pure pascal. some of it simply in the canvas units. Others in audio libraries. QuoteAbout numlib syntax, I am thinking, maybe it is worth of building an interface, a translator with more digestible names and conversion of everything that starts with "arb". As i wrote, it needs aliasing for mere humans, but that is all.