¿ªÔÆÌåÓý

Re: Coding styles


Jack Purdum
 

And I'm off...

Often programmers don't think things like variable and function names make a difference, but it really can affect how easy or difficult it is to read someone else's code. When I was teaching, I found that students tended to use variable names like 'a', 'b', 'c'. Eventually, some guidelines emerged (this was in a C++ course):

1. Variables are nouns, and what they are should be obvious from their name (hatSize, portNumber, vfoFrequency)
??? a. Use camel notation, first letter is lower case and upper case for subparts.

2. Methods (functions) are verbs denoting some action takes place within a black box.
??? a. First letter of the method name is upper case, then camel notation. (This makes it easier to recognize some programming
??????? elements, like pointer-to-function rather a pointer variable.)
??? b. The name should tell the action performed.
??? c. The method name should NOT say how something is done. That is, BubbleSortList() is not so good, SortList() is better.
??????? The programmer does not need to know how you do something, only what it does and how to interface with it. That way,
??????? you can change the algorithm within the box and not be lying to its user.

3. Methods (functions) should be cohesive. That is, if you cannot describe what the function does in two sentences or less, it's
??? probably trying to do too much. Students tend to write Swiss Army knives and pass in a bazillion parameters to their multi-
????purpose function.
??? a. Simpler is better, and enhances the chance for reuse.
??? b. If you are passing in 6 or more parameters, either the method is not cohesive, or you need to pass a pointer to some kind
??????? of data structure.

4. Avoid coupling between methods (functions) as much as possible. That is, one function should do its task without help from
????another methods. Sometimes, coupling cannot be avoided (e.g., you must Open() a file before you can Read() it), but it should
????be minimized as much as possible.


While the above is purely a "style" consideration, it is an important one in that it can make it so much easier to read code.

Jack, W8TEE

On Monday, May 7, 2018, 3:17:30 PM EDT, Art N4EZZ <n4ezz@...> wrote:


Hi,

I normally just sit here and listen to all of you smart folks with the
hope that I can learn something from your discussions, but this trip
down memory lane is too much.

PDP-11/70s, VT-100 terminals and pre-System V Unix the old Version 7,
later all sorts of Unixes and Stratus VOS as well. Yep, I am an old Bell
head.

First screen editor, Emacs, yes I still use it. IDEs, yes although I use
them mostly for compile/debug cycles.

I was for many years a system administrator and typically wrote programs
to accomplish the various utility functions that you would expect. I
also wrote a number of programs to support some rather large conversion
projects.

The scariest words I ever heard in a staff meeting, here is some code
install it on XYZ machine "it should work just fine". Almost always that
translated to "you will spend at least a week getting it to compile and
actually run".

So coding style, don't care but be consistent, if you are consistent I
can figure it out.

Functions and variables should describe what they are used for.

Comments, yes please, if you do something that might be
counter-intuitive then please explain why as well as what.

To finish, something I learned back then about unintended consequences.
After 20 hours working with Bell Labs on a problem in a telephone switch
we found the problem, the fix took about 20 seconds. Before the Bell
Labs guys dropped off the call one of them, I think it was Dennis said,
"you know I wrote that code and I never imagined it could do that".

Thanks for keeping my old mind active and it is back to learning mode
for me.

Art N4EZZ <n4ezz@...>
GnuPG key ID 0x6712DD0E
=============================
Suppose you were an idiot. And suppose you were a member of Congress.
But I repeat myself.
? ? ? Mark Twain, a Biography




Join [email protected] to automatically receive all group messages.