I know this has been asked and suggested before but there hasn't been any new post since 2009 about it. I also know there has been some strong opinions against this. However I believe there is a way to implement this and leave the current simplicity of Small Basic as is. I would suggest prefixing local variables and parameters with a special character (maybe a dot). This way global variables would still be used the same way as before and students wouldn't have to know about scope if you don't want to go into it (no pun intended). Here's an example:
.y = .x * 2 ' define new local variable and use parameter
y = 20 ' define global variable
.Func = .y * 3 ' define output of function
Good suggestion. I don't believe that Microsoft would want to implement anytime soon though. There is a way to implement something similar in the LitDev extension. I don't know if you know that it is there. It is LDCall.Function if you have the extension loaded. He also has a sample program in Other-Samples. Look for LDCall.sb.
If you're trying to teach kids about DRY principle...
...then Small Basic is probably not the right language for you.
Small Basic works to teach computational thinking and logic, but it isn't suited to "proper" (for want of a better term) programs. If you're getting into software engineering methodology, you probably need to move to a more advanced language with more advanced tools.
As an aside, I'm not certain you've got DRY right.It doesn't mean "eliminate duplicate code" and shouldn't be interpreted literally:
Bill Venners: What's the DRY principle?
Dave Thomas: Don't Repeat Yourself (or DRY) is probably one of the most misunderstood parts of the book.
Bill Venners: How is DRY misunderstood and what is the correct way to understand it?
Dave Thomas:Most people take DRY to mean you shouldn't duplicate code. That's not its intention. The idea behind DRY is far grander than that.
DRY says that every piece of system knowledge should have one authoritative, unambiguous representation. Every piece of knowledge in the development of something should have a single representation. A system's knowledge is far broader than just its code. It refers to database schemas, test plans, the build system, even documentation.
- Edited by Liam McSherryEditor Wednesday, February 15, 2017 12:11 PM
Well I guess part of it is don't repeat code. And by the way I am not trying to teach kids programming principles I am just trying to teach them to eliminate duplicate code. And this should be taught as soon as possible not when they get to C# or Java.
Here's a quote from Robert C. Martin:
Duplicate code is the root of all evil in software design. When a system is littered with many snippets of identical, or nearly identical code, it is indicative of sloppiness, carelessness, and sheer unprofessionalism. It is the guilt-edged responsibility of all software developers to root out and eliminate duplication whenever they find it.
- Edited by Grega Stamac Thursday, February 16, 2017 9:37 AM
Hi Grega !
I think it's a good idea.
But we should agree on a format and a character who can work internationally.In some regional settings, the point is used as the decimal separator, whereas in other countries the comma is used as the decimal delimiter.
You're right. It was just a suggestion. Variable name starting with a number is not valid anyway so ".343" wouldn't be interpreted as a local variable name. But you are right it should be more explicit to separate it. Maybe "~"?
- Edited by Grega Stamac Thursday, February 16, 2017 9:29 AM
local vars should be defined by prefix LOCAL or LOC, not just ~ or # or other symbols
also sub shoud be defined as functions with no return
for simplicity sub should return 0 or "" if no return specified
params should be passed by array args, as in ld xtension
textwindow.writeline(call sum(2 3 4))
'result is 2.75
call sum(2 3 4)
It is almost exactly the same as using LDCall.Function. Just replace
call sum(2 3 4)
LDCall.Function3("sum", 2, 3, 4)
There's absolutely no benefit except few less characters.
With my solution you can even have IntelliSense working with args you cannot.
- Edited by Grega Stamac Saturday, February 18, 2017 7:48 PM