![]() |
Forum Index : Microcontroller and PC projects : CMM2: Guide to writing portable reusable code
Page 1 of 3 ![]() ![]() |
|||||
Author | Message | ||||
mkopack73 Senior Member ![]() Joined: 03/07/2020 Location: United StatesPosts: 261 |
Ok, this is still a work in progress, and in particular I need to work on the examples, but I've been starting to put this together and wanted to share and give folks a chance to comment. It's up on Google Docs here: https://docs.google.com/document/d/1dkQcLM2FRTiNELj5P3neeFih0oN2tOOxC2jy3BBH5Po/edit?usp=sharing It's a combination of things I've figured out, some good computer science best practices, and findings other have posted here on the forum. Nothing is saying you have to follow any of these things. They are simply some things that will make it easier/safer to reuse your code in other programs, or allow others to use your code. These try to overcome some of the possible problems that can creep in when programs use different settings (like OPTION MODE or OPTION DEFAULT, etc.) Anyhow, feel free to leave comments on the document and I'll address them as I go. I have a lot more to write! Hope this helps folks! |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
I think you've got this backwards, Commodore BASIC used floats by default, integers used % and Strings used $, same as MMBasic. That matches my experience with other microcomputers of the time, and I've also seen it in youtube videos with Commodore BASIC. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
I recommend the book "The BASIC Handbook" by David Lien I have 2nd and 3rd editions and prefer the layout of the 2nd edition. 2nd edition 1981 and 3rd edition 1986 I have seen the 2nd edition available as a pdf. The books cover most commands you are likely to come across when looking at old programs and it is very handy when you want to know what alternatives there are. Jim VK7JH MMedit |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
Seems like a good idea, I've added a couple of suggestions and typo fixes on the document. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
I got lost and ended up highlighting the whole page. I chickened out before I did any damage. I was reading the section on OPTION DEFAULT. The choices are FLOAT | INTEGER | STRING | NONE By default it is FLOAT The safest way to force you to be clear is to set OPTION DEFAULT NONE. Than you have to be specific for all types and will end up with code that works for all. One of the biggest gotchas is signed integers of different bit length. They are very common when talking to external devices. Functions like function signed16(x) if x > 32767 then signed16 = x - 65536 else signed16 = x endif end function are handy things to have in the toolbox. Jim VK7JH MMedit |
||||
CircuitGizmos![]() Guru ![]() Joined: 08/09/2011 Location: United StatesPosts: 1427 |
I have both, too. Yes, the 2nd is better. Micromites and Maximites! - Beginning Maximite |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10310 |
2nd edition downloadable from here |
||||
mkopack73 Senior Member ![]() Joined: 03/07/2020 Location: United StatesPosts: 261 |
Sure, but not really the point of what I'm putting together... I'm trying to give suggestions and guidelines for how people can write code so that it will work if they (or others) were to copy/paste portions into another program, regardless of what preferences for things like Option mode or option default, etc are set to. MMBasic allows a lot of flexibility, but because it allows you to change some of those options, it can make it tough to get code to work the same way all the time... |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
I just discovered CINT is different in MMBasic vs the traditional basics from The BASIC Handbook. CMM2 manual: CINT [...] Round numbers with fractional portions up or down [...] 45.57 will round to 46 BASIC Handbook: CINT [...] Numbers are always rounded down [...] Variations In Usage None known. |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
I have 2nd and 3rd editions and prefer the layout of the 2nd edition. 2nd edition 1981 and 3rd edition 1986 I have seen the 2nd edition available as a pdf. The books cover most commands you are likely to come across when looking at old programs and it is very handy when you want to know what alternatives there are. Jim Sure, but not really the point of what I'm putting together... I'm trying to give suggestions and guidelines for how people can write code so that it will work if they (or others) were to copy/paste portions into another program, regardless of what preferences for things like Option mode or option default, etc are set to. MMBasic allows a lot of flexibility, but because it allows you to change some of those options, it can make it tough to get code to work the same way all the time... Another thing that might be useful - how to check for a constant or variable that may be defined or undefined, and OPTION EXPLICIT may be set or not. Similarly, calling a SUB and catching the error if it doesn't exist. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
I just discovered CINT is different in MMBasic vs the traditional basics from The BASIC Handbook. CMM2 manual: CINT [...] Round numbers with fractional portions up or down [...] 45.57 will round to 46 BASIC Handbook: CINT [...] Numbers are always rounded down [...] Variations In Usage None known. The third edition has it right. Jim VK7JH MMedit |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
I just discovered CINT is different in MMBasic vs the traditional basics from The BASIC Handbook. CMM2 manual: CINT [...] Round numbers with fractional portions up or down [...] 45.57 will round to 46 BASIC Handbook: CINT [...] Numbers are always rounded down [...] Variations In Usage None known. The third edition has it right. Jim Thanks, I'm glad to know I was mistaken. |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
Interesting thread on re "The BASIC Handbook" << -- any other books from "back in the day" that are worth a look? Nim Entropy is not what it used to be |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Nim You should be able to find the "Microsoft QuickBASIC 4.5 2nd Edition Manual" as a pdf. And the "Quick Basic Technical Reference" can be found also but might need a copy and paste to download it. I have both in hard copy and I refer to the Technical Reference often. I am not sure about the copyright of either so I am not going to distribute copies. Jim VK7JH MMedit |
||||
capsikin Guru ![]() Joined: 30/06/2020 Location: AustraliaPosts: 341 |
Nim I liked learning about BASIC programming from the Usborne 1980s computer books years ago, and some are available online. towards the bottom of the page at https://usborne.com/browse-books/features/computer-and-coding-books/ |
||||
mkopack73 Senior Member ![]() Joined: 03/07/2020 Location: United StatesPosts: 261 |
I chickened out before I did any damage. I was reading the section on OPTION DEFAULT. The choices are FLOAT | INTEGER | STRING | NONE By default it is FLOAT The safest way to force you to be clear is to set OPTION DEFAULT NONE. Than you have to be specific for all types and will end up with code that works for all. Got it. Yeah if you use OPTION EXPLICIT then it won't matter what you set the DEFAULT to since you MUST declare the type when creating the variable. One of the biggest gotchas is signed integers of different bit length. They are very common when talking to external devices. Functions like function signed16(x) if x > 32767 then signed16 = x - 65536 else signed16 = x endif end function are handy things to have in the toolbox. Jim Any suggestions on where I should include that in the doc? |
||||
mkopack73 Senior Member ![]() Joined: 03/07/2020 Location: United StatesPosts: 261 |
Another thing that might be useful - how to check for a constant or variable that may be defined or undefined, and OPTION EXPLICIT may be set or not. Similarly, calling a SUB and catching the error if it doesn't exist. Hmm, have a way to do that defined or not variable? Maybe using the ERROR handling? Checking whether OPTION EXPLICIT is set or not shouldn't really matter so long as you always do the definition of your variables and such following the rules the Explicit forces on you, but if you have an idea how check for it I'm all ears. For missing subs/funcs - again, maybe something using the error handling? But how would you test for the existence (like as a test at the start of your program) without actually triggering the function (making it do something) ? Short of putting an error check at every sub/func call I don't know how you'd do it. |
||||
mkopack73 Senior Member ![]() Joined: 03/07/2020 Location: United StatesPosts: 261 |
Side Note: While I recommend the use of variable suffixes to indicate type, I often find this frustrating and confusing because I learned BASIC on the Commodore 64. In Commodore BASIC, integers didn’t have suffixes, while floats were denoted with % and Strings with $. As a result the MMBasic use of % for integers seems backwards to me (and likely any others who cut their teeth on Commodore BASIC.) However, given the shortcomings of the other approaches to variable declaration, this method tends to be the most clear and least error prone. Thanks, yeah my 47 yr old brain was trying to remember what my 18 yr old brain knew way back when! LOL... |
||||
mkopack73 Senior Member ![]() Joined: 03/07/2020 Location: United StatesPosts: 261 |
Ignore, duplicate post... Edited 2020-09-14 06:27 by mkopack73 |
||||
panky![]() Guru ![]() Joined: 02/10/2012 Location: AustraliaPosts: 1114 |
@mkopack73 Nice work Mike, there have been a couple of previous attempts over the years but this is by far and away the best - keep up the great work. A couple of corrections:- 1. Page 13 - the LOCAL statement in a sub or function should NOT have the DIM, it will error if you do. The same is also true for STATIC on page 14. 2. Page 13 - in your ToDo, the answer is yes, but I feel it should be STRONGLY discouraged as it is just plain confusing! Watching with interest. Doug. ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
Page 1 of 3 ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |