Topics

Coding styles


Jack, W8TEE
 

Absolutely agree. Some of us can't remember what we had for breakfast, let alone code we wrote six months ago. I've actually read code on line and said to myself: "I get it!" only to realize it was my code.

Jack, W8TEE


On Sunday, May 6, 2018, 4:36:36 PM EDT, Tim Gorman <tgorman2@...> wrote:


A man after my own heart!

When you are called in to fix a problem with your code two years after
you wrote it then it becomes apparent quickly that simpler is better.

It's why I always put the "{" on the line following the if statement. I
can print the code out, put a ruler on the printout and tick off all
the beginnings and ends of sections. I know the current preferred method
is to put the starting "{" on the same line as the if statement (or any
other thing you can think of) but I've just had much quicker success
finding problems doing it my way. Especially when someone else has been
modifying my code.

Same with the paragraph comment indicators, /* and */. Always on a line
of their own.

tim ab0wr

On Sun, 6 May 2018 18:09:21 +0000 (UTC)
"Jack Purdum via Groups.Io" <jjpurdum=yahoo.com@groups.io> wrote:

> (Actually, it's the post-decrement operator, not the decrement
> operator.) I wasn't saying I don't know what the post-decrement
> operator does, I was just asking why make the code harder to read by
> using it there. Moving it to the next line makes the code easier to
> read and has no impact on the way it works or the generated code.
> Anything the programmer can do to make the code easier to read and
> that is performance-neutral should be done. That's why I almost never
> use the ternary operator: It's almost always easier to read a simple
> if-else statement block and the generated code's the same.
>
> Jack, W8TEE

>






Michael Monteith
 

 I definitely put brackets on the following lines.  In particular a lot of IDEs and even a lot of editors will do matching brackets for you.  In that case you can see quickly where the begin and end are.   On some of them they have vertical ghost tab lines and easy to look up and down and see where the align.  Not to mention if you forget one it's easy to track back. 

 Michael
KM4OLT


Jack, W8TEE
 

Braces or brackets? Braces mark statement/function blocks while brackets are most often used with array sizes. The old K&R style was to leave the opening brace on the same line as the expression block, and then align the closing brace with the expression block start. I think that was done to get more lines on the screen when a 25 line display was common. Today, most seem to place the opening brace on its own line. If the block spans more than a page, the latest IDE shows the opening expression.

You can also use Ctrl-T to format your code to a common C coding style.

Jack, W8TEE


On Sunday, May 6, 2018, 7:27:08 PM EDT, Michael Monteith via Groups.Io <michael_r_monteith@...> wrote:


 I definitely put brackets on the following lines.  In particular a lot of IDEs and even a lot of editors will do matching brackets for you.  In that case you can see quickly where the begin and end are.   On some of them they have vertical ghost tab lines and easy to look up and down and see where the align.  Not to mention if you forget one it's easy to track back. 

 Michael
KM4OLT


Jerry Gaffke
 

You guys take this so seriously.
Maybe you should find a hobby.   ;-)

Though I understand, I'm afflicted by the same malady.
Just a slightly different strain.
Here's a fix, no need to ever again deal with my coding style (one of many possible tools):
    http://uncrustify.sourceforge.net/ 

If you object to K&R style, there's a lot of it out there for you to sic crustify on.
Here's a few million lines to get you started:
    https://github.com/torvalds/linux

> You can also use Ctrl-T to format your code to a common C coding style.

I assume that's for the Arduino IDE, whose editor I mostly avoid.
Here's various tricks for vi/vim users:
    https://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim

Here's a discussion of the various indentation styles:
    https://softwareengineering.stackexchange.com/questions/99543/what-is-the-difference-between-kr-and-one-true-brace-style-1tbs-styles
They can't agree either.
But somebody in there did a study of error rates in code using the various styles.
Note that K&R style won, at least by a little bit.
Though perhaps that's because folks using the K&R style
are more likely to have read K&R (highly recommended).

I was quite serious about that back pocket thing.
If I have a difficult algorithm to work on, I code it tight so I can see
as much of the work at one time as possible.  Preferably so it all fits
on one side of a sheet of typing paper.  Better yet, half of one side.
Then pull it out at odd times during the day, jot notes when new insights come.
Works for me.

Didn't anybody have any comments about on how better to compute SWR? 

Jerry, KE7ER


On Sun, May 6, 2018 at 05:31 pm, Jack Purdum wrote:
Braces or brackets? Braces mark statement/function blocks while brackets are most often used with array sizes. The old K&R style was to leave the opening brace on the same line as the expression block, and then align the closing brace with the expression block start. I think that was done to get more lines on the screen when a 25 line display was common. Today, most seem to place the opening brace on its own line. If the block spans more than a page, the latest IDE shows the opening expression.

You can also use Ctrl-T to format your code to a common C coding style.
 


Jack, W8TEE
 

The brace thing is really a matter of choice. I learned C back in the late 1970's when K&R was the only C book and I had a 64x16 modified TV for a CRT, so I put things on the same line, simply so I could see more lines without scrolling:

   if (w == 5) {
      y = true;
   } else {
      y = false;
   }

Even though the braces are not required with a single statement, I almost always use them. When I don't, invariably I need to add another statement or a debug print statement, so I have to add the brace anyway. However, with function signatures, I always place the opening brace for the function body on a new line. I think people pick what works for them. No one style is "correct", so, to me at least, there's no reason to even debate what's correct when it comes to braces. However, we should all try to make our code as readable as possible.

When I was in high school, the football coach knew I was a ham radio operator and wanted me to build two Heathkit walkie talkies. I built them, and they didn't work. I was mortified. I barely slept that night. The next morning, my Mom told me I got up in the middle of the night and wrote something on the pad next to the phone. I read what I wrote, went downstairs and checked it and, sure enough, that was the problem. Ever since then, I keep a pad and pencil next to the bed. I can't tell you how many teaching examples I've used that came from that pad or programming problems that were solved by reading the pad the next morning. My experiences have convinced me that your brain continues to problem solve even when you're asleep.

Jack, W8TEE


On Sunday, May 6, 2018, 11:51:40 PM EDT, Jerry Gaffke via Groups.Io <jgaffke@...> wrote:


You guys take this so seriously.
Maybe you should find a hobby.   ;-)

Though I understand, I'm afflicted by the same malady.
Just a slightly different strain.
Here's a fix, no need to ever again deal with my coding style (one of many possible tools):
    http://uncrustify.sourceforge.net/ 

If you object to K&R style, there's a lot of it out there for you to sic crustify on.
Here's a few million lines to get you started:
    https://github.com/torvalds/linux

> You can also use Ctrl-T to format your code to a common C coding style.

I assume that's for the Arduino IDE, whose editor I mostly avoid.
Here's various tricks for vi/vim users:
    https://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim

Here's a discussion of the various indentation styles:
    https://softwareengineering.stackexchange.com/questions/99543/what-is-the-difference-between-kr-and-one-true-brace-style-1tbs-styles
They can't agree either.
But somebody in there did a study of error rates in code using the various styles.
Note that K&R style won, at least by a little bit.
Though perhaps that's because folks using the K&R style
are more likely to have read K&R (highly recommended).

I was quite serious about that back pocket thing.
If I have a difficult algorithm to work on, I code it tight so I can see
as much of the work at one time as possible.  Preferably so it all fits
on one side of a sheet of typing paper.  Better yet, half of one side.
Then pull it out at odd times during the day, jot notes when new insights come.
Works for me.

Didn't anybody have any comments about on how better to compute SWR? 

Jerry, KE7ER


On Sun, May 6, 2018 at 05:31 pm, Jack Purdum wrote:
Braces or brackets? Braces mark statement/function blocks while brackets are most often used with array sizes. The old K&R style was to leave the opening brace on the same line as the expression block, and then align the closing brace with the expression block start. I think that was done to get more lines on the screen when a 25 line display was common. Today, most seem to place the opening brace on its own line. If the block spans more than a page, the latest IDE shows the opening expression.

You can also use Ctrl-T to format your code to a common C coding style.
 


Ashhar Farhan
 

64x16? K&R?? Surely, you are not talking floppy shuffle on the Xerox 820 or (jerry pournelle, peace be upon him) a Kaypro?
BDS C was the only game in town. I brought up a CP/M system for my undergraduate project work. 
- f
- f

On Mon, 7 May 2018, 09:44 Jack Purdum via Groups.Io, <jjpurdum=yahoo.com@groups.io> wrote:
The brace thing is really a matter of choice. I learned C back in the late 1970's when K&R was the only C book and I had a 64x16 modified TV for a CRT, so I put things on the same line, simply so I could see more lines without scrolling:

   if (w == 5) {
      y = true;
   } else {
      y = false;
   }

Even though the braces are not required with a single statement, I almost always use them. When I don't, invariably I need to add another statement or a debug print statement, so I have to add the brace anyway. However, with function signatures, I always place the opening brace for the function body on a new line. I think people pick what works for them. No one style is "correct", so, to me at least, there's no reason to even debate what's correct when it comes to braces. However, we should all try to make our code as readable as possible.

When I was in high school, the football coach knew I was a ham radio operator and wanted me to build two Heathkit walkie talkies. I built them, and they didn't work. I was mortified. I barely slept that night. The next morning, my Mom told me I got up in the middle of the night and wrote something on the pad next to the phone. I read what I wrote, went downstairs and checked it and, sure enough, that was the problem. Ever since then, I keep a pad and pencil next to the bed. I can't tell you how many teaching examples I've used that came from that pad or programming problems that were solved by reading the pad the next morning. My experiences have convinced me that your brain continues to problem solve even when you're asleep.

Jack, W8TEE


On Sunday, May 6, 2018, 11:51:40 PM EDT, Jerry Gaffke via Groups.Io <jgaffke=yahoo.com@groups.io> wrote:


You guys take this so seriously.
Maybe you should find a hobby.   ;-)

Though I understand, I'm afflicted by the same malady.
Just a slightly different strain.
Here's a fix, no need to ever again deal with my coding style (one of many possible tools):
    http://uncrustify.sourceforge.net/ 

If you object to K&R style, there's a lot of it out there for you to sic crustify on.
Here's a few million lines to get you started:
    https://github.com/torvalds/linux

> You can also use Ctrl-T to format your code to a common C coding style.

I assume that's for the Arduino IDE, whose editor I mostly avoid.
Here's various tricks for vi/vim users:
    https://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim

Here's a discussion of the various indentation styles:
    https://softwareengineering.stackexchange.com/questions/99543/what-is-the-difference-between-kr-and-one-true-brace-style-1tbs-styles
They can't agree either.
But somebody in there did a study of error rates in code using the various styles.
Note that K&R style won, at least by a little bit.
Though perhaps that's because folks using the K&R style
are more likely to have read K&R (highly recommended).

I was quite serious about that back pocket thing.
If I have a difficult algorithm to work on, I code it tight so I can see
as much of the work at one time as possible.  Preferably so it all fits
on one side of a sheet of typing paper.  Better yet, half of one side.
Then pull it out at odd times during the day, jot notes when new insights come.
Works for me.

Didn't anybody have any comments about on how better to compute SWR? 

Jerry, KE7ER


On Sun, May 6, 2018 at 05:31 pm, Jack Purdum wrote:
Braces or brackets? Braces mark statement/function blocks while brackets are most often used with array sizes. The old K&R style was to leave the opening brace on the same line as the expression block, and then align the closing brace with the expression block start. I think that was done to get more lines on the screen when a 25 line display was common. Today, most seem to place the opening brace on its own line. If the block spans more than a page, the latest IDE shows the opening expression.

You can also use Ctrl-T to format your code to a common C coding style.
 


Dennis Zabawa
 

I once published some code on-line and got an "improved" version of it emailed to me by a self-proclaimed, "expert" programmer.  His improvement was to remove all the comments and shorten all the variable and procedure/function names to make it "run faster".  I guess the "expert" didn't understand that the compiled code will be the same regardless of the things he had changed.  Albeit, it would compile faster.

My advice is  to comment, comment, comment.  Use variable and procedure names that are descriptive.  As Jack said, that clever, little bit of coding you did last week may read like sanskrit  today.  


pat griffin
 

I tried not to weigh in on this but gave up. Years ago managing a group of developers and support people, I would drive the hot shots crazy. A programmer would write a really elegant piece of code only to have me say "the support people can't handle this.  Go back and Beowulf it up." That meant write it less elegantly but more understandable to someone not as smart as you.  One programmer I remember would  always ask,  "In longhand?" Those were the days.  We worked hard, laughed a lot, ate a lot of pizza and drank all the beer.

73, Pat  AA4PG


http://www.cahabatechnology.com


From: BITX20@groups.io <BITX20@groups.io> on behalf of Jack Purdum via Groups.Io <jjpurdum@...>
Sent: Sunday, May 6, 2018 10:14:44 PM
To: BITX20@groups.io
Subject: Re: [BITX20] Coding styles
 
The brace thing is really a matter of choice. I learned C back in the late 1970's when K&R was the only C book and I had a 64x16 modified TV for a CRT, so I put things on the same line, simply so I could see more lines without scrolling:

   if (w == 5) {
      y = true;
   } else {
      y = false;
   }

Even though the braces are not required with a single statement, I almost always use them. When I don't, invariably I need to add another statement or a debug print statement, so I have to add the brace anyway. However, with function signatures, I always place the opening brace for the function body on a new line. I think people pick what works for them. No one style is "correct", so, to me at least, there's no reason to even debate what's correct when it comes to braces. However, we should all try to make our code as readable as possible.

When I was in high school, the football coach knew I was a ham radio operator and wanted me to build two Heathkit walkie talkies. I built them, and they didn't work. I was mortified. I barely slept that night. The next morning, my Mom told me I got up in the middle of the night and wrote something on the pad next to the phone. I read what I wrote, went downstairs and checked it and, sure enough, that was the problem. Ever since then, I keep a pad and pencil next to the bed. I can't tell you how many teaching examples I've used that came from that pad or programming problems that were solved by reading the pad the next morning. My experiences have convinced me that your brain continues to problem solve even when you're asleep.

Jack, W8TEE


On Sunday, May 6, 2018, 11:51:40 PM EDT, Jerry Gaffke via Groups.Io <jgaffke@...> wrote:


You guys take this so seriously.
Maybe you should find a hobby.   ;-)

Though I understand, I'm afflicted by the same malady.
Just a slightly different strain.
Here's a fix, no need to ever again deal with my coding style (one of many possible tools):
    http://uncrustify.sourceforge.net/ 

If you object to K&R style, there's a lot of it out there for you to sic crustify on.
Here's a few million lines to get you started:
    https://github.com/torvalds/linux

> You can also use Ctrl-T to format your code to a common C coding style.

I assume that's for the Arduino IDE, whose editor I mostly avoid.
Here's various tricks for vi/vim users:
    https://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim

Here's a discussion of the various indentation styles:
    https://softwareengineering.stackexchange.com/questions/99543/what-is-the-difference-between-kr-and-one-true-brace-style-1tbs-styles
They can't agree either.
But somebody in there did a study of error rates in code using the various styles.
Note that K&R style won, at least by a little bit.
Though perhaps that's because folks using the K&R style
are more likely to have read K&R (highly recommended).

I was quite serious about that back pocket thing.
If I have a difficult algorithm to work on, I code it tight so I can see
as much of the work at one time as possible.  Preferably so it all fits
on one side of a sheet of typing paper.  Better yet, half of one side.
Then pull it out at odd times during the day, jot notes when new insights come.
Works for me.

Didn't anybody have any comments about on how better to compute SWR? 

Jerry, KE7ER


On Sun, May 6, 2018 at 05:31 pm, Jack Purdum wrote:
Braces or brackets? Braces mark statement/function blocks while brackets are most often used with array sizes. The old K&R style was to leave the opening brace on the same line as the expression block, and then align the closing brace with the expression block start. I think that was done to get more lines on the screen when a 25 line display was common. Today, most seem to place the opening brace on its own line. If the block spans more than a page, the latest IDE shows the opening expression.

You can also use Ctrl-T to format your code to a common C coding style.
 


Jack, W8TEE
 

HA!

I had my own software company for 17 years, writing programming tools (e.g., an MSDOS C compiler, linker, assembler, IDE, etc.) It was a small company so "programming teams" usually were only 2 people. Each team would be given a task to complete and a timeline, usually 2-3 weeks. Then we would meet on Friday morning for a code walk-thru. Every programmer had to attend. If the team didn't follow the company style guidelines, they had to buy beer and pizza for everyone after the walk-thru.

The code walk-thru's were good because it made it easy for someone to fill in if someone else was on vacation or ill...a real problem for a small company if everyone isn't at least familiar with the work of others. The carrot: If the team passed the walk-thru with no changes, I bought pizza and beer and all of the programmers got the afternoon off. You cannot imagine how many times I went to the office on Thursday night before a walk-thru so see ALL of the programmers still there helping the team with the code. I felt I got far and above the time back for the afternoon off.

Funny how Jolt Cola, beer, and pizza pushes people to do things they probably wouldn't if you just straight-out asked them.

Jack, W8TEE


On Monday, May 7, 2018, 9:09:32 AM EDT, pat griffin <patgriffin@...> wrote:


I tried not to weigh in on this but gave up. Years ago managing a group of developers and support people, I would drive the hot shots crazy. A programmer would write a really elegant piece of code only to have me say "the support people can't handle this.  Go back and Beowulf it up." That meant write it less elegantly but more understandable to someone not as smart as you.  One programmer I remember would  always ask,  "In longhand?" Those were the days.  We worked hard, laughed a lot, ate a lot of pizza and drank all the beer.

73, Pat  AA4PG


http://www.cahabatechnology.com

From: BITX20@groups.io <BITX20@groups.io> on behalf of Jack Purdum via Groups.Io <jjpurdum@...>
Sent: Sunday, May 6, 2018 10:14:44 PM
To: BITX20@groups.io
Subject: Re: [BITX20] Coding styles
 
The brace thing is really a matter of choice. I learned C back in the late 1970's when K&R was the only C book and I had a 64x16 modified TV for a CRT, so I put things on the same line, simply so I could see more lines without scrolling:

   if (w == 5) {
      y = true;
   } else {
      y = false;
   }

Even though the braces are not required with a single statement, I almost always use them. When I don't, invariably I need to add another statement or a debug print statement, so I have to add the brace anyway. However, with function signatures, I always place the opening brace for the function body on a new line. I think people pick what works for them. No one style is "correct", so, to me at least, there's no reason to even debate what's correct when it comes to braces. However, we should all try to make our code as readable as possible.

When I was in high school, the football coach knew I was a ham radio operator and wanted me to build two Heathkit walkie talkies. I built them, and they didn't work. I was mortified. I barely slept that night. The next morning, my Mom told me I got up in the middle of the night and wrote something on the pad next to the phone. I read what I wrote, went downstairs and checked it and, sure enough, that was the problem. Ever since then, I keep a pad and pencil next to the bed. I can't tell you how many teaching examples I've used that came from that pad or programming problems that were solved by reading the pad the next morning. My experiences have convinced me that your brain continues to problem solve even when you're asleep.

Jack, W8TEE


On Sunday, May 6, 2018, 11:51:40 PM EDT, Jerry Gaffke via Groups.Io <jgaffke@...> wrote:


You guys take this so seriously.
Maybe you should find a hobby.   ;-)

Though I understand, I'm afflicted by the same malady.
Just a slightly different strain.
Here's a fix, no need to ever again deal with my coding style (one of many possible tools):
    http://uncrustify.sourceforge.net/ 

If you object to K&R style, there's a lot of it out there for you to sic crustify on.
Here's a few million lines to get you started:
    https://github.com/torvalds/linux

> You can also use Ctrl-T to format your code to a common C coding style.

I assume that's for the Arduino IDE, whose editor I mostly avoid.
Here's various tricks for vi/vim users:
    https://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim

Here's a discussion of the various indentation styles:
    https://softwareengineering.stackexchange.com/questions/99543/what-is-the-difference-between-kr-and-one-true-brace-style-1tbs-styles
They can't agree either.
But somebody in there did a study of error rates in code using the various styles.
Note that K&R style won, at least by a little bit.
Though perhaps that's because folks using the K&R style
are more likely to have read K&R (highly recommended).

I was quite serious about that back pocket thing.
If I have a difficult algorithm to work on, I code it tight so I can see
as much of the work at one time as possible.  Preferably so it all fits
on one side of a sheet of typing paper.  Better yet, half of one side.
Then pull it out at odd times during the day, jot notes when new insights come.
Works for me.

Didn't anybody have any comments about on how better to compute SWR? 

Jerry, KE7ER


On Sun, May 6, 2018 at 05:31 pm, Jack Purdum wrote:
Braces or brackets? Braces mark statement/function blocks while brackets are most often used with array sizes. The old K&R style was to leave the opening brace on the same line as the expression block, and then align the closing brace with the expression block start. I think that was done to get more lines on the screen when a 25 line display was common. Today, most seem to place the opening brace on its own line. If the block spans more than a page, the latest IDE shows the opening expression.

You can also use Ctrl-T to format your code to a common C coding style.
 


Vince Vielhaber
 

I prefer:

y = (w == 5) ? true : false;

Vince.

On 05/07/2018 12:14 AM, Jack Purdum via Groups.Io wrote:
The brace thing is really a matter of choice. I learned C back in the
late 1970's when K&R was the only C book and I had a 64x16 modified TV
for a CRT, so I put things on the same line, simply so I could see more
lines without scrolling:

if (w == 5) {
y = true;
} else {
y = false;
}

Even though the braces are not required with a single statement, I
almost always use them. When I don't, invariably I need to add another
statement or a debug print statement, so I have to add the brace anyway.
However, with function signatures, I always place the opening brace for
the function body on a new line. I think people pick what works for
them. No one style is "correct", so, to me at least, there's no reason
to even debate what's correct when it comes to braces. However, we
should all try to make our code as readable as possible.

When I was in high school, the football coach knew I was a ham radio
operator and wanted me to build two Heathkit walkie talkies. I built
them, and they didn't work. I was mortified. I barely slept that night.
The next morning, my Mom told me I got up in the middle of the night and
wrote something on the pad next to the phone. I read what I wrote, went
downstairs and checked it and, sure enough, that was the problem. Ever
since then, I keep a pad and pencil next to the bed. I can't tell you
how many teaching examples I've used that came from that pad or
programming problems that were solved by reading the pad the next
morning. My experiences have convinced me that your brain continues to
problem solve even when you're asleep.

Jack, W8TEE


On Sunday, May 6, 2018, 11:51:40 PM EDT, Jerry Gaffke via Groups.Io
<jgaffke=yahoo.com@groups.io> wrote:


You guys take this so seriously.
Maybe you should find a hobby. ;-)

Though I understand, I'm afflicted by the same malady.
Just a slightly different strain.
Here's a fix, no need to ever again deal with my coding style (one of
many possible tools):
http://uncrustify.sourceforge.net/

If you object to K&R style, there's a lot of it out there for you to sic
crustify on.
Here's a few million lines to get you started:
https://github.com/torvalds/linux

You can also use Ctrl-T to format your code to a common C coding style.
I assume that's for the Arduino IDE, whose editor I mostly avoid.
Here's various tricks for vi/vim users:

https://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim

Here's a discussion of the various indentation styles:

https://softwareengineering.stackexchange.com/questions/99543/what-is-the-difference-between-kr-and-one-true-brace-style-1tbs-styles
They can't agree either.
But somebody in there did a study of error rates in code using the
various styles.
Note that K&R style won, at least by a little bit.
Though perhaps that's because folks using the K&R style
are more likely to have read K&R (highly recommended).

I was quite serious about that back pocket thing.
If I have a difficult algorithm to work on, I code it tight so I can see
as much of the work at one time as possible. Preferably so it all fits
on one side of a sheet of typing paper. Better yet, half of one side.
Then pull it out at odd times during the day, jot notes when new
insights come.
Works for me.

Didn't anybody have any comments about on how better to compute SWR?

Jerry, KE7ER


On Sun, May 6, 2018 at 05:31 pm, Jack Purdum wrote:

Braces or brackets? Braces mark statement/function blocks while
brackets are most often used with array sizes. The old K&R style was
to leave the opening brace on the same line as the expression block,
and then align the closing brace with the expression block start. I
think that was done to get more lines on the screen when a 25 line
display was common. Today, most seem to place the opening brace on
its own line. If the block spans more than a page, the latest IDE
shows the opening expression.

You can also use Ctrl-T to format your code to a common C coding style.



Jack, W8TEE
 

Yep. My first computer was a SOL-20 kit with a modified Hitachi TV for a monitor. I knew Leor Zollman when he was a student at MIT quite well and tried to get him to add floating point to his BDS (Brain-Dead Software) C compiler. (My company sold a statistics package called Microstat and I wanted to convert it to C. The Whitesmith's compiler floating point was so slow, it was not viable.) He didn't have time, so we wrote our own full C compiler (i.e., with fp and structures). We demo-ed the C version Microstat at a show in Chicago and we were surprised to find out that more people wanted to know about the compiler we used than Microstat. So we started selling the Eco-C88 C compiler. It was the first C compiler with an IDE (yes, even before Turbo C) that had the editor, compiler, assembler, and linker all in one package. Back then, most C compilers gave error messages like: "Error 507:323", and you had to look up what error 507 was at line 323. Our IDE had an error window at the bottom of the screen that would say something like: "Duplicate definition error for comPort at line 323. P54." The "P54 was a page number in the C Programming Guide book where you could go for a discussion of what the error was.

Fun times...

Jack, W8TEE


On Monday, May 7, 2018, 1:44:42 AM EDT, Ashhar Farhan <farhanbox@...> wrote:


64x16? K&R?? Surely, you are not talking floppy shuffle on the Xerox 820 or (jerry pournelle, peace be upon him) a Kaypro?
BDS C was the only game in town. I brought up a CP/M system for my undergraduate project work. 
- f
- f

On Mon, 7 May 2018, 09:44 Jack Purdum via Groups.Io, <jjpurdum=yahoo.com@groups.io> wrote:
The brace thing is really a matter of choice. I learned C back in the late 1970's when K&R was the only C book and I had a 64x16 modified TV for a CRT, so I put things on the same line, simply so I could see more lines without scrolling:

   if (w == 5) {
      y = true;
   } else {
      y = false;
   }

Even though the braces are not required with a single statement, I almost always use them. When I don't, invariably I need to add another statement or a debug print statement, so I have to add the brace anyway. However, with function signatures, I always place the opening brace for the function body on a new line. I think people pick what works for them. No one style is "correct", so, to me at least, there's no reason to even debate what's correct when it comes to braces. However, we should all try to make our code as readable as possible.

When I was in high school, the football coach knew I was a ham radio operator and wanted me to build two Heathkit walkie talkies. I built them, and they didn't work. I was mortified. I barely slept that night. The next morning, my Mom told me I got up in the middle of the night and wrote something on the pad next to the phone. I read what I wrote, went downstairs and checked it and, sure enough, that was the problem. Ever since then, I keep a pad and pencil next to the bed. I can't tell you how many teaching examples I've used that came from that pad or programming problems that were solved by reading the pad the next morning. My experiences have convinced me that your brain continues to problem solve even when you're asleep.

Jack, W8TEE


On Sunday, May 6, 2018, 11:51:40 PM EDT, Jerry Gaffke via Groups.Io <jgaffke=yahoo.com@groups.io> wrote:


You guys take this so seriously.
Maybe you should find a hobby.   ;-)

Though I understand, I'm afflicted by the same malady.
Just a slightly different strain.
Here's a fix, no need to ever again deal with my coding style (one of many possible tools):
    http://uncrustify.sourceforge.net/ 

If you object to K&R style, there's a lot of it out there for you to sic crustify on.
Here's a few million lines to get you started:
    https://github.com/torvalds/linux

> You can also use Ctrl-T to format your code to a common C coding style.

I assume that's for the Arduino IDE, whose editor I mostly avoid.
Here's various tricks for vi/vim users:
    https://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim

Here's a discussion of the various indentation styles:
    https://softwareengineering.stackexchange.com/questions/99543/what-is-the-difference-between-kr-and-one-true-brace-style-1tbs-styles
They can't agree either.
But somebody in there did a study of error rates in code using the various styles.
Note that K&R style won, at least by a little bit.
Though perhaps that's because folks using the K&R style
are more likely to have read K&R (highly recommended).

I was quite serious about that back pocket thing.
If I have a difficult algorithm to work on, I code it tight so I can see
as much of the work at one time as possible.  Preferably so it all fits
on one side of a sheet of typing paper.  Better yet, half of one side.
Then pull it out at odd times during the day, jot notes when new insights come.
Works for me.

Didn't anybody have any comments about on how better to compute SWR? 

Jerry, KE7ER


On Sun, May 6, 2018 at 05:31 pm, Jack Purdum wrote:
Braces or brackets? Braces mark statement/function blocks while brackets are most often used with array sizes. The old K&R style was to leave the opening brace on the same line as the expression block, and then align the closing brace with the expression block start. I think that was done to get more lines on the screen when a 25 line display was common. Today, most seem to place the opening brace on its own line. If the block spans more than a page, the latest IDE shows the opening expression.

You can also use Ctrl-T to format your code to a common C coding style.
 


Jack, W8TEE
 

A lot of people do prefer the ternary operator. The reason I avoided it was because if I need to examine one of the values in the ternary expression, it was a lot more messy to add a Serial.print() statement in a ternary expression.

Jack, W8TEE

On Monday, May 7, 2018, 10:16:55 AM EDT, Vince Vielhaber <vev@...> wrote:


I prefer:

    y = (w == 5) ? true : false;

Vince.


On 05/07/2018 12:14 AM, Jack Purdum via Groups.Io wrote:
> The brace thing is really a matter of choice. I learned C back in the
> late 1970's when K&R was the only C book and I had a 64x16 modified TV
> for a CRT, so I put things on the same line, simply so I could see more
> lines without scrolling:
>
>    if (w == 5) {
>      y = true;
>    } else {
>      y = false;
>    }
>
> Even though the braces are not required with a single statement, I
> almost always use them. When I don't, invariably I need to add another
> statement or a debug print statement, so I have to add the brace anyway.
> However, with function signatures, I always place the opening brace for
> the function body on a new line. I think people pick what works for
> them. No one style is "correct", so, to me at least, there's no reason
> to even debate what's correct when it comes to braces. However, we
> should all try to make our code as readable as possible.
>
> When I was in high school, the football coach knew I was a ham radio
> operator and wanted me to build two Heathkit walkie talkies. I built
> them, and they didn't work. I was mortified. I barely slept that night.
> The next morning, my Mom told me I got up in the middle of the night and
> wrote something on the pad next to the phone. I read what I wrote, went
> downstairs and checked it and, sure enough, that was the problem. Ever
> since then, I keep a pad and pencil next to the bed. I can't tell you
> how many teaching examples I've used that came from that pad or
> programming problems that were solved by reading the pad the next
> morning. My experiences have convinced me that your brain continues to
> problem solve even when you're asleep.
>
> Jack, W8TEE
>
>
> On Sunday, May 6, 2018, 11:51:40 PM EDT, Jerry Gaffke via Groups.Io
> <jgaffke=yahoo.com@groups.io> wrote:
>
>
> You guys take this so seriously.
> Maybe you should find a hobby.  ;-)
>
> Though I understand, I'm afflicted by the same malady.
> Just a slightly different strain.
> Here's a fix, no need to ever again deal with my coding style (one of
> many possible tools):
>    http://uncrustify.sourceforge.net/
>
> If you object to K&R style, there's a lot of it out there for you to sic
> crustify on.
> Here's a few million lines to get you started:
>    https://github.com/torvalds/linux
>
>> You can also use Ctrl-T to format your code to a common C coding style.
>
> I assume that's for the Arduino IDE, whose editor I mostly avoid.
> Here's various tricks for vi/vim users:
>
https://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim
>
> Here's a discussion of the various indentation styles:
>
https://softwareengineering.stackexchange.com/questions/99543/what-is-the-difference-between-kr-and-one-true-brace-style-1tbs-styles
> They can't agree either.
> But somebody in there did a study of error rates in code using the
> various styles.
> Note that K&R style won, at least by a little bit.
> Though perhaps that's because folks using the K&R style
> are more likely to have read K&R (highly recommended).
>
> I was quite serious about that back pocket thing.
> If I have a difficult algorithm to work on, I code it tight so I can see
> as much of the work at one time as possible.  Preferably so it all fits
> on one side of a sheet of typing paper.  Better yet, half of one side.
> Then pull it out at odd times during the day, jot notes when new
> insights come.
> Works for me.
>
> Didn't anybody have any comments about on how better to compute SWR?
>
> Jerry, KE7ER
>
>
> On Sun, May 6, 2018 at 05:31 pm, Jack Purdum wrote:
>
>    Braces or brackets? Braces mark statement/function blocks while
>    brackets are most often used with array sizes. The old K&R style was
>    to leave the opening brace on the same line as the expression block,
>    and then align the closing brace with the expression block start. I
>    think that was done to get more lines on the screen when a 25 line
>    display was common. Today, most seem to place the opening brace on
>    its own line. If the block spans more than a page, the latest IDE
>    shows the opening expression.
>
>    You can also use Ctrl-T to format your code to a common C coding style.
>
>
>

--
  Michigan VHF Corp.  http://www.nobucks.net/   http://www.CDupe.com/
                          http://www.metalworkingfun.com




Michael Monteith
 

Yeah, I meant braces.  That's what I get trying to jump in at work doing too many things.   I remember those old screens too well.   Then again I had to work on the computers as well.   So many fun toys these days.

Michael
KM4OLT


On Sun, May 6, 2018 at 05:31 pm, Jack Purdum wrote:
Braces or brackets? Braces mark statement/function blocks while brackets are most often used with array sizes. The old K&R style was to leave the opening brace on the same line as the expression block, and then align the closing brace with the expression block start. I think that was done to get more lines on the screen when a 25 line display was common. Today, most seem to place the opening brace on its own line. If the block spans more than a page, the latest IDE shows the opening expression.

You can also use Ctrl-T to format your code to a common C coding style.

Jack, W8TEE
 


Jerry Gaffke
 

Often my programs are over half comments, but most of those comments are
up at the top.  The code is kept tight so that when I am seriously getting my head into it
I am not distracted by a lot of chatty comments or having to flip through pages and pages.
Or screens and screens, or popping about through different files. 

The comments up top often become a terse skeleton of a manual, 
plus notes for coders that follow.  Can have web references to datasheets
and small excerpts from them.  Occasionally ASCII timing diagrams, perhaps
showing how some GPIO ports talk to a peripheral.  Having all of that in the same file
makes it more likely I'll keep the material in sync with the code.

Much of the coding I have done in the last 20 years has been VHDL.
Can be harder to grok, since there is no central processor finding a path through the code.
All the code executes all the time.  That has contributed to my coding style.

Jerry, KE7ER


On Mon, May 7, 2018 at 05:59 am, Dennis Zabawa wrote:
My advice is  to comment, comment, comment.  Use variable and procedure names that are descriptive.  As Jack said, that clever, little bit of coding you did last week may read like sanskrit  today.  


Rod Davis
 

Vince,

I like your humor.

Rod KM6SN



On 05/07/2018 07:16 AM, Vince Vielhaber wrote:
I prefer:

    y = (w == 5) ? true : false;

Vince.


On 05/07/2018 12:14 AM, Jack Purdum via Groups.Io wrote:
The brace thing is really a matter of choice. I learned C back in the
late 1970's when K&R was the only C book and I had a 64x16 modified TV
for a CRT, so I put things on the same line, simply so I could see more
lines without scrolling:

   if (w == 5) {
      y = true;
   } else {
      y = false;
   }

Even though the braces are not required with a single statement, I
almost always use them. When I don't, invariably I need to add another
statement or a debug print statement, so I have to add the brace anyway.
However, with function signatures, I always place the opening brace for
the function body on a new line. I think people pick what works for
them. No one style is "correct", so, to me at least, there's no reason
to even debate what's correct when it comes to braces. However, we
should all try to make our code as readable as possible.

When I was in high school, the football coach knew I was a ham radio
operator and wanted me to build two Heathkit walkie talkies. I built
them, and they didn't work. I was mortified. I barely slept that night.
The next morning, my Mom told me I got up in the middle of the night and
wrote something on the pad next to the phone. I read what I wrote, went
downstairs and checked it and, sure enough, that was the problem. Ever
since then, I keep a pad and pencil next to the bed. I can't tell you
how many teaching examples I've used that came from that pad or
programming problems that were solved by reading the pad the next
morning. My experiences have convinced me that your brain continues to
problem solve even when you're asleep.

Jack, W8TEE


On Sunday, May 6, 2018, 11:51:40 PM EDT, Jerry Gaffke via Groups.Io
<jgaffke@...> wrote:


You guys take this so seriously.
Maybe you should find a hobby.   ;-)

Though I understand, I'm afflicted by the same malady.
Just a slightly different strain.
Here's a fix, no need to ever again deal with my coding style (one of
many possible tools):
    http://uncrustify.sourceforge.net/

If you object to K&R style, there's a lot of it out there for you to sic
crustify on.
Here's a few million lines to get you started:
    https://github.com/torvalds/linux

You can also use Ctrl-T to format your code to a common C coding style.

I assume that's for the Arduino IDE, whose editor I mostly avoid.
Here's various tricks for vi/vim users:

  https://stackoverflow.com/questions/2355834/how-can-i-autoformat-indent-c-code-in-vim

Here's a discussion of the various indentation styles:

  https://softwareengineering.stackexchange.com/questions/99543/what-is-the-difference-between-kr-and-one-true-brace-style-1tbs-styles
They can't agree either.
But somebody in there did a study of error rates in code using the
various styles.
Note that K&R style won, at least by a little bit.
Though perhaps that's because folks using the K&R style
are more likely to have read K&R (highly recommended).

I was quite serious about that back pocket thing.
If I have a difficult algorithm to work on, I code it tight so I can see
as much of the work at one time as possible.  Preferably so it all fits
on one side of a sheet of typing paper.  Better yet, half of one side.
Then pull it out at odd times during the day, jot notes when new
insights come.
Works for me.

Didn't anybody have any comments about on how better to compute SWR?

Jerry, KE7ER


On Sun, May 6, 2018 at 05:31 pm, Jack Purdum wrote:

    Braces or brackets? Braces mark statement/function blocks while
    brackets are most often used with array sizes. The old K&R style was
    to leave the opening brace on the same line as the expression block,
    and then align the closing brace with the expression block start. I
    think that was done to get more lines on the screen when a 25 line
    display was common. Today, most seem to place the opening brace on
    its own line. If the block spans more than a page, the latest IDE
    shows the opening expression.

    You can also use Ctrl-T to format your code to a common C coding style.






Jerry Gaffke
 

Some C coders won't see that as humor.
That's how they code.
And if you work with it enough to where it is no longer gibberish,
the more compact representation might be understood more quickly.

There are different styles between programming languages as well as within programming languages.
The more esoteric ones can become insanely compact, doing in a line what would take a page in others.
I still remember the shock back in my school days when taking a class covering Snobol
after first getting my feet wet with Fortran. 

Jerry


On Mon, May 7, 2018 at 07:40 am, Rod Davis wrote:

Vince,

I like your humor.

Rod KM6SN

 

. . .

 

On 05/07/2018 07:16 AM, Vince Vielhaber wrote:
I prefer:

    y = (w == 5) ? true : false;

Vince.


Rod Davis
 

Jerry,

I started writing C in 1974,owned K&R, programmed professionally until mid 90s, have written
and read tons of C, and actually saw Vince's line as concise and clear, and still enjoyed the humor.

Rod KM6SN


On 05/07/2018 07:58 AM, Jerry Gaffke via Groups.Io wrote:
Some C coders won't see that as humor.
That's how they code.
And if you work with it enough to where it is no longer gibberish,
the more compact representation might be understood more quickly.

There are different styles between programming languages as well as within programming languages.
The more esoteric ones can become insanely compact, doing in a line what would take a page in others.
I still remember the shock back in my school days when taking a class covering Snobol
after first getting my feet wet with Fortran. 

Jerry

On Mon, May 7, 2018 at 07:40 am, Rod Davis wrote:

Vince,

I like your humor.

Rod KM6SN

 

. . .

 

On 05/07/2018 07:16 AM, Vince Vielhaber wrote:
I prefer:

    y = (w == 5) ? true : false;

Vince.


Jim Strohm
 

Oh no, not ANOTHER Xerox 820 veteran!

My first real computer was a Xerox 820 that somebody had hacked together with a pair of 8" floppies.  I learned a lot about how much I didn't know.

WordStar was my very best friend, and a few various text adventure games were my second best friends.

Halloween this year will be my 35th anniversary of being online ... beginning with a 300 baud acoustic coupler modem.

73
Jim N6OTQ


w2ttt <w2ttt@...>
 

Oh... I had several Xerox 820 homebrew setups... one as a BBS running a Bell 212A modem!
73,
Gordon Beattie W2TTT




On Mon, May 7, 2018 at 11:27 AM -0400, "Jim Strohm" <jim.strohm@...> wrote:

Oh no, not ANOTHER Xerox 820 veteran!

My first real computer was a Xerox 820 that somebody had hacked together with a pair of 8" floppies.  I learned a lot about how much I didn't know.

WordStar was my very best friend, and a few various text adventure games were my second best friends.

Halloween this year will be my 35th anniversary of being online ... beginning with a 300 baud acoustic coupler modem.

73
Jim N6OTQ


Tim Gorman
 

I've often wondered if the original coding of the "if" statement in C
didn't derive from fortran syntax:

if (expression) then
statements
endif

The "then" had to be on the same line as the if. In C, the "then" is
replaced by the "{" brace.

At least that's how we had to write fortran in 1968 for the mainframe
to compile it!

tim ab0wr

On Mon, 07 May 2018 05:44:24 +0000
"Ashhar Farhan" <farhanbox@...> wrote:

64x16? K&R?? Surely, you are not talking floppy shuffle on the Xerox
820 or (jerry pournelle, peace be upon him) a Kaypro?
BDS C was the only game in town. I brought up a CP/M system for my
undergraduate project work.
- f
- f