gtag('config', 'G-0PFHD683JR');
Price Prediction

Programming models: All the things that we have learned not to do

I want to present a fairly unusual perspective around programming models.

To date, we have three main models:

  1. Organized programming,
  2. Programming directed towards the object, and
  3. Job programming.

Programming models are essential ways to regulate the code. They tell you what structures to be used, and most importantly, What to avoid. Instead of giving you more strength, they set borders by adding rules for how to write your code.

Also, perhaps there will be a fourth model. I will explain why in this article, belt in!

Organized programming

In the early days of programming, Edzar Diggera realized a basic problem: Programming is difficult, and programmers do not do it very well. The programs will grow in complexity and become a great chaos, it is impossible to manage.

Therefore, he suggested that sport discipline apply for proof. This means:

  1. Start with small units that you can prove that they are correct.
  2. Use these units to provide a larger unit. Due to the presence of small units properly, the largest unit is also correct (if done correctly).

His system was similar to the standard character of your code, making it dry (do not repeat yourself). But with “sports guide”.

Now the main part: Digexra note that some uses goto Data makes this decomposition very difficult. Other uses gotoHowever, he did not. And the latter gotoS is basically just a map of structures like if/then/else and do/while.

So I suggested removing the first type of gotoThe bad type. Or even better: removal goto Completely and present if/then/else and do/while. This programming is organized.

This is really all it is. And he was right goto Being harmful, so his suggestion “won” over time. Of course, the actual mathematical proofs did not become anything, but his suggestion of what we now call organized programming succeeded.

short

deputy gotoOnly if/then/else and do/while = Organized programming

So yes, the structured programming does not give a new power, as it removes energy.

Programming directed at the object (OP)

OOP mainly includes the transfer of a stacking frame to call the job to a pile.

By doing this, local variables advertised can be found with a long -term job after returning the job. The function becomes originally for a category, local variables become similar variables, and overlapping functions become ways.

This is oop.

Now, OOP is often associated with “real or triple world modeling, inheritance, and multiple shapes, but all of this was possible before. The largest power in OOP is the multiplicity of shapes. It is allowed to release dependency, the structure of the auxiliary program, and more. However, this OOP did not invent, as we will see in a second.

Multiple shapes in c

As you promised, an example of how to achieve multiple shapes before OOP is something. Programmers C used techniques such as function indicators to achieve similar results. See a simplified example below.

Screenplay: We want to process different types of data packets received across the network. Each type requires a specific processing function package, but we want a general way to deal with any incoming package.

// Define the function pointer type for processing any packet
typedef void (_process_func_ptr)(void_ packet_data);
// Generic header includes a pointer to the specific processor
typedef struct {
    int packet_type;
    int packet_length;
    process_func_ptr process; // Pointer to the specific function
    void* data; // Pointer to the actual packet data
} GenericPacket;

When we receive and define a specific package type, for example, we will create a general counterpart and set the process index on the Process_Aute function address, and data to refer to the actual AuthPacket data:

// Specific packet data structure
typedef struct { ... authentication fields... } AuthPacketData;

// Specific processing function
void process_auth(void* packet_data) {
    AuthPacketData* auth_data = (AuthPacketData\*)packet_data;
    // ... process authentication data ...
    printf("Processing Auth Packet\n");
}

// ... elsewhere, when an auth packet arrives ...
AuthPacketData specific_auth_data; // Assume this is filled
GenericPacket incoming_packet;
incoming_packet.packet_type = AUTH_TYPE;
incoming_packet.packet_length = sizeof(AuthPacketData);
incoming_packet.process = process_auth; // Point to the correct function
incoming_packet.data = &specific_auth_data;

Now, the public handling episode can be called simply the job index stored within GenericPacket:

void handle_incoming(GenericPacket\* packet) {
    // Polymorphic call: executes the function pointed to by 'process'
    packet->process(packet->data);
}

// ... calling the generic handler ...
handle_incoming(&incoming_packet); // This will call process_auth

If the next package is a datapacket, then we will prepare GenericPacket with its process index on Process_Data, and Handle_inComing will implement Process_Data instead, although calls appear to be identical (packet->process(packet->data)). The behavior changes based on the designated function index, which depends on the type of package that is dealt with.

This multi -shape behavior method is also used for the independence of IO and many other things.

Why is OO still useful?

Although C, for example, it can achieve multiple shapes, it requires careful manual preparation, and needs to adhere to the agreements. It is an error.

OOP languages ​​such as Java or C# have not invented the multiple shapes, but they are official and mechanism. Features such as virtual functions, inheritance and interfaces deal with the basic function index (such as VTALES) automatically. So all the aforementioned negatives disappeared. Until you get the type of safety.

short

OOP has not invented multiple shapes (or inheritance or packaging). I just created an easy and safe way for us to do this and restrict Devs to use it in this way. So again, Devs did not gain a new power of OOP. Their strength is restricted by OOP.

Job programming (FP)

FP is all about domination. You cannot change the value of the variable. never. Therefore, the state has not been modified; A new country is created.

Think about it: What causes most synchronous insects? Racing conditions, your worms, simultaneous modernization issues? All stem from multiple interconnected indicators trying to change the same data part at the same time.

If the data never changes, these problems disappear. This is what is about FP.

Is pure stability practical?

There are some purely functional languages ​​such as Haskell and LISP, but most languages ​​are now not purely functional. They only integrate FP ideas, for example:

  • Java has final variables and non -changing records,
  • Typescript: Radonly rates, strict empty checks,
  • Rust: Variable variables are virtual (Let)
  • Kotlin has VAL (non -changing) versus VAR (changeable) and non -changing groups by default.

Architectural impact

The effect makes the state much easier for the aforementioned reasons. Patterns like events sources, where they store a series of events (non -changeable facts) instead of a changeable condition, are directly inspired by the FP principles.

short

In FP, you cannot change the value of the variable. Again, the developer is restricted.

summary

The pattern is clear. Programming models restrict Devs:

  • organizer: Take away goto.
  • OOPI took the raw function indicators away.
  • FunctionalI took an unrestricted task.

Models tell us what we should do. Or, differently, we have learned over the past fifty years that freedom of programming can be dangerous. The restrictions make us build better systems.

So, return to my original claim that there will be no fourth model. What’s more than gotoJob indicators and duties, do you want to take them …? Also, all these models were discovered between 1950 and 1970, which strengthened my theory that it may not see fourth.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button