Abuse of macros
The use of macros in Clipper was a great gambiarra that brought several problems and was often abused, as is the case of the example shown.
There’s no reason to do that other than ignorance or laziness to do it the right way. Most of the places where macro was used could be done without macro in an even simpler and certainly safer way. The case of arbitrary formula compilation was a case that could be justifiable but is not. Until the Harbour has a better solution than Clipper, doing the compilation the right way and allowing you to save and load compiled code blocks.
Let’s assume you really want to let users type formulas on their own. This is reckless and should come with a compiler of its own to ensure that everything is ok and is doing nothing more than it should. The use of the macro is a huge security hole. For those who do not know what it is and are wondering what it would be like, is the eval()
javascript.
Equivalent in C#
In static and fully compiled languages it is more complicated to do this. C# even provides some ways to run on a virtual machine (it would also give on a Runtime more complete). But before doing this try to use the right tools of a static language. Find out about the design standards structural, especially the Strategy Pattern which I think is what you really need. It will not allow the user to type formulas but will facilitate the inclusion of formulas to customize the software. These formulas will be previously compiled and possibly validated by the software provider. It has several techniques to be followed to create a secure system of plugin to do this properly.
Another possibility to actually let the user type the formula is to create a compiler for this.
C# always allowed to generate code at runtime, but it was complicated, with the .NET Compiler Platform It became much easier. You can even simulate what the Clipper macro did in a reasonably simple way, and if you abstract, the syntax is even similar. But the fact that it can generate code from a source does not mean that it should, does not mean that a number of security measures should not be taken, and are fairly complicated measures to implement.
An example of the scripting:
int result = await CSharpScript.EvaluateAsync<int>("1 + 2");
A beginning of documentation can be found on Microsoft Github.
I think the Async
unnecessary in simple cases like this, but I don’t even know if the current version has a synchronous form of execution.
Completion
I stress that this should not be used by those who do not have full understanding of all the implications of its use and know how to solve everything or accept that the code is reckless. " There should be a law "obliging a code so to be used only with the express consent of the user. While it is true that a user who is harmed because of this and has not been notified of the risk (in some cases even if it has been notified), may certainly sue those who exposed it in this way.
Come on, just create a micro parser. The user enters the format in some textual field and his application analyzes after "tokenizing" the string to the corresponding variables in his code. It’s much safer that way, a lot of people fiddling with the code like you said it is not something cool. What you want is CONFIGURATION. Make your program configurable. This idea of Clipper is terrible!
– felipsmartins
There’s a section in the great book The Pragmatic Programer that says: Configure, Don’t Integrate
– felipsmartins
@felipsmartins the idea was of dBase and the Clipper was obliged to keep for compatibility. But the bad idea is not the Clipper. It’s from the programmer who uses it :)
– Maniero
Unfortunately, Harbour inherited the macro for the same reason, compatibility. I hope one day I can recompile (even if I’m making a Fork) without the Macro Clause. As for the question problem, a basic parser, as Elipe has already commented, resolves without the damaging side effects of the macro.
– Bacco
bigown - First, Dbase does not have this refusal available, but the Clipper inherited from C, by which it was developed; as I put above, the misuse of the resource is due to the inability or laziness of those who develop in developing the necessary controls. There are many programmers who think they are the guy, but they think they are far away, because they are the ones who present solutions and not criticism, the critics (negatives), are people disappointed with life and its potential, hate themselves for not being good as the people around them, Then they don’t say anything good because they have nothing to contribute. Simple as that!
– Arnaldo Oliveira
@Arnaldooliveira Did the answer solve the problem? Do you think you can accept one of them? See [tour] how to do this. You’d be helping the community by identifying the best solution. You can only accept one of them, but you can vote for anything on the entire site.
– Maniero