Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
  • You only need to get the bytes if line[0]=='F' so you should move it inside the if block.

  • If you only need to get these 3 Int16 values, IMHO using Array.Copy, Array.Reverse is a little bit too much.

     while(SerialPort.BytesToRead > 50){
     string line = SerialPort.ReadLine();
     if (line[0]=='F'){
     byte[] encoded = enc.GetBytes(line);
     short value1 = BitConverter.ToInt16(new byte[] {encoded[2] , encoded [1]}, 0);
     short value2 = BitConverter.ToInt16(new byte[] {encoded[4] , encoded [3]}, 0);
     short value3 = BitConverter.ToInt16(new byte[] {encoded[6] , encoded [5]}, 0);
     }
     }
    
  • naming variables value1, value2 and value3 screams for new meaningful names and for a List<T> or any other collection. In this way the shown conversation could be done using a loop.

  • comments like // read line and turn string in byte array does not add any value to the code, because the comment is describing what is done instead of why something is done.

A very good read about comments: http://codereview.stackexchange.com/a/90113/29371 https://codereview.stackexchange.com/a/90113/29371

  • You only need to get the bytes if line[0]=='F' so you should move it inside the if block.

  • If you only need to get these 3 Int16 values, IMHO using Array.Copy, Array.Reverse is a little bit too much.

     while(SerialPort.BytesToRead > 50){
     string line = SerialPort.ReadLine();
     if (line[0]=='F'){
     byte[] encoded = enc.GetBytes(line);
     short value1 = BitConverter.ToInt16(new byte[] {encoded[2] , encoded [1]}, 0);
     short value2 = BitConverter.ToInt16(new byte[] {encoded[4] , encoded [3]}, 0);
     short value3 = BitConverter.ToInt16(new byte[] {encoded[6] , encoded [5]}, 0);
     }
     }
    
  • naming variables value1, value2 and value3 screams for new meaningful names and for a List<T> or any other collection. In this way the shown conversation could be done using a loop.

  • comments like // read line and turn string in byte array does not add any value to the code, because the comment is describing what is done instead of why something is done.

A very good read about comments: http://codereview.stackexchange.com/a/90113/29371

  • You only need to get the bytes if line[0]=='F' so you should move it inside the if block.

  • If you only need to get these 3 Int16 values, IMHO using Array.Copy, Array.Reverse is a little bit too much.

     while(SerialPort.BytesToRead > 50){
     string line = SerialPort.ReadLine();
     if (line[0]=='F'){
     byte[] encoded = enc.GetBytes(line);
     short value1 = BitConverter.ToInt16(new byte[] {encoded[2] , encoded [1]}, 0);
     short value2 = BitConverter.ToInt16(new byte[] {encoded[4] , encoded [3]}, 0);
     short value3 = BitConverter.ToInt16(new byte[] {encoded[6] , encoded [5]}, 0);
     }
     }
    
  • naming variables value1, value2 and value3 screams for new meaningful names and for a List<T> or any other collection. In this way the shown conversation could be done using a loop.

  • comments like // read line and turn string in byte array does not add any value to the code, because the comment is describing what is done instead of why something is done.

A very good read about comments: https://codereview.stackexchange.com/a/90113/29371

fixed typo and removed superfluous blank lines
Source Link
BCdotWEB
  • 11.4k
  • 2
  • 28
  • 45
  • You only need to get the bytes if line[0]=='F' so you should move it inside the if block.

  • If you only need to get these 3 Int16 values, IMHO using Array.Copy, Array.Reverse is a little bit too much.

     while(SerialPort.BytesToRead > 50){
     string line = SerialPort.ReadLine();
     if (line[0]=='F'){
     byte[] encoded = enc.GetBytes(line);
     short value1 = BitConverter.ToInt16(new byte[] {encoded[2] , encoded [1]}, 0);
     short value2 = BitConverter.ToInt16(new byte[] {encoded[4] , encoded [3]}, 0);
     short value3 = BitConverter.ToInt16(new byte[] {encoded[6] , encoded [5]}, 0);
     }
     }
    
  • naming variables value1, value2 and value3 screemsscreams for new meaningful names and for a List<T> or any other collection. In this way the shown conversation could be done using a loop.

  • comments like // read line and turn string in byte array does not add any value to the code, because the comment is describing what is done instead of why something is done.

A very good read about comments: http://codereview.stackexchange.com/a/90113/29371

  • You only need to get the bytes if line[0]=='F' so you should move it inside the if block.

  • If you only need to get these 3 Int16 values, IMHO using Array.Copy, Array.Reverse is a little bit too much.

     while(SerialPort.BytesToRead > 50){
     string line = SerialPort.ReadLine();
     if (line[0]=='F'){
     byte[] encoded = enc.GetBytes(line);
     short value1 = BitConverter.ToInt16(new byte[] {encoded[2] , encoded [1]}, 0);
     short value2 = BitConverter.ToInt16(new byte[] {encoded[4] , encoded [3]}, 0);
     short value3 = BitConverter.ToInt16(new byte[] {encoded[6] , encoded [5]}, 0);
     }
     }
    
  • naming variables value1, value2 and value3 screems for new meaningful names and for a List<T> or any other collection. In this way the shown conversation could be done using a loop.

  • comments like // read line and turn string in byte array does not add any value to the code, because the comment is describing what is done instead of why something is done.

A very good read about comments: http://codereview.stackexchange.com/a/90113/29371

  • You only need to get the bytes if line[0]=='F' so you should move it inside the if block.

  • If you only need to get these 3 Int16 values, IMHO using Array.Copy, Array.Reverse is a little bit too much.

     while(SerialPort.BytesToRead > 50){
     string line = SerialPort.ReadLine();
     if (line[0]=='F'){
     byte[] encoded = enc.GetBytes(line);
     short value1 = BitConverter.ToInt16(new byte[] {encoded[2] , encoded [1]}, 0);
     short value2 = BitConverter.ToInt16(new byte[] {encoded[4] , encoded [3]}, 0);
     short value3 = BitConverter.ToInt16(new byte[] {encoded[6] , encoded [5]}, 0);
     }
     }
    
  • naming variables value1, value2 and value3 screams for new meaningful names and for a List<T> or any other collection. In this way the shown conversation could be done using a loop.

  • comments like // read line and turn string in byte array does not add any value to the code, because the comment is describing what is done instead of why something is done.

A very good read about comments: http://codereview.stackexchange.com/a/90113/29371

edited body
Source Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177
  • You only need to get the bytes if line[0]=='F' so you should move it inside the if block.

  • If you only need to get these 3 Int16 values, IMHO using Array.Copy, Array.Reverse is a little bit too much.

     while(SerialPort.BytesToRead > 50){
     string line = SerialPort.ReadLine();
     if (line[0]=='F'){
     byte[] encoded = enc.GetBytes(line);
     short value1 = BitConverter.ToInt16(new byte[] {encoded[2] }, encoded [1]}, 0);
     short value2 = BitConverter.ToInt16(new byte[] {encoded[4] }, encoded [3]}, 0);
     short value3 = BitConverter.ToInt16(new byte[] {encoded[6] }, encoded [5]}, 0);
     }
     }
    
  • naming variables value1, value2 and value3 screems for new meaningful names and for a List<T> or any other collection. In this way the shown conversation could be done using a loop.

  • comments like // read line and turn string in byte array does not add any value to the code, because the comment is describing what is done instead of why something is done.

A very good read about comments: http://codereview.stackexchange.com/a/90113/29371

  • You only need to get the bytes if line[0]=='F' so you should move it inside the if block.

  • If you only need to get these 3 Int16 values, IMHO using Array.Copy, Array.Reverse is a little bit too much.

     while(SerialPort.BytesToRead > 50){
     string line = SerialPort.ReadLine();
     if (line[0]=='F'){
     byte[] encoded = enc.GetBytes(line);
     short value1 = BitConverter.ToInt16(new byte[] {encoded[2] }, encoded [1], 0);
     short value2 = BitConverter.ToInt16(new byte[] {encoded[4] }, encoded [3], 0);
     short value3 = BitConverter.ToInt16(new byte[] {encoded[6] }, encoded [5], 0);
     }
     }
    
  • naming variables value1, value2 and value3 screems for new meaningful names and for a List<T> or any other collection. In this way the shown conversation could be done using a loop.

  • comments like // read line and turn string in byte array does not add any value to the code, because the comment is describing what is done instead of why something is done.

A very good read about comments: http://codereview.stackexchange.com/a/90113/29371

  • You only need to get the bytes if line[0]=='F' so you should move it inside the if block.

  • If you only need to get these 3 Int16 values, IMHO using Array.Copy, Array.Reverse is a little bit too much.

     while(SerialPort.BytesToRead > 50){
     string line = SerialPort.ReadLine();
     if (line[0]=='F'){
     byte[] encoded = enc.GetBytes(line);
     short value1 = BitConverter.ToInt16(new byte[] {encoded[2] , encoded [1]}, 0);
     short value2 = BitConverter.ToInt16(new byte[] {encoded[4] , encoded [3]}, 0);
     short value3 = BitConverter.ToInt16(new byte[] {encoded[6] , encoded [5]}, 0);
     }
     }
    
  • naming variables value1, value2 and value3 screems for new meaningful names and for a List<T> or any other collection. In this way the shown conversation could be done using a loop.

  • comments like // read line and turn string in byte array does not add any value to the code, because the comment is describing what is done instead of why something is done.

A very good read about comments: http://codereview.stackexchange.com/a/90113/29371

added 4 characters in body
Source Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177
Loading
Source Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /