473,587 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Not a very good Idea!

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.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;

#define STRUCT2 struct2.str2.st 1.s1
#define STRUCT3 struct3.str3.st 3.s3

and then use

struct1.str1.st rct3 = 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?

Nov 14 '05 #1
18 1357

On Mon, 3 Apr 2005 va******@rediff mail.com 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?
Oh, dear.
For example,

struct1.str1.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;


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.st rct3 = struct2.str2.st 1.s1
+ struct3.str3.st 3.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.s t1;
struct foo *q = &struct3.str3.s t3;

struct1.str1.st rct3 = 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.

HTH,
-Arthur
Nov 14 '05 #2
va******@rediff mail.com 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.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;

#define STRUCT2 struct2.str2.st 1.s1
#define STRUCT3 struct3.str3.st 3.s3

and then use

struct1.str1.st rct3 = 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.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.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:

struct1.str1.st rct3 =
struct2.str2.st 1.s1 + struct3.str3.st 3.s3;

struct1.str1.st rct3 =
struct2.str2.st 1.s1 +
struct3.str3.st 3.s3;

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

It would probably be even better to restructure your code so you're
not using so many deeply nested structures.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
Keith Thompson wrote:
struct1.str1.st rct3 =
struct2.str2.st 1.s1 +
struct3.str3.st 3.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.st rct3
= struct2.str2.st 1.s1
+ struct3.str3.st 3.s3;

--
pete
Nov 14 '05 #4
va******@rediff mail.com 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.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;

#define STRUCT2 struct2.str2.st 1.s1
#define STRUCT3 struct3.str3.st 3.s3

and then use

struct1.str1.st rct3 = 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
static inline void
struct_add (STRUCT_NAME *result, const STRUCT_NAME *left, const STRUCT_NAME *right)
{ result->whatever.resul t = left->x.y.z + right->x.y.z ;
}

and you can call that using:

struct_add (&result, &left, &right) ;

if they are defined as structs or

struct_add (result, left, right) ;

if you already have pointers to the structs.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (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 UF************* *******@newsrea d1.prod.itd.ear thlink.net
Nov 14 '05 #5
va******@rediff mail.com 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.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.s3;

#define STRUCT2 struct2.str2.st 1.s1
#define STRUCT3 struct3.str3.st 3.s3

and then use

struct1.str1.st rct3 = 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.st rct3 = struct2.str2.st 1.s1
+ struct3.str3.st 3.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.c om, 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
Nov 14 '05 #6
va******@rediff mail.com 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.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.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

struct1
Nov 14 '05 #7
On Mon, 04 Apr 2005 11:34:58 +0000, CBFalconer wrote:

....
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.st rct3 = struct2.str2.st 1.s1
+ struct3.str3.st 3.s3;
Yes, there are lots of ways of handling this, this looks good here.
Intermediaries could be appropriate in some circumstances, e.g.

s2211 = struct2.str2.st 1.s1;
s3333 = struct3.str3.st 3.s3;
struct1.str1.st rct3 = s2211 + s3333;

Using with-like pointers could be natural if you access a number of
members from an innermost structure

type *const p221 = &struct2.str2.s t1;
type *const p333 = &struct3.str3.s t3;

struct1.str1.st rct3 = p221->s1 + p333->s3;

struct2.str3.st rct1 = p221->s3 + p333->s1; /* for example */

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.

Lawrence
Nov 14 '05 #8
On Mon, 04 Apr 2005 09:50:17 GMT, pete <pf*****@mindsp ring.com> wrote:
Keith Thompson wrote:
struct1.str1.st rct3 =
struct2.str2.st 1.s1 +
struct3.str3.st 3.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.s trct3
= struct2.str2.st 1.s1
+ struct3.str3.st 3.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.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #9
Keith Thompson 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.st rct3=struct2.st r2.st1.s1 + struct3.str3.st 3.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:

struct1.str1.st rct3 =
struct2.str2.st 1.s1 + struct3.str3.st 3.s3;

struct1.str1.st rct3 =
struct2.str2.st 1.s1 +
struct3.str3.st 3.s3;

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


And there's always the '\' at the end of line, which greatly expands the
number of variants this code can be reformatted

struct1.s\
tr1.strct\
3 = struc\
t2.str2.s\
t1.s1 + s\
truct3.st\
r3.st3.s3;

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.

:)

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2414
by: Eric S. Johansson | last post by:
I have an application where I need a very simple database, effectively a very large dictionary. The very large dictionary must be accessed from multiple processes simultaneously. I need to be able to lock records within the very large dictionary when records are written to. Estimated number of records will be in the ballpark of 50,000 to...
8
3064
by: news | last post by:
I seriously doubt this is possible...but you never know, so here goes. Due to bad pre-planning I have a Web page that is VERY table heavy with a very complicated and delicate setup. Any changes to the table structure, as in size changes or additions of new cells, throws the whole thing out of whack. (Let me pause here to say I understand...
8
1687
by: Ondine | last post by:
Hi I have a client running an Access 2000 database on a small network with 3 pc's. Two of the laptop pcs have a data replica, which they use when not connected to the network, the 'server' being the 3rd machine. The other day they reported that 'suddenly' a whole load of data that they had entered (on to the 'server' main data) that day...
3
1277
by: AAA | last post by:
I want to start programming in MS Visual C++. I understand that .NET is required? Or do I have to get MSStudio to integrate my work into the NET frame work? And any idea when VS 2005 is going to come out. Should I buy MSC++ now and then up-grade? I need some real basic fatherly advice in very some simple English. I programmed a little (basic)...
5
2622
by: BethInAK | last post by:
Thank you in advance for any possible help you have to offer, before I pull my hair out! I am incredibly frustrated attempting to learn asp.net. Its really pissing me off. Please help. First, please tell me the best asp .net book to purchase for a stupid beginner, preferably with vb instead of C. I have no idea what most of the c code...
17
2433
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The specific example was to define an "outer product" operator for matrices. (There was even a PEP, number 211, about this.) I gave it some thought,...
6
2274
by: Marc | last post by:
How could I directly trigger a very simple on localhost and a known port listening server from my internet browser client? Local host means the little server would be running on the client machine, where my browser resides. Browser would be IE, O.S. Windows 2000 or XP, and it's for an intranet application. The goal of the little server on the...
16
1659
by: lovecreatesbeauty | last post by:
`Writing C code is very simple', one guy related to my work said. I'm not sure whether he is an expert or not. What he said about C programming like this can't convince me. I think there should be two kinds of people can make such a comment on C programming. One is C expert with rich experiences of some years on real projects; The other is...
28
2616
by: steve yee | last post by:
i think c should adapt c++ template standard, as well as namespace. if so, c can replace c++ in many cases.
0
7923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5719
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.