Code efficiency

Joined
Feb 17, 2023
Messages
2
Reaction score
0
Hello everyone, I come with a piece of code that got me into a "brawl" in the office:

It's a module designed to accelerate and decelerate a motor from a PLC within a certain range of RPS. The normal speed is generated in a second module that doesn't matter here. My boss created the code and then shared with me:

While...
//Logic protections...
//Acceleration iterations...
//Deceleration iterations...
//Limits:
IF output_speed > MaxSpeedAllowed THEN
output_speed := MaxSpeedAllowed;
END_IF;

IF output_speed < MinSpeedAllowed THEN
output_speed := MinSpeedAllowed;
END_IF;
endWhile;


I told him that he could improve it a lot by setting the high and low limits of speed as conditionals and then running the acceleration or declaration depending on the conditions, because this way there would be many less iterations per cycle and we would be changing the output speed only once per cycle, which is the ideal case:

While...
//Logic protections...
//Limits:
IF currentSpeed + speedFactor > MaxSpeedAllowed THEN
currentSpeed := MaxSpeedAllowed;

ELSE_IF currentSpeed - SpeedFactor < MinSpeedAllowed THEN
currentSpeed := MinSpeedAllowed;
ELSE
//Acceleration iterations...
//Deceleration iterations...
endWhile;

He got annoyed and told me to "stop asking stupid questions". I would like to have your opinion because I am getting tired of this guy and I just want to make him swallow his ego.
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
In terms of performance and efficiency, your version of the code with conditional statements is likely to be more efficient than your boss's original code. This is because your version only updates the output speed once per cycle, while your boss's version updates it potentially multiple times per cycle, depending on how many iterations are executed.
Furthermore, your version of the code is more concise and easier to read, which can make it easier to maintain in the long run. However, it's important to note that there may be other considerations at play, such as the specific requirements of your system or other technical constraints, that may affect the best approach.
In terms of how to handle the situation with your boss, it's important to approach the discussion in a professional and respectful manner. It's possible that your boss may have reasons for using their original approach that you're not aware of, and getting defensive or confrontational is unlikely to be productive. Instead, you could try to have a constructive conversation where you explain your reasoning for the alternative approach and see if you can come to a mutually beneficial solution. Ultimately, the goal should be to improve the quality of the code and the performance of the system, not to prove who is right or wrong.
Hello everyone, I come with a piece of code that got me into a "brawl" in the office:

It's a module designed to accelerate and decelerate a motor from a PLC within a certain range of RPS. The normal speed is generated in a second module that doesn't matter here. My boss created the code and then shared with me:

While...
//Logic protections...
//Acceleration iterations...
//Deceleration iterations...
//Limits:
IF output_speed > MaxSpeedAllowed THEN
output_speed := MaxSpeedAllowed;
END_IF;

IF output_speed < MinSpeedAllowed THEN
output_speed := MinSpeedAllowed;
END_IF;
endWhile;


I told him that he could improve it a lot by setting the high and low limits of speed as conditionals and then running the acceleration or declaration depending on the conditions, because this way there would be many less iterations per cycle and we would be changing the output speed only once per cycle, which is the ideal case:

While...
//Logic protections...
//Limits:
IF currentSpeed + speedFactor > MaxSpeedAllowed THEN
currentSpeed := MaxSpeedAllowed;

ELSE_IF currentSpeed - SpeedFactor < MinSpeedAllowed THEN
currentSpeed := MinSpeedAllowed;
ELSE
//Acceleration iterations...
//Deceleration iterations...
endWhile;

He got annoyed and told me to "stop asking stupid questions". I would like to have your opinion because I am getting tired of this guy and I just want to make him swallow his ego.
In terms of performance and efficiency, your version of the code with conditional statements is likely to be more efficient than your boss's original code. This is because your version only updates the output speed once per cycle, while your boss's version updates it potentially multiple times per cycle, depending on how many iterations are executed.

Furthermore, your version of the code is more concise and easier to read, which can make it easier to maintain in the long run. However, it's important to note that there may be other considerations at play, such as the specific requirements of your system or other technical constraints, that may affect the best approach.

In terms of how to handle the situation with your boss, it's important to approach the discussion in a professional and respectful manner. It's possible that your boss may have reasons for using their original approach that you're not aware of, and getting defensive or confrontational is unlikely to be productive. Instead, you could try to have a constructive conversation where you explain your reasoning for the alternative approach and see if you can come to a mutually beneficial solution. Ultimately, the goal should be to improve the quality of the code and the performance of the system, not to prove who is right or wrong.

Best solution is to work for yourself until you can quit your job , then Nobody will question you i am quitting programming soon moving into trading
 
Joined
Sep 21, 2022
Messages
121
Reaction score
15
Your boss's logic is clear.

As for your logic:

It is not clear exactly what speedFactor is.

Adding or subtracting a speed factor to a speed, suggests its not really a factor.

If the current speed is MinSpeedAllowed, the motor can not accelerate (perform the acceleration iterations) until speedFactor is zero or negative. Both make no sense.

Bear in mind that Kuncode is probably using a chat bot. If you stated your logic has more chocolate eggs, it would agree with you.
 
Joined
Feb 17, 2023
Messages
2
Reaction score
0
Your boss's logic is clear.

As for your logic:

It is not clear exactly what speedFactor is.

Adding or subtracting a speed factor to a speed, suggests its not really a factor.

If the current speed is MinSpeedAllowed, the motor can not accelerate (perform the acceleration iterations) until speedFactor is zero or negative. Both make no sense.

Bear in mind that Kuncode is probably using a chat bot. If you stated your logic has more chocolate eggs, it would agree with you.
Yes, I realized my logic issue after I wrote the whole program to see if I am right or my code was bad.

The issue would be solved by writing the conditionals as follows:
------------_
IF accelerationMode == TRUE && (currentSpeed + speedChange > MaxSpeedAllowed) THEN
currentSpeed := MaxSpeedAllowed;
ELSE_IF accelerationMode == TRUE && (currentSpeed + speedChange <= MaxSpeedAllowed) THEN
//Acceleration
ELSE_IF accelerationMode == FALSE && (currentSpeed - speedChange < MinSpeedAllowed) THEN
currentSpeed := MinSpeedAllowed;
ELSE
//Deceleration
------------_

I have to add that my boss is actually a genius if you ask me, and it's a popular joke in the industry that this guy is never wrong, like a Pope

It's not easy to manage the hard comments specially seeing that he won't be open to explaining, but instead being rude. After all he compromised to teach me if I stayed to work in his country. I am always communicating with patience and respect and I expect the same coming the other way.

Anyways, thank you for the reality check and honest reply, this other guy's reply does look like a chatGPT stuff.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top