Coding ... is there any advantage to write a sketch like this?

Hello,

I am an old newbie... (done 3 project which are all working, using Nanos, I2C LCD, Dotmatrix display, Neo GPS, PM2.5 meter, ... ... ...)
BUT
I have a question regarding writing sketches...

What is the advantage to write a Sketch as follows? (extract from a sketch)

void setup() {
lcd.init(); 
lcd.backlight(); 
Serial.begin(115200); 
gpsPort.begin(9600);
}

or like the foillowing?

void setup() {
  lcd.init(); lcd.backlight(); Serial.begin(115200); gpsPort.begin(9600);{

I tested both version and they both work fine.

Thanks for a response,

Eric

The advantage is that you can read and follow the code blocks more easily

I am willing to bet that the second version, as posted, does not compile due to the last character. This rather makes the point I think

1 Like

The advantage of the second method is that is saves paper on the line printer. If I remember, you can usually squeeze up to 132 characters on to each line. Come to think of it, I have not seen much lately of that green/white scored paper with sprocket holes.

1 Like

The big advantage is that you can spend hours figuring out what "expected ) before ; in line 12" could possibly mean. So the second aproach is benefitial for beginners: it teaches you the worst possible practice and lets you suffer the most why trying to debug it.

True... you won the bet...
It should be like as follows...

void setup() {
  lcd.init(); lcd.backlight(); Serial.begin(115200); gpsPort.begin(9600);}

So if one day we meet, I pay for the Pizza...

This is what happens, if one gets pressured (to get ready) by the wife who wants to go to the supermarket...
Quick quick quick... :smile:

You mean this printer?
I saw one, long time ago...
https://m.media-amazon.com/images/I/61ACuorLZ+L.AC_UF350,350_QL80_DpWeblab.jpg

The first method makes it a lot easier to comment out a line.

Didn't really "squeeze" the characters onto the line, the paper was 14 7/8" wide and typically used 10 characters/inch.

of course you haven't. That type was just after the extinction of 6V6GT (6L6, 8BQ5...) :slightly_smiling_face:

3 different types you got there, mind you. 6L6 was/is a lot beefier than 6V6 and 6BQ5. The latter are also different, the former being a beam tetrode and the latter a pentode. Let's not go into sonic differences given the risk of unleashing an armed conflict.

More like this one: IBM 1403 - Wikipedia
Which would be more period correct with the 6V6 anyway...

Three common activities in programming are

  • reading code: proper indentation helps
  • diffs in version control: seeing which statements have been inserted and removed
  • debugging with breakpoints: setting them and stepping through code

Putting each statement on its own line is better for all of them.

what do you think the advantage of putting everything on one line might be?

abiding by some style that is common gets you use to reading code and seeing patterns that are easily recognizable. That makes it easier to read and debug code without looking at it in detail. Bugs tend to stand out

Btw, there are sometimes valid reasons to bunch a couple of things together in one line. We generally always do so in for-loops:
for (uint8_t i = 0; i < xyz; i ++)
instead of

for (
  uint8_t i = 0;
  i < xyz;
  i ++;)

It can also be useful in switch/cases if they involve little code for each case, in which case it helps readability (or rather, 'scrollability'):

switch (rt_state) {
  case CW_FAST : count += 10; break;
  case CW_SLOW : count +=  1; break;
  case CCW_FAST: count -= 10; break;
  case CCW_SLOW: count -=  1; break;
}

So much for the devil's advocate...

I remember writing an assembly program to clean the type train before printing letters to policy holders for an insurance company. The machine was a 360 mod 40 running three partitions. The standard program to clean the print characters would only run stand-alone, so all partitions and work had to be finished. The field engineer gave me a direct warning to never, ever print so adjacent characters on a three character train segment were printed sequentially. The train could explode because the train would be completely stopped. It worked and everyone was happy.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.