473,421 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

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 7422
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**********@invalid.invalid> writes:
Jarno A Wuolijoki wrote:
On Tue, 21 Dec 2004, Charlie Gordon wrote:
"Jarno A Wuolijoki" <jw******@cs.Helsinki.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_Keith) 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**********@invalid.invalid> writes:
Jarno A Wuolijoki wrote:
On Tue, 21 Dec 2004, Charlie Gordon wrote:

"Jarno A Wuolijoki" <jw******@cs.Helsinki.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**********@invalid.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_Keith) 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**********@invalid.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.org> 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.Helsinki.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
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,...
2
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...
5
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...
2
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...
5
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...
3
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
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,...
0
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...

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.