473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PROS/CONS: #define BEGIN {

A colleague of mine is proposing that we use a set of preprocessor
definitions to make our C code more readable:

#define BEGIN {
#define ENG }
#define EQ ==
etc.

My initial reaction is "Yuck!" (Not too different from the FAQ, which just
says "Bleah").

Aside from that, what are some good reasons to do this, or not to do this?
A few negatives I come up with include:

* Won't work with some tools, such as
- syntax-aware editors
- metrics
- static analysis tools
- coding style enforcement
- code indenters/reformatters
- pretty printers

* Does not check for mismatches (e.g., "{" vs. "END").

* Does not prevent use of {, }, ==, etc., just adds alternatives.

* Requires re-training of C developers who are used to standard C syntax.
Nov 14 '05
57 7531
On Tue, 21 Dec 2004, Michael Mair wrote:
Jarno A Wuolijoki wrote:
s/class/struct/g if feeling nitpicky. I'm fairly positive that, given some
time, I can craft a similar case in the topical language as well.
[...] Otherwise, you would not hunt for a bug but for a syntax error.


Are you absolutely sure that you want to turn this into a discussion
on whether or not a failure to compile can be counted as a bug or did
you just get baited by the "if feeling nitpicky"-part?-)
Anyway, if I wanted to go on with this, I'd probably make the following
case (which I obviously don't as otherwise I'd had it written here
right after these words):

int main(void) {
#if PLATFORM_Z
int a[] = {0);
#else
int a[] = {0};
#endif
return 0;
}

Is this a syntax error or a bug? Or perhaps a bug that shows up as
a syntax error under certain conditions?
Nov 14 '05 #51
Michael Mair <Mi**********@i nvalid.invalid> writes:
Jarno A Wuolijoki wrote:
On Tue, 21 Dec 2004, Charlie Gordon wrote:
"Jarno A Wuolijoki" <jw******@cs.He lsinki.FI> wrote
For an oppsite example, Today I debugged about half an hour a piece
of code that had ')' instead of '}' at the end of a block. Funny how
I hadn't realized before how similar to each other they can be made to
look like..

Half an hour on this ???
What editor do you use ?

Ultraedit. (it probably supports brace matching but I just didn't
realize that at the moment)


IIRC, it does.

What is your compiler / environment ?

gcc.


Which did not give you a parse error?

What was the error message at compile time ?

Something about some offtopic standard forbidding initialization
inside classes (I didn't say it was C..). Naturally, the error
pointed to a different source file and the main false track was that
I expected having actually put it in inside a class due to other
reasons I'm not going to enumerate here.


Sounds like a problem of messy coding.


It's not surprising that a mismatched brace ('{', '}') could confuse a
compiler. It could cause the compiler to assume that subsequent code
is at a different scope that what was intended. It will eventually
detect and report a syntax error, but it could report any number of
confusing semantic errors first. (No, I don't have an example.)

--
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 #52
Keith Thompson wrote:
Michael Mair <Mi**********@i nvalid.invalid> writes:
Jarno A Wuolijoki wrote:
On Tue, 21 Dec 2004, Charlie Gordon wrote:

"Jarno A Wuolijoki" <jw******@cs.He lsinki.FI> wrote

>For an oppsite example, Today I debugged about half an hour a piece
>of code that had ')' instead of '}' at the end of a block. Funny how
>I hadn't realized before how similar to each other they can be made to
>look like..

Half an hour on this ???
What editor do you use ?

Ultraedit. (it probably supports brace matching but I just didn't
realize that at the moment)


IIRC, it does.

What is your compiler / environment ?

gcc.


Which did not give you a parse error?

What was the error message at compile time ?

Something about some offtopic standard forbidding initialization
inside classes (I didn't say it was C..). Naturally, the error
pointed to a different source file and the main false track was that
I expected having actually put it in inside a class due to other
reasons I'm not going to enumerate here.


Sounds like a problem of messy coding.


It's not surprising that a mismatched brace ('{', '}') could confuse a
compiler. It could cause the compiler to assume that subsequent code
is at a different scope that what was intended. It will eventually
detect and report a syntax error, but it could report any number of
confusing semantic errors first. (No, I don't have an example.)


My point was rather that Jarno A Wuolijoki called it a bug
which implied (for me) that the whole thing compiled and
the program did some kind of work.
The "messy" referred to the fact that a different source
file was implied. This usually happens only if the error
occurred in a header file. Thus, the header file was not
checked for errors and not even included into some sort of
test translation unit which includes all the headers to
check for syntax errors in the headers.
<OT>
On errors like that, gcc usually dies with a parse error.
On parse errors, you easily can check where the whole
problem occurs by commenting out large chunks of code
until it runs. If the whole payload of the translation
unit is gone, then you move on to the headers. Why waste
time with unstructured searching when you cannot spot
the reason for the error at once?
</OT>

As one can see from the example brought by Jarno A
Wuolijoki, it was not a bug we were talking about but
a syntax error. Happens to me, too. My objection was
rather to the term bug. }/) mismatches happen just like
other syntax errors and can be found easily.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #53
Michael Mair <Mi**********@i nvalid.invalid> writes:
[...]
My point was rather that Jarno A Wuolijoki called it a bug
which implied (for me) that the whole thing compiled and
the program did some kind of work.

[...]

Some of us (presumably including Jarno) have a more expansive
definition of "bug".

--
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 #54


Keith Thompson wrote:
Michael Mair <Mi**********@i nvalid.invalid> writes:
[...]
My point was rather that Jarno A Wuolijoki called it a bug
which implied (for me) that the whole thing compiled and
the program did some kind of work.


[...]

Some of us (presumably including Jarno) have a more expansive
definition of "bug".


I go by the one from the jargonfile:

http://www.catb.org/~esr/jargon/html/B/bug.html :
"
bug: n.

An unwanted and unintended property of a program or piece of
hardware, esp. one that causes it to malfunction. Antonym of feature.
Examples: “There's a bug in the editor: it writes things out backwards.”
“The system crashed because of a hardware bug.” “Fred is a winner, but
he has a few bugs” (i.e., Fred is a good guy, but he has a few
personality problems).

[snip: Historical Notes]
"

Source code which does not compile is not exactly a program
in my book.
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #55
On Tue, 21 Dec 2004 23:27:39 GMT, Keith Thompson
<ks***@mib.or g> wrote:
It's not surprising that a mismatched brace ('{', '}') could confuse a
compiler. It could cause the compiler to assume that subsequent code
is at a different scope that what was intended. It will eventually
detect and report a syntax error, but it could report any number of
confusing semantic errors first. (No, I don't have an example.)


The following (deliberately non-indented) faulty program gives a number
of errors, on no compiler I have here does it say "you missed off a }".
On the other hand, I had to force vim (the editor I use) to not indent
it, the error was very obvious when it indented automatically.

void func(char *ptr)
{
if (ptr)
{
*ptr = 'a';
}

char buff[2];

int main(void)
{
func(buff);
}

Some compilers will give "parse error before 'char'" on the line
declaring buff, some will give "parse error before '{'" on the line
after "int main", at least one C99 compiler gives "parse error at end of
input" at the end of the file, and a C89 compiler (gcc v3 with -ansi
-pedantic) gives:

warning: ISO C89 forbids mixed declarations and code
warning: ISO C forbids nested functions

Chris C
Nov 14 '05 #56
In article <Pi************ *************** ****@sbz-31.cs.Helsinki. FI>,
Jarno A Wuolijoki <jw******@cs.He lsinki.FI> wrote:
On Tue, 21 Dec 2004, Richard Bos wrote:
Thomas Stegen <th***********@ gmail.com> wrote:
> Well, how often do you have nesting errors? I can't remember when I
> last saw or heard of one. And it is not obvious at all to me that
> BEGIN and END are easier to remember to put in than { and }.


Point in case: I was hand-hacking a Windows resource file last week.
Windows resource files use BEGIN and END. I forgot nearly all the
second-level BEGINs, because to my mind, POPUP plus indentation already
defined the beginning of the block. Using C, I hardly have to think to
just put the { at the end of the if/while/for-line.
Ok, this may be influenced by me being more used to { } than to BEGIN
END, but it's enough to show that BEGIN and END aren't inherently less
nesting-error-prone.


For an oppsite example, Today I debugged about half an hour a piece
of code that had ')' instead of '}' at the end of a block. Funny how
I hadn't realized before how similar to each other they can be made to
look like..


Standard technique for when it takes more than 5 minutes to
understand a syntax error.
(Most implementations allow this one way or another.)
Run the program through the preprocessor only, typical -E.
Then through a beautifier (cb, indent).
It pays off to have a small script in place for this. At least
for someone elses code.

--

--
Albert van der Horst,Oranjestr 8,3511 RA UTRECHT,THE NETHERLANDS
One man-hour to invent,
One man-week to implement,
One lawyer-year to patent.
Nov 14 '05 #57
Mike Malone wrote:
A colleague of mine is proposing that we use a set of preprocessor
definitions to make our C code more readable:

#define BEGIN {
#define ENG }
#define EQ ==
etc.

My initial reaction is "Yuck!" (Not too different from the FAQ, which just
says "Bleah").


Why not go all the way, and just program in PASCAL or COBAL?
If you don't like the language, why use it?
Why waste time doing a half-assed job of switching to
anopther language?

I assume you plan on never using any code analysis tools,
which are likely to barf on the hacked-up syntax.

I also assume you don't expect any outsiders to ever see your
code, especially people buying your product. It would be a
BIG negitive to me if I saw that stuff.
Nov 14 '05 #58

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

Similar topics

112
10370
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please, share your experience in using IDENTITY as PK .
2
2027
by: Zhou Lei | last post by:
Hi friends I'm a newbie learning XSLT to transform an XML to some other documents. Now I have some questions, anyone could give me some suggestions on them? 1. If we save our documents in XML rules and these files should be published on Internet through WWW, what we can benefit from the XML files? And what are the drawbacks (is it too complex or time-consuming because we have to define a new set of XML elements to save the documents, and...
5
7644
by: Fred | last post by:
Not much expertise on XSLT and trying to understand it's uses when creating apps in VS.NET? If I wanted flexibility on the UI (View aspect of M.V.C.): - How does it compare with creating business components that can be consumed by WebForms, WinForms, mobile devices, etc? Is it even fair to compare the such technologies? - How about for cases when you need to display dynamic elements on the form/grid (as compared to knowing data elements...
2
2825
by: scott | last post by:
Hi, Just wondering what sort of problems and advantages people have found using stored procedures. I have an app developed in VB6 & VB.NET and our developers are starting to re-write some of the code in stored procedures (im advocating encryption of them). When deploying an application however stored procedure seem to add another level of complexity to installation. In future we also plan to have an basic ASP app with some of the...
5
1824
by: JayCallas | last post by:
I have a requirement where I need to perform a query for position information. But for some types of entries, I need to "expand" the row to include additional position rows. Let me explain with an example: An index is a security that is made up of components where each component has a "weight" or a number of shares. So if I have 1 share of the index, I have X shares of each component. AAPL is an Equity, CSCO is an Equity, SPY is an...
3
4232
by: Andrea | last post by:
Hello everyone, I'd like to know which are the main pros and cons of using XML implementation in business organizations. >From a technical perspective, I find XML powerful, but looks like it is being pushed more from technical people than from businessmen... So, some questions: 1. Pros and cons
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
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
2
3813
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.