How to create runtime formulas in C#?

Asked

Viewed 986 times

4

There are many advantages when things change for the better, so were the changes from the Clipper to Delphi and C#, actually only in the visual part. There was in the Clipper a fantastic feature called MACRO SUBSTITUTION.

Well, in Clipper it was possible to create formulas using Macro Substitution, where the user could create them or use the patterns that were stored in the database.

A simple example would be the calculation of an individual’s salary, as follows:

{
    var SB = 1200,00;
    var HM = 220;
    var HT = 215;
    var VP = 0;

    var sFormula = 'HT * (SB / HM)';

    VP = &sFormula;
}

Where: - SB = Basic salary - HM = Monthly Hours - HT = Hours Worked - VP = Amount of Payment

As SB, HM and HT can change from collaborator to collaborator, these would be received values in a function that would return the value of VP that would be 1172,73; This same process to calculate all events of Payroll.

Because this method, because it gives the Company the freedom to create any formula that it deems necessary to perform any calculation, the only limitation would be the use of the acronyms used in the formulas because they are defined by the software, but to include more acronyms would be a simple process.

I know this is possible in PHP and I need to know if someone knows how to do it in C#.

  • 2

    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!

  • 1

    There’s a section in the great book The Pragmatic Programer that says: Configure, Don’t Integrate

  • 1

    @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 :)

  • 1

    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.

  • 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!

  • @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.

Show 1 more comment

1 answer

5

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.

  • Fact:1. "it was a great gambiarra that brought several problems" - Truth, it brought several, if not many, for those who did not know how to use it, this is true!

  • Fact: 2. "Let’s assume you really want to let users type formulas on their own. This is reckless, "yes, true, if the developer is lazy and does not validate the information before processing it.

  • Fact: 3. "Find out about structural design patterns, especially the Strategy Pattern that I think is what you really need." finally something useful.

  • Fact: 4. "A documentation start can be found on Microsoft’s Github. There is no definitive official documentation yet." something useful too! Congratulations, you’re getting better.

  • Fact: 5. "I emphasize that this should not be used by those who do not have complete understanding of all the implications of its use", for it is, if the experiences in the past were not good, it is worth saying: Fool what does not learn from the experiences of others, but imbecile what does not learn from own. Therefore, my experiences have always been good, because I have always made the necessary validations so that there is security as to the validity of the information, because what goes wrong, is not that it sucks, but by the lazy developer who misapplies the method or by the mediocre one who reads a book is already a programmer. ;-)

  • If you have the intellectual capacity or professional potential to answer my question, post something about GOOD PRACTICES or a use case. Leave your frustrations and try, at least try to be ethical and professional, or rather, don’t comment. I make my own words: "There is no reason to do this other than ignorance or laziness to do it the right way." ; and the right way is to post a path, preferably the right one, so you add value, show your knowledge and become useful to yourself and those around you. Valeu bigown! -> https://www.youtube.com/watch?v=IUO-o_Bg8AY

  • 1

    Good practices do not exist. There is knowledge or lack of it. Good practice is synonymous with "I will do as I have heard it is good, because I have no technical condition to decide whether in this case it is right or not".

  • @Bacco - I agree with you!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.