Not a very good Idea!

Discussion in 'C Programming' started by vashwath@rediffmail.com, Apr 4, 2005.

Guest

Hi all,
As per our coding rule, a line should not have more than 80 characters.
When accessing a structure elements which are deeply nested, we are not
able follow this rule. To acheive this Can we use macro?

Advertisements

On Mon, 3 Apr 2005 wrote:
>
> Hi all,
> As per our coding rule, a line should not have more than 80 characters.
> When accessing a structure elements which are deeply nested, we are not
> able follow this rule. To acheive this Can we use macro?

First of all, that's fewer than 80 columns. Secondly, the C programming
language is whitespace-insensitive (in most ways), so you can always write

struct1.str1.strct3 = struct2.str2.st1.s1
+ struct3.str3.st3.s3;

A much better alternative is to rework your data structure, and use the
much simpler

x = y + z;

Of course, this will require getting rid of a lot of tiny intermediate
struct definitions (struct1, str1, st1, s1, and so on). But that's a
very good thing. Nested structs are bad for readability, and mostly bad
for thinking in general.

If you can't do that (for example, if you have more silly "house rules"
that require lots of nested data structures), you can use

struct foo *p = &struct2.str2.st1;
struct foo *q = &struct3.str3.st3;

struct1.str1.strct3 = p->s1 + q->s3;

Again, not the best solution, but a darn sight better than trying to
use the preprocessor to wiggle out of a problem you yourself created
with nested data structures.

Advertisements

writes:
> As per our coding rule, a line should not have more than 80 characters.
> When accessing a structure elements which are deeply nested, we are not
> able follow this rule. To acheive this Can we use macro?
>
> For example,
>
> struct1.str1.strct3=struct2.str2.st1.s1 + struct3.str3.st3.s3;
>
> #define STRUCT2 struct2.str2.st1.s1
> #define STRUCT3 struct3.str3.st3.s3
>
> and then use
>
> struct1.str1.strct3 = STRUCT2 + STRUCT3;
>
> Is this a good Idea(if not please let me know what are the problems it
> may cause)?Or Is there a better Idea than this?

struct1.str1.strct3=struct2.str2.st1.s1 + struct3.str3.st3.s3;

is obviously less than 80 columns, but assuming it's meant as an
example, there's no reason it all has to be on one line. The
following are equivalent:

wrote:
>
> Hi all,
> As per our coding rule, a line should not have more than 80 characters.
> When accessing a structure elements which are deeply nested, we are not
> able follow this rule. To acheive this Can we use macro?
>
> For example,
>
> struct1.str1.strct3=struct2.str2.st1.s1 + struct3.str3.st3.s3;
>
> #define STRUCT2 struct2.str2.st1.s1
> #define STRUCT3 struct3.str3.st3.s3
>
> and then use
>
> struct1.str1.strct3 = STRUCT2 + STRUCT3;
>
> Is this a good Idea

No.
> (if not please let me know what are the problems it
> may cause)?

Difficult to read, difficult to debug when it goes wrong.
> Or Is there a better Idea than this?

Use a C99 compiler or a compiler that understand inline and
use something like

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (Yes it's valid)
+-----------------------------------------------------------+
Learning Linux is like joining a cult. Sure it's fun at first but
you waste time, become brainwashed, and then have to be de-programmed
by Bill Gates before you can work for Him again.
- Ray Lopez, in UFhK4.33289$

wrote:
>
> As per our coding rule, a line should not have more than 80
> characters. When accessing a structure elements which are deeply
> nested, we are not able follow this rule. To acheive this Can we
> use macro?
>
> For example,
>
> struct1.str1.strct3=struct2.str2.st1.s1 + struct3.str3.st3.s3;
>
> #define STRUCT2 struct2.str2.st1.s1
> #define STRUCT3 struct3.str3.st3.s3
>
> and then use
>
> struct1.str1.strct3 = STRUCT2 + STRUCT3;
>
> Is this a good Idea(if not please let me know what are the
> problems it may cause)?Or Is there a better Idea than this?

Not in my opinion. It shows up the lack of a 'with' statement in
C, but that is another matter. You can simply break the statement
up:

struct1.str1.strct3 = struct2.str2.st1.s1
+ struct3.str3.st3.s3;

I also consider an 80 char line length excessive, 72 is better. If
you are continuously falling off the right it probably indicates
that you are failing to break your code up into small enough
modules.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

wrote:
> Hi all,
> As per our coding rule, a line should not have more than 80 characters.
> When accessing a structure elements which are deeply nested, we are not
> able follow this rule. To acheive this Can we use macro?
>
> For example,
>
> struct1.str1.strct3=struct2.str2.st1.s1 + struct3.str3.st3.s3;

Others have mentioned various ways of inserting line
breaks to keep individual lines short, but I have not yet
seen anything as radical as

(The reason for exhibiting this abomination is to raise
the important question: What is the problem your "coding rule"
seeks to solve or avoid? When the rule makes trouble it is
time to ask whether the cure is worse than the disease; this
will guide you in deciding whether to bend the rule or to uphold
it and make changes elsewhere.)

....
> Not in my opinion. It shows up the lack of a 'with' statement in
> C, but that is another matter.

IMO Pascal's with statement is one of the truely nasty parts of that
language. C has a vastly better way of dealing with that problem using
pointers to structures or unions. In effect you can create a with
construct where each structure being aliased is named explicitly.
> You can simply break the statement
> up:
>
> struct1.str1.strct3 = struct2.str2.st1.s1
> + struct3.str3.st3.s3;

Yes, there are lots of ways of handling this, this looks good here.
Intermediaries could be appropriate in some circumstances, e.g.

This can work for the LHS too.
> I also consider an 80 char line length excessive, 72 is better. If
> you are continuously falling off the right it probably indicates
> that you are failing to break your code up into small enough
> modules.

It is very easy to get to and beyond 80 columns, although I try to limit
myself to that for C code. It depends on indentation amount, expression
complexity, whether you have some reasonable length text in string
literals and so on. Making modules (do you mean functions?) too small is
not a good thing, it creates complexity in the interfaces and call tree.

Of course, professional programmers always format their code so that the
way the code looks reflects what this code does. The above, for example,
is a nice way to format a code that calculates the area of a rectangle.

pete wrote on 04/04/05 :
> Keith Thompson wrote:
>
>> struct1.str1.strct3 =
>> struct2.str2.st1.s1 +
>> struct3.str3.st3.s3;
>
> I prefer to start, rather than to terminate,
> a continued line with a binary operator,
> to make it more obvious that it's a continued line.
>
> struct1.str1.strct3
> = struct2.str2.st1.s1
> + struct3.str3.st3.s3;

In article <>,
Emmanuel Delahaye <> wrote:
>pete wrote on 04/04/05 :
>> I prefer to start, rather than to terminate,
>> a continued line with a binary operator,
>> to make it more obvious that it's a continued line.
>> struct1.str1.strct3
>> = struct2.str2.st1.s1
>> + struct3.str3.st3.s3;
>Agreed.

FORTRAN lives ;-)

(and, no, I didn't mean "Fortran").
--
Usenet is like a slice of lemon, wrapped around a large gold brick.

On Mon, 04 Apr 2005 10:17:19 -0700, Alan Balmer
<> wrote:
> On Mon, 04 Apr 2005 09:50:17 GMT, pete <> wrote:
>
>>Keith Thompson wrote:
>>
>>> struct1.str1.strct3 =
>>> struct2.str2.st1.s1 +
>>> struct3.str3.st3.s3;
>>
>>I prefer to start, rather than to terminate,
>>a continued line with a binary operator,
>>to make it more obvious that it's a continued line.
>>
>>struct1.str1.strct3
>> = struct2.str2.st1.s1
>> + struct3.str3.st3.s3;
>
> An interesting take. I generally read code from top to bottom, so
> prefer the binary operator on the end of the line, to make it more
> obvious that there's more to come.

I do as well, but it's a style thing (roughly half of us at work prefer
the operators at the end and the others prefer them at the start of the
next line).

In article <1112886039.35604f388b6a67a07b1128bf3c3273d9@teranews>, 0001
Rufus V. Smith <> wrote: 0002
>> I also consider an 80 char line length excessive, 72 is better. 0003
0004
>Yes, please. My Teletype is only 72 columns... 0005
0006
And for those of us using punched cards, if you use more than 72 0007
characters it will run in to the sequence numbers. 0008
0009
-- Richard 0010
0011

Mine too. However 72 also allows for 80 char text output after
inserting line numbers, and avoids most line wrapping when quoted
on usenet, etc. etc. In fact I try to limit my lines to 65 chars.
This also tends to increase legibility.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!