LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2014, 08:07 AM   #16
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229

Quote:
Originally Posted by metaschima View Post
So then, how do you comment out code ?
If it's not needed, just delete it…
 
Old 06-29-2014, 11:03 AM   #17
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by mina86 View Post
If it's not needed, just delete it…
Well yes, but what about when you are debugging ? Say you want to test two or more pieces of code ? Yes, you can do it with the preprocessor, but smeezekitty said he hates it. So, if he hates the preprocessor but doesn't mind /**/ comments everywhere, then what is his solution ? I thought maybe he had one. Of course, it could be just a rant.
 
Old 06-29-2014, 12:01 PM   #18
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
The problem with abusing the preprocessor like that is the code path become unclear.
Usually I just use /* */ to comment it out. That is one disadvantage of using those
comments in code is that you lose that ability. I still don't mind them though

When I am testing sometimes I mark the code position and cut the code and paste it to another file.
 
Old 06-29-2014, 12:08 PM   #19
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by smeezekitty View Post
When I am testing sometimes I mark the code position and cut the code and paste it to another file.
Or one can delete the code, build debug version, and then undo changes on the file (any serious editor has that functionality). Or rename a function to something else while they test its different implementation. Or add code at its beginning and add “return” at the end of the new code so that the old one is unreachable. Or even wrap the code in “if (0) { … }”.

There are so many options that I don't recall when was the last time I used “#if 0”.
 
Old 06-29-2014, 12:13 PM   #20
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by mina86 View Post
Or even wrap the code in “if (0) { … }”.
That is also a possibility.
 
Old 06-29-2014, 12:15 PM   #21
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I don't really get what are we talking about, but I think that's what "if" is good for.
Code:
if (options.debug>=1) {
    fprintf (stderr, "Debug: x=%d, y=%d\n", x, y);
}
 
Old 06-29-2014, 12:30 PM   #22
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by mina86
and if you're using reasonable editor you probably can configure it to enter comments with some simple keystroke
Yeah, I don't know why anyone would write comment markers by hand. In Emacs, I can just use comment-dwim (M-;) which comments or uncomments the marked region. For example, with all the code marked, comment-dwim toggles between these 2:

Code:
#include <stdio.h>

int main() {
    printf("hello world");      /* a comment */
    return 0;
}
Code:
/* #include <stdio.h> */

/* int main() { */
/*     printf("hello world");      /\* a comment *\/ */
/*     return 0; */
/* } */
As you can see, it handles existing /* */ comments by escaping the comment markers.

Last edited by ntubski; 06-29-2014 at 12:30 PM. Reason: missing word
 
Old 06-29-2014, 01:25 PM   #23
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Certainly most IDEs can handle things properly. However, you can't just assume that everyone has a good IDE. I mean why else are Linux kernel devs so strict about style ? With a good IDE you can set your own style, but ... they're still strict about it.
 
Old 06-29-2014, 02:15 PM   #24
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by metaschima View Post
Certainly most IDEs can handle things properly. However, you can't just assume that everyone has a good IDE. I mean why else are Linux kernel devs so strict about style ? With a good IDE you can set your own style, but ... they're still strict about it.
For one, code is not always read in an IDE. For example, a lot of kernel code I read, I read in my mail client (or LKML archives).

If someone writing code does not have a decent editor, then their first priority is to get one, and not worry about comment style.
 
Old 06-30-2014, 06:31 AM   #25
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I end up using #if 0 #else #endif a lot because there are some prototype projects where they change the lower sensory layers a lot because they want to test which are better, but we want to keep the capability to talk to the other ones until we reach a final conclusion. As a result it's better to take the entire function and #if 0 it out until we know which direction we're going in.

The other thing there is if you try to /* */ the whole function, any random comment in the middle of that screws your attempt up.

And #if 0 is not perfect either, you have to have legal code within it. Make a file with bad syntax or just non-code within #if 0 and it won't always compile if you happen to have syntax based terms in there, for instance ";".

All a matter of preference though. I'm glad that C is C and I rarely encounter anyone's code that is unreadable; bad maybe, but not unreadable.
 
Old 06-30-2014, 08:54 AM   #26
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by rtmistler View Post
I end up using #if 0 #else #endif a lot because there are some prototype projects where they change the lower sensory layers a lot because they want to test which are better, but we want to keep the capability to talk to the other ones until we reach a final conclusion. As a result it's better to take the entire function and #if 0 it out until we know which direction we're going in.
In that case, define a macro and do “#if THAT_MACRO” and not “#if 0”, e.g.:

Code:
#define USE_NEW_API

/* … */

#ifdef USE_NEW_API

void do_foo(void) {
	do_something();
	do_something_else_using_new_api();
	finalise_or_something();
}

#else

void do_foo(void) {
	do_something_completely_different();
	use_the_old_api();
}

#endif

/* … */

int main(void) {
	/* … */
	do_foo()
	/* … */
}

#endif

Last edited by mina86; 06-30-2014 at 09:45 AM. Reason: Added missing endif.
 
Old 06-30-2014, 09:00 AM   #27
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
LOL Mina86, not in the project I'm on; the hardware changes come rapidly for "experiementation" purposes and then suddenly I have to support both in one software release (therefore no preprocessor directive but instead true if-else). The coupe de grace is "can we get BOTH of those at the same time so we can parallel their data under the same test conditions?"

Nobody uses it, then everybody uses it, the suddenly an entirely new sensor is miraculously found that does it all. ... and then I'm keeping track of three sensory outputs.
 
Old 06-30-2014, 09:47 AM   #28
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
So you're saying you can use “#if 0”, but you cannot use “#if TEST_MACRO”? I'm confused.

Also, VCS is your friend, delete what is not needed at given time, and bring it back if it later is needed again.
 
Old 06-30-2014, 10:10 AM   #29
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
No I could do that if it were my choice, it isn't.

I am saying that ultimately when they ask me to support "either or" as a configuration option, that preprocessor directives are useless at that point because all code will need to be included in the final result. And then further when they ask me to have parallel processes or threads talking to both sensors; recording the data from both at the same time so they can run a field test and compare the data - once again preprocessor directives are a moot point. In fact, in those situations, the "either or" case is a minor headache, the "both at the same time" case is an architectural expansion where I have to write a lot more code; also fully knowing that eventually much of it will be going away ... until someone long in the future says "You know ... we were getting better readings way back when we were using the <blah-blah> sensor ..." So a lot of this is the "R" part of R&D.

In thinking a bit further though on #if 0 versus #ifdef _SOME_WORDS_; I actually do not prefer unclear words for the reason that I see _TEST_MACRO_ or something like that and then I have to FIGURE OUT if that is ever declared. That's inconvenient. #if 0 is 100% clear, "This code is NOT included in the compilation".

I have a colleague who uses #ifdev _NEVER_ where they have #define _NEVER_ 0 somewhere. Yeah ... cool.

I don't mind that you choose to do what you do, please don't worry about my style. But like I say, this brief discussion has made me realize that I do like #if 0 because it is clear as to its intention.
 
Old 07-01-2014, 12:42 AM   #30
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Back to /* */ pair .v. //

/* */ pairs permit comments to spread over more than one line.
Unless that is you prefer a long, loopy winding, wrapped around line where the entire comment is only visible if you have set your editor to wrap lines).

So it is more useful to comment out entire chunks of code or text spread over many lines with one pair. (in // you would have to comment out each and every line and god help you in debugging if you forgot one line and more specially if you had to revert back after testing and forgot one line).

Also old c compilers may not permit //.

On the flip side, /* */ type comments within comments don't always work correctly. (// has eliminated the problem).

My choice : use both appropriately.

OK
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I get people to use Linux? I'm bad at converting people over. Mr. Hill Linux - Newbie 50 07-11-2020 10:41 AM
How do people learn to modify-patch Windows 9X and write programs? Holering Programming 5 05-10-2014 07:30 AM
read/write,write/write lock with smbclient fails swatidas11 Linux - Networking 1 03-10-2010 12:27 PM
LXer: People Who’ve Never Run Linux Shouldn’t Write About Linux LXer Syndicated Linux News 0 12-28-2007 09:50 AM
How do people write 4.3GB data DVDs? zoubidoo Linux - General 4 02-01-2007 01:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration