# Programming Resources



## paoling (Oct 5, 2011)

Hi to all..! Anyone knows a good book of general programming?

Not about languages types, but about how to structure a script/program to make what I want. Since I'm writing some complex scripts I would like to be sure to approach my goal in the best way. Now I'm doing this: think what I want to obtain, think about every step and decision that the program can do, write them on paper like this.

on note

(look for that and this) ----> [make something] ---> (retrive value)
.............................................................................../ \
..........................................................................TRUE.......FALSE
.................................................................[do somethin].....[do something other]

end on

etc. Then I will create some little functions like: "look_for_that_and_this" "make_something", that can be reusable with parameters and so.

This approach gave me some very good results, as long as I can predict and see the whole structure and behaviour in my mind. 

But I have problems with keeping all the things simple and organized, even with this approach. This is useful to spot buggy parts and to review the code (I make comments anyway). 
I have lots of variables, and sometimes I see that a workaround can do a better and faster job than the usual straight method. The approach to divide human thoughts in steps is correct to me, but I need some useful tips and rules about variable names, functions use, categories and so.

Paolo


----------



## chrisboy (Oct 5, 2011)

"Code complete" is a book for improving the programming style. The examples are in Java, but can be transfered to any language. It was quite helpful to me.


----------



## paoling (Oct 5, 2011)

I will check it thank you so much! 
Do you think that the object oriented nature of Java will be a problem for transferring those concept to KSP?


----------



## wst3 (Oct 5, 2011)

Forgive me if this comes off as a little negative, but I don't believe you can really learn about programming by reading books, nor, for that matter can you really hone your chops without picking one (or two) languages.

With that said...

Since you want to work with KSP I'd start with an interpreted scripting language. Interpreted because, well, cause it's instant gratification<G>! And scripting because most scripting languages follow a lot of the same basic conventions.

If I were just starting with programming I'd hope someone pointed me towards PERL. PERL is available on almost every platform you can imagine, it supports both procedural and objected oriented approaches, it has libraries that let you do some pretty amazing things (I once prototyped a secure web interface in PERL, and ended up keeping it.)

Another big advantage for PERL is the books. They are wonderful, well written, complete, and a very nice balance of pure algorithms and practical examples.

Yet another advantage is the PERL community. Folks are very helpful!

It is also a very nice foundation that appears to be easy to move from. I say appears because I started out using other languages long before PERL came to be, so I'm not a great judge, but I have watched PERL programmers leap into C, C++, Java, and other languages with ease.

If you are intrigued let me know and I'll go dig up my PERL books and get you some titles.

There was, once upon a time, a book titled "An Algorithmic Approach to Problem Solving" - or something very close to that. It was unique in that is used a variety of languages to demonstrate basic principles. Almost none of those languages are actively used these days, which my be why it is so difficult to find. But it covers everything from different ways to build decision trees to a variety of sort and search algorithms to some other cool stuff.

I've dabbled with KSP, and I think you can get some real mileage out of PERL...


----------



## Big Bob (Oct 6, 2011)

Hi Paolo,

I think that generally the KSP syntax is quite similar to Pascal and there are a ton of books devoted to programming in Pascal. While most such books use a specific language for their illustrations, you should be able to relate these to the KSP without too much difficulty.

Rejoice,

Bob


----------



## paoling (Oct 7, 2011)

Hi to all!

Thank you for your suggestions, I just need to say that I know some basic things about programming. I wrote a little Media Player in Delphi when I was 16, and fortunately Delphi is based on Pascal 

Then I moved to C++ for sometime and then I devoted myself to music 

When I first tried to write something in KSP I was firstly happy for the simple approach and familiarity about what I've already learnt, but then this userfriendly approach (only one loop type, global variables, callbacks, and so) is somewhat frustrating if you want to do more advanced things.

There's no real multitasking apart emulating it with wait() and PGS, there are no native parameter functions, you have to declare ALL the variables, even counters, and it's often prefereable to declare new counters to avoid conflicts with other callbacks.

Fortunately it's so linear that you can easily predict what it is doing in a certain moment and condition. And you can spot engine glitches before losing
the patience and the confidence about programming skills.

But apart from that, I see that often my approach in writing code is far from smart and systematic. Anyway I get my goals, but I need something to tame my creativity in a more clever way.

Another thing a book could help me about, is to not reinvent the wheel every time I program, and to utilize the right tools to approach my goals. Nils' editor implements the FOR ..TO loops, but I've never used them. Why? Only because I don't always think about the most direct way to solve the problem; this could be a matter of experience, but maybe a good reading can help me to spot the best solution in less time. 

I've checked through Code Complete 2 and it can be the book I was looking for.
I will look about PERL and I will check for "An Algorithmic Approach to Problem Solving"
Thank you so much for your suggestions!


----------



## Big Bob (Oct 7, 2011)

I think we may have misunderstood where you were coming from, at least I did. Since you have more of a programming background than I thought, I'm now a little puzzled by your original request.

Perhaps your biggest problem is a lack of discipline? It sounds a bit like you know what to do but don't put it into practice?

Most modern high-level languages more or less force good programming practices. But, when a language doesn't force you to do things the best way, that doesn't prevent you from still choosing to do it the best way. The KSE (Nils' Editor) can be a big help in assisting you to write more modular code. You should avail yourself of all its features. You say you don't use for-loops, why not? Of course at the KSP level, they will be reduced to while-loops but as far as the source code is concerned they appear to be for-loops. Since we do most of our thinking at the source-code level, this allows us to think more like we would in a higher-level language.

Why not use the KSE family structure to organize your data more logically. And, of course use KS functions and pseudo-local variables until they come out of your ears. Think in small blocks with a highly modular approach to problem solving. And, to save actual KSP code volume, convert some of your oft-repeated KS functions into native, KN functions. Even though KN functions don't handle parameter-passing in and of themselves, that doesn't stop us from writing our own parameter passing code. The idea is still sound.

Indeed we can all wish that the KSP were more richly endowed but on the other hand there is still a lot we can do with it. We just have to work a little harder and exercise more discipline in how we go about it. For example, we all wish that the KSP would support floating point math and give us a ton of functions to boot. But, the fact that these capabilities are missing need not stop us from doing it some other way.

If you think that all-integer arithmetic is a real bummer, let me tell you about the early days of microprocessors when even integer-multiply and divide had to be done with self-written subroutines! And on top of that, the longest word length was 8 bits. 

And, as far as multitasking goes, the KSP *does* provide a fairly decent multitasking model. It's just not pre-emptive. What is it that you want to multitask and why can't you do it with the KSP?

Why not just study some well-written scripts and think about them in the light of your former programming background. Think about how you could improve the structure or simplify the logic, etc. Then, concoct a non-trivial case study and code it, test it and debug it. After you have it working flawlessly, then rewrite it a couple of times with the intent of making it cleaner each time (without changing what it does). In other words, there is no real substitute for hard work and perserverence. :lol: 

Rejoice,

Bob


----------



## wst3 (Oct 7, 2011)

Big Bob @ Fri Oct 07 said:


> <snip>If you think that all-integer arithmetic is a real bummer, let me tell you about the early days of microprocessors when even integer-multiply and divide had to be done with self-written subroutines! And on top of that, the longest word length was 8 bits.<snipped again>



What you say is true enough, but on the other hand... I also remember that I had all the opcodes I used regularly memorized - not a terribly impressive feat when you think of just how few there were!!!


----------



## paoling (Oct 7, 2011)

Thanks Bob for your long answer. I think that "lack of discipline" is the right term; I'm already writing scripts for instruments I'm doing and I think that they work fairly well. My question was about the relation beetween the amount of time I spend debugging strange situations in releation to the time spent in writing code. When the debugging things become absurd, I change that part of code.

I don't know if I'm already at the point to analize a script that I like and to rewrite with less lines. But I could write ten versions of a script I've done and the next will be better than the last.

I think that, maybe, I'm at the right point to make something that works. To realize my vision and my ideas. I will try to learn some basics and good practicies about approaching problems and solve them.


----------



## sbkp (Oct 7, 2011)

I don't know if KSP lends itself to design patterns, but getting yourself steeped in some significant design patterns could be a very good idea.


----------



## paoling (Oct 8, 2011)

I've read some pages of Code Complete and it's the book for me. There's an example in the beginning that explains my situation. If you have to build a three feet tower with soda tins, you could probably start and finish it with a bunch of tins and time. But if you have to build a tower that is 10x higher, you don't need only 10x of the tins and time, but a better approach and a different planning of the problem.


----------



## Big Bob (Oct 8, 2011)

> What you say is true enough, but on the other hand... I also remember that I had all the opcodes I used regularly memorized - not a terribly impressive feat when you think of just how few there were!!!



Ah yes, I also spent many an hour with machine-code-level programming and debugging. I also remember having memorized the clock-cycle times for each instruction so that I could quickly determine execution times for each subroutine and sub-section of a program that had to control hardware in real time. Usually it wouldn't execute fast enough so one had to keep squeezing the code down tighter and tighter to make it fly. :lol: 

Hey Paolo, sounds like you're going about it in the right way. I'm also glad you found an appropriate book for your situation.

I also hope I didn't come off as too critical and I wish you every success.

Rejoice,

Bob


----------



## andreasOL (Oct 8, 2011)

Hi

Interesting thread, both regarding KSP and programming per se.

I try to use the family concept of KSE for almost everything, maybe except for general purpose loop variables i, j,... in the main body of callbacks. In KSE functions I always use local variables for while and for loops. Too many times I found out when beginning with KSP that I have overwritten i in a function while still being used in the main code.

I use the family concept for grouping variables that form the workspace for functions which can be parameterless and thus are compiled to true KSP functions. I name these functions with the same family prefix which gives me the very slight feeling of calling a method (albeit static) of an object I constructed beforehand.

Applying design patterns is difficult without any sign of object oriented behaviour in KSP. Maybe the workspace family object I described above can be thought of as a singleton.

This leads me to a thought for a possible extension of KSE. A family that is the clone of another family and then the ability to pass a family name to a function. Inside the function the passed family name is used with appended "property" names of the family (which is then valid for all families being clones of each other).

These families could then be thought of as instances of a "class" and the family name would act as the object name which is passed to a function (a KSE function).

I started programming for Z80 more then 30 years ago while still at school and I also remember counting clock cycles to implement a stepper motor controller which generated steps with controllable rising and falling frequency ramps and stuff like that...

Cheers and have a nice weekend,
Andreas


----------



## Big Bob (Oct 8, 2011)

> This leads me to a thought for a possible extension of KSE. A family that is the clone of another family and then the ability to pass a family name to a function. Inside the function the passed family name is used with appended "property" names of the family (which is then valid for all families being clones of each other).



Hi Andreas,

Always nice to meet another bit-twiddler from way back :lol: 

Regarding families, half of your request has been in place for some time. ie you *can *pass a family name as a function parameter. The other half, ie being able to declare family 'types' is something I asked for a long time ago but Nils never got around to implementing. But we can of course clone families ourselves with cut and paste, so being able to pass family names is still quite useful no?

Rejoice,

Bob


----------



## andreasOL (Oct 8, 2011)

Hi Bob,

:oops: ...could have tried myself. Great!

The other half should be accomplishable with the use of a macro. The macro defines the class and instantiating the macro instantiated the instance.

Not that this revolutionizes everything but it can be handy to group and modify data.

Thanks, Bob!

best,
Andreas


----------



## Big Bob (Oct 8, 2011)

> The other half should be accomplishable with the use of a macro. The macro defines the class and instantiating the macro instantiated the instance.



Yep, that will do it, good suggestion. o-[][]-o 

Bob


----------



## tonewill (Oct 9, 2011)

andreasOL @ 8th October 2011 said:


> Hi
> 
> This leads me to a thought for a possible extension of KSE. A family that is the clone of another family and then the ability to pass a family name to a function. Inside the function the passed family name is used with appended "property" names of the family (which is then valid for all families being clones of each other).
> 
> These families could then be thought of as instances of a "class" and the family name would act as the object name which is passed to a function (a KSE function).





> The other half should be accomplishable with the use of a macro. The macro defines the class and instantiating the macro instantiated the instance.


I found this thread because I was looking for ideas on simulating a class in Kontakt. I know it's asking a lot , but if either of you could give a very small example using both these ideas that would really help me understand it; I tend to grasp things easier if I see an example. I understand if you don't have time though.

Barry.


----------



## Big Bob (Oct 9, 2011)

How about something like this.

*on init*
``note_family(key)
``note_family(generated) 
*end on*

*on note*
``key.id := EVENT_ID
``key*.note* := EVENT_NOTE
``key.vel := EVENT_VELOCITY
``ignore_event(key.id)
``generated.id := play_note(key*.note*,key.vel,0,-1)
``generated*.note* := key*.note*
``generated.vel := key.vel
``_{ etc, etc, }_
*end on*


macro note_family(#name#)
``*family* #name#
````*declare* id
````*declare** note*
````*declare* vel
``*end* *family*
*end* macro


Rejoice,

Bob


----------



## tonewill (Oct 9, 2011)

Thank very much Bob, I can see it now. I had in mind the idea of being able to have parameters for controls like someControl.text("some name") someControl.width(82) that sort of thing but I think that would be a bit more difficult. I'll work on it and see.

EDIT: Andreas also mentioned passing the family name to a function and I was wondering what could be done there.

Thanks again,
Barry.


----------



## Big Bob (Oct 9, 2011)

Sorry I left that out Barry, here's an embellished version of the code example. Maybe this will clear up that part?

*on init* 
``note_family(key) 
``note_family(generated) 
``note_family(clone)
*end on* 

*on note* 
``key.id := EVENT_ID 
``key*.note* := EVENT_NOTE 
``key.vel := EVENT_VELOCITY 
``ignore_event(key.id) 
``generated.id := play_note(key*.note*,key.vel,0,-1) 
``generated*.note* := key*.note* 
``generated.vel := key.vel 
``copy_note_family(key,clone)
``_{ etc, etc, }_ 
*end on* 


macro note_family(#name#) 
``*family* #name# 
````*declare* id 
````*declare** note* 
````*declare* vel 
``*end* *family* 
*end* macro 

*function* copy_note_family(source,destination)
``destination.id := source.id
``destination*.note* := source*.note*
``destination.vel := source.vel
*end function*


Rejoice,

Bob


----------



## tonewill (Oct 9, 2011)

Ah, got it. Thanks again Bob, much appreciated.

Barry.


----------

