P
P
palmage2022-04-18 22:50:42
C++ / C#
palmage, 2022-04-18 22:50:42

Is it legal to write a program from procedures without in/out parameters that operate on global variables?

Good afternoon!

I came across a program written to my inexperienced mind in a strange style, namely:

Almost all actions are performed by a large number of procedures that do not know how to IN / OUT parameters. At the same time, they operate with global parameters. The program already has 4200 lines.
A simplified program can be represented as follows:

int a;
int b;
int result;
int c = 6;
void Click1() { 
   proc1();
   proc2();
   proc3(); 
}
void Click2() { 
   proc1();
   proc2();
   proc4(); 
}
void proc1() {a = 3;}
void proc2() {b = 5;}
void proc3() {result = a + b + c; Console.Write(result)}
void proc4() {result = a * b / c; Console.Write(result)}


I have to develop this program and I'm interested in the question - is it permissible to write programs in this way or should I make an effort to change it or at least not repeat this style in new blocks of code?

In my opinion, the program should look like this:
int C = 6;
void Click1() { 
   proc1(int out a);
   proc2(int out b);
   proc3(a, b, C);
}
void Click2() { 
   proc1(int out a);
   proc2(int out b);
   proc4(a, b, C); 
}
void proc1(int out a) {a = 3;}
void proc2(int out b) {b = 5;}
void proc3(int a, int b, C) {
    int result;
    result = a + b + С;
    Console.Write(result)
}
void proc4(int a, int b, C) {
    int result;
    result = a * b / С;
    Console.Write(result)
}

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Akina, 2022-04-19
@Akina

Don't shoot the pianist - he plays the best he can.
Well, why did you all jump at once? The moron wrote, quit nafig ... The question clearly states that the program created on the initiative is completely ready and correctly performs everything put into it - so the person who created it, as it were, is not quite a moron. And the fact that the management requires it to be developed clearly says that it is also useful.
As I understand it, this program is executed locally and exclusively, it is hardly multi-threaded, and I even admit that it does not allow the simultaneous launch of several instances (although this is already quite an assumption). That is why I do not see any contraindications to normal refactoring and eventually getting an application that retains all the functionality, but written "according to science".
Now the main thing is to fully understand what and how the program does, and simply understand what will be cheaper - modify the program or, while maintaining the current version and using it as a basis, write a new version from scratch.
Armenian Radio

Such programs are written either in a fierce hurry, or with a complete lack of experience - and this says more about the employer than about the program developer.

Such programs often start as little gimmicks created by one worker for himself, to make his work easier and take off his stupid routine. And the employer here does not have any side at all - he did not instruct and did not order, it was born by itself. And then, as the worker could, he did just that. And the fact that the program not only works, but is so useful that a decision was made to develop it and pay for this development is a fat plus for the employee.

W
Wataru, 2022-04-18
@wataru

For such a code, in theory, it is necessary to hit on the hands.
It is impossible to develop and maintain it already or will become in the near future. It is urgent to refactor or even rewrite parts from scratch.
Obviously, this was written by someone with no experience at all, or someone who "relearned" C from some other ancient language.
I agree with other respondents: if there is no way to fix it, run.

N
Nikolai Savelyev, 2022-04-19
@AgentSmith

This code was written by a schoolboy who is not familiar with programming from the word "absolutely".
You have two choices:
- rewrite everything from scratch
- run away from there
The fact that you yourself did not understand this indicates that your level of programming is at the same level.

Is it legal to write a program from procedures without in/out parameters that operate on global variables?

No, not legal, according to DRY/SOLID programming laws and others. But you haven't heard of them, have you?

A
Armenian Radio, 2022-04-18
@gbg

The right decision would be to pick up the work book.
Such programs are written either in a fierce hurry, or with a complete lack of experience - and this says more about the employer than about the developer of the program.
4k lines - this can still be refactored in a couple of months in an embrace with a debugger. I once got three projects of 50k lines in Delphi. With entities like Button72. Ah, there were times...

F
Filipp42, 2022-04-19
@Filipp42

I don’t know in which case this approach is generally applicable (maybe they tried to increase performance this way?), But I think, a couple of thousand more lines of code, and the program will become completely unreadable, or at least it will be very difficult to maintain it.
I recommend reading the SICP chapters on abstraction with procedures.

D
d-stream, 2022-04-19
@d-stream

Legal.
No one forbids even sleeping on the ceiling and pooping while standing. But it is uncomfortable and the feet constantly smell.
Therefore, within the framework of the development and support of the program - to bring it to a more convenient state in terms of support and development. Naturally, having studied materials about refactoring and its concepts (Kerievsky, Martin, etc.).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question