The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* [TUHS] margins and indenting, and arbitrary c rules
@ 2017-11-09 15:09 Noel Chiappa
  2017-11-09 15:41 ` Don Hopkins
  0 siblings, 1 reply; 7+ messages in thread
From: Noel Chiappa @ 2017-11-09 15:09 UTC (permalink / raw)


    > From: Steve Simon

    > At the risk of stirring up another hornets nest...

I usually try not to join in to non-history threads (this list has too much
random flamage on current topics, IMNSHO), but I'll make an exception here...

    > this means my code is usually fairly narrow.

I have what I think are somewhat unusual thoughts on nesting depth, etc, which
are: keep the code structure as simple as possible. That means no complex
nested if/then/else structures, etc (more below).

I'd been moving in this direction for a while, but something that happened a
long time ago at MIT crystalized this for me: a younger colleague brought me a
routine that had a complex nested if/etc structure in it, which had a bug; I
don't recall if he just wanted advice, or what, but I recall I fixed his
problem by..... throwing away half his code. (Literally.)

That really emphasized to me the downside of complexity like that; it makes it
harder to understand, harder to debug, and harder for someone else (or you
yourself, once you've been away from it for a while) to modify it. I've been
getting more formal about that ever since.


So, how did I throw away half his code? I have a number of techniques. I try
not to use 'else' unless it's the very best way to do something. So instead
of:

    if (some-conditional) {
       <some code>;
       }
      else {
       <some other code>;
       }      

(iterated, recursively) do:

    if (some-conditional) {
       <some code>;
       xxx;
       }

    <some other code>;

where xxx is a 'return', or a 'break', or a 'continue'. That way, you can
empty your brain once you hit that, and start with a clean slate.

This is also an example of another thing I do, which is use lots of simple
flow-control primitives; not go-tos (which I abhor), but loop exits, restarts,
etc. Again, it just makes things simpler; once you hit it, you can throw away
all that context/state in your mind.


Keep the control flow structure as simple as possible. If your code is several
levels of indent deep, you have much bigger problems than fitting it on the
screen.


I wrote up a complete programming style guide for people at Proteon working on
the CGW, which codified a lot of this; if there's interest, I'll see if I can
find a copy (I know I have a hardcopy _somewhere_).

     Noel


^ permalink raw reply	[flat|nested] 7+ messages in thread
* [TUHS] margins and indenting, and arbitrary c rules
@ 2017-11-09 16:03 Noel Chiappa
  0 siblings, 0 replies; 7+ messages in thread
From: Noel Chiappa @ 2017-11-09 16:03 UTC (permalink / raw)


    > From: Don Hopkins

    > https://stackoverflow.com/questions/268132/invert-if-statement-to-reduce-nesting/8493256

Thanks for that - interesting read. (Among other things, it explains where the
'only have one return, at the end' comes from - which I never could understand.)

    > Nested if statements have more 'join' points where control flow merges
    > back together at the end instead of bailing out early

Exactly. In high-level terms, it's much like having a ton of goto's. Yes, the
_syntax_ is different, but the semantics - and understanding how it works - is
the same - i.e. hard.

    Noel


^ permalink raw reply	[flat|nested] 7+ messages in thread
* [TUHS] margins and indenting, and arbitrary c rules
@ 2017-11-09 15:31 Noel Chiappa
  2017-11-10 18:41 ` Ian Zimmerman
  0 siblings, 1 reply; 7+ messages in thread
From: Noel Chiappa @ 2017-11-09 15:31 UTC (permalink / raw)


    > I'd been moving in this direction for a while

Now that I think about it, I may have subconciously absorbed this from Ken's
and Dennis' code in the V6 kernel. Go take a look at it: more than one level
of indent is quite rare in anything there (including tty.c, which has some
pretty complex stuff in it).

I don't know if this was subconcious but deliberate, or concious, or just
something they did for other reasons (e.g. typing long lines took too long on
their TTY's :-), but it's very noticeable, and consistent.

It interesting that both seem to have had the same style; tty.c is in dmr/, so
presumably Dennis', but the stuff in ken/ is the same way.


Oh, one other trick for simplifying code structure (which I noticed looking
through the V6 code a bit - this was something they _didn't_ do in one place,
which I would have done): if you have

	if (<condition>) {
		<some complex piece of code>
		}
	<implicit return>
}

invert it:

	if (<!condition>)
		return;

	<some complex piece of code>
}

That gets that whole complex piece of code down one level of nesting.

     Noel


^ permalink raw reply	[flat|nested] 7+ messages in thread
* [TUHS] margins and indenting, and arbitrary c rules
@ 2017-11-09  8:07 Steve Simon
  0 siblings, 0 replies; 7+ messages in thread
From: Steve Simon @ 2017-11-09  8:07 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 320 bytes --]


At the risk of stirring up another hornets nest...

I never accepted the Microsoft

“write functions for maximal indentation, with only one return”

and stick to

“get out if the function asap, try to have success at the bottom of the function”

style.

this means my code is usually fairly narrow.

-Steve





^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-11-10 18:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-09 15:09 [TUHS] margins and indenting, and arbitrary c rules Noel Chiappa
2017-11-09 15:41 ` Don Hopkins
2017-11-09 19:13   ` Ron Natalie
  -- strict thread matches above, loose matches on Subject: below --
2017-11-09 16:03 Noel Chiappa
2017-11-09 15:31 Noel Chiappa
2017-11-10 18:41 ` Ian Zimmerman
2017-11-09  8:07 Steve Simon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).