HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Simple dishwasher C++ code

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
codesimpledishwasher

Problem

I have a project in embedded systems and I have to write the code in C, and make it simple.

This is my first time, and it's only a simulation of how it should work.

#include
#include

void manualMode();
 void defaultMode();

int temp, timeToWash, operation;

int main() {
     int mode =0, part=3, waterLevel;
     int doorSensor=0, timer = 3,waterLevelSensor = 0, tempSensor = 25;
     char startPause;
     cout>mode;
      if (mode == 1)
                 defaultMode();
      if (mode == 2)
                 manualMode();
 }while(mode == 0);

 cout>part;
 switch(part){
            case 1:
            case 2:
                 waterLevel= 0.5;
                 break;
            case 3:
                 waterLevel = 1;
                 break;
 }
 if (doorSensor == 0){
      do{
                 cout>startPause;
                 cout>temp;
     cout>timeToWash;
     cout>operation;
}
void defaultMode(){
     int whatToWash;
     cout>whatToWash;
     switch(whatToWash){
          case 1:
                temp = 30;
                timeToWash = 3;
                break;
          case 2:
                temp = 40;
                timeToWash = 3;
                break;
          case 3:
                temp = 60;
                timeToWash = 3;
                break;
     }
 }

Solution

I would have some suggestions for improvements regarding C++ in general (I'm not familiar with embedded systems).

Indentation

Make sure to use uniform indentation across the code, in order to make it more readable. (E.g., the do-while loop, the switch-block in the main method should be at the same level as the declarations at the beginning.)

Code organization

It is very good, that manual mode and default mode are two separate functions. Breaking down the code makes it easier to read. I would also export some other parts to separate functions, especially the main cycle. (Remark: this is a generic principle. It might be that in the context of embedded systems there are some further considerations, which I'm not aware of, e.g. that code should be as fast as possible, for which this advice does not apply. Please consider this.)

Variable initialization

I would initialize each variable explicitly to some default value (e.g. 0), even if it is sure to be initialized later. In this way, you can for sure prevent undefined behavior (e.g., in case the code gets modified later, and a path is introduced where the variable is not initialized anymore).

Control flow

In the do-while loop for the washing-cycle, startPause is read, but then, at the end of the loop, it is set to a constant value (independently of what has been read). So this loop only runs once. I think either the whole logic should be removed, or improved in a way that looping really depends on the input value.

Input validation

In more places, input is required from the user, but it is never checked if it is within the range of the allowed values. In a production system this would be a must, but also in your case, I would clarify if this is required.

Context

StackExchange Code Review Q#79203, answer score: 4

Revisions (0)

No revisions yet.