![]() |
|
|
Welcome to the { mindfrost82.com } forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
What would be considered "unusual" C++ code?
I've been coding alone on pet projects for a while now and my code is
beginning to develop eccentricities that run against some features in C++. Would any of this be considered normal? 0: Using fprintf( stderr ...) instead of std::cerr 0: Making use of goto statements instead of brief functions (though I do use this carefully). 0: wrapping up large chunks of program behavior in a namespace to avoid a class that is used only once. 0: avoiding loops other than while. 0: Using C style strings where ever possible. If people have good reasons that these practices are to be stopped I will take their counsel and I would like to know what other odd things people have seen or do as well. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: What would be considered "unusual" C++ code?
RyanMcCoskrie ha scritto:
> I've been coding alone on pet projects for a while now and my code is > beginning to develop > eccentricities that run against some features in C++. Would any of > this be considered normal? > > > 0: Using fprintf( stderr ...) instead of std::cerr That is a religion issue. > 0: Making use of goto statements instead of brief functions (though I > do use this carefully). That cannot be considered normal. gotos usually reveal a strong (and unresolved) C programming background. C++ programmers very rarely need gotos, if ever. I used gotos maybe a couple of times in the last eight years and only for prototype code, having them replaced with more orthodox code in the final sources. Definitely avoid. > 0: wrapping up large chunks of program behavior in a namespace > to avoid a class that is used only once. I see nothing wrong with it. That's what namespaces are for. > 0: avoiding loops other than while. What's wrong with for loops? do-while loops are just extremely rare to occur in practice, but not bad programming style per-se. > 0: Using C style strings where ever possible. If with "C style strings" you mean using char* with malloc/free/strdup, then you are just making your life (much) more complicated than what it ought to be. You're welcome. HTH, Ganesh -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: What would be considered "unusual" C++ code?
RyanMcCoskrie wrote, On 2.8.2008 12:28:
> I've been coding alone on pet projects for a while now and my code is > beginning to develop > eccentricities that run against some features in C++. Would any of > this be considered normal? > > > 0: Using fprintf( stderr ...) instead of std::cerr If you do it because C++ IO streams formatting is rather clumsy, I can understand that. Otherwise I do not understand :) > > 0: Making use of goto statements instead of brief functions (though I > do use this carefully). Sometimes, goto is the best means to an end. But as you say, it has to be used carefully. > > 0: wrapping up large chunks of program behavior in a namespace > to avoid a class that is used only once. I do not completely understand what you mean here. Using namespaces to organize code is a good thing. But I would not avoid class with a single use site either. > > 0: avoiding loops other than while. Ugh! Why? for(;;) is very useful construct for looping over containers with iterators and such. > > 0: Using C style strings where ever possible. Why? I use C style strings only when I do not want to manipulate with their contents much, like constant strings or literals; when I am only going to print them or write them into file as the next thing. Std::string is so much easier to handle than C style strings with all the clumsy strfoo() functions. > > > If people have good reasons that these practices are to be stopped I > will take their counsel > and I would like to know what other odd things people have seen or do > as well. > -- VH [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: What would be considered "unusual" C++ code?
> 0: Making use of goto statements instead of brief functions (though I
> do use this carefully). Goto leads often to so called spaghetti code where the executed sequence of the code is not clear. Usually, a goto can be replaced by a function, if-statement or some sort of loop. These replacements have a clearer meaning and other people are able to understand your code quicker. Thats why these replacement have been developed. > 0: wrapping up large chunks of program behavior in a namespace > to avoid a class that is used only once. Did not get that. > 0: avoiding loops other than while. What for? Take the loop which solves the problem best is better. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: What would be considered "unusual" C++ code?
RyanMcCoskrie wrote:
> I've been coding alone on pet projects for a while now and my code is > beginning to develop > eccentricities that run against some features in C++. Would any of > this be considered normal? > > > 0: Using fprintf( stderr ...) instead of std::cerr You really really shouldn't do that (s|f|)printf isn't typesafe and these kind of errors can show up under very subtle conditions. Consider the following: <snip> const char * s ="%d\n"; printf(s,3L); </snip> This works perfectly for me, because sizeof(long)==sizeof(int) on my platform. But I can't move it over to another one where that's not the case. Formatting stuff on an ostream is much more verbose and storing/restoring the current state can be a real pain in the neck. It would be nice to have more standardized functions/operators for things like this. But on the other hand you're able overwrite the << operator hides complexity of object output from you. > 0: Making use of goto statements instead of brief functions (though I > do use this carefully). Uhh -- I will not comment on that. It's a looong discussion and if even godfather knuth can demonstrate that in some circumstances it's preferable to use goto, who am I to reject it all together. > 0: wrapping up large chunks of program behavior in a namespace > to avoid a class that is used only once. I think there's nothing wrong with classes that are only used once. Even with object instances of such classes. First of all think of a class as a namespace with additional properties: a. It allows access control via private,public,protected and inheritance b. It may work on a "hidden" chunk of data. If this is meant to be static for the whole program you call it static for the class (and still don't need an instance). If you need multiple instances you need object instances of his class. So classes as namespace replacements make sense for me. > 0: avoiding loops other than while. Never heard that that for() {} loops are supposed to be bad. They tend to become boring when you have to code <snip> for(C::ierator i=c.begin();i!=c.end(),i++) </snip> all over the place. But I guess that's the reason the for_each template was born. > 0: Using C style strings where ever possible. No -- only use it where it isn't avoidable. This one is the reason for many many stack overflow errors, injection of malicious code etc... It'll blow up your local power plant. I still wonder why the STL still employs it at many places without even alternative calls accepting std::string & e.g: there is no std::fstream(std::string &) but there is no std::fstream(const char *) or codecvt<...>::in etc.. But avoid it wherever you can. > If people have good reasons that these practices are to be stopped I > will take their counsel > and I would like to know what other odd things people have seen or do > as well. > My 0.06€ O. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: What would be considered "unusual" C++ code?
RyanMcCoskrie <ryan.mccoskrie@gmail.com> wrote:
> Would any of this be considered normal? > > 0: Using fprintf( stderr ...) instead of std::cerr This is not normal (IME.) Reason not to use: fprintf isn't typesafe. Errors only happen at runtime which makes them difficult to test (especially in this case, since you are in an error condition when the fprintf error would occur, so which error is causing the problem?) > 0: Making use of goto statements instead of brief functions (though > I do use this carefully). This is not normal. Reason not to use: goto's can jump up the page in code so when encountered, one has to scan the entire function to figure out where the SP might end up. This coupled with large functions (you imply that you shun "brief" functions,) make code harder to read. Also, as the "gotos considered harmful" paper shows, it makes it harder to follow program flow. When you are at a line in the program below a label, how do you keep track of how you got there? > 0: wrapping up large chunks of program behavior in a namespace to > avoid a class that is used only once. I'm not sure I know what you mean by this. I see namespaces traditionally used to wrap up libraries, so as to avoid having to append warts on every identifier in the library to ensure uniqueness. > 0: avoiding loops other than while. Not all that unusual. Most people seem to avoid the do { } while() loop but use for and while loops almost interchangeably. I personally use a for loop is the "conditional variable" is only used inside the body of the loop and a while if it is needed outside the body of the loop. > 0: Using C style strings where ever possible. Pretty normal, especially as function parameters and return values. > If people have good reasons that these practices are to be stopped > I will take their counsel There you go. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: What would be considered "unusual" C++ code?
On 2008-08-02 12:28, RyanMcCoskrie wrote:
> I've been coding alone on pet projects for a while now and my code is > beginning to develop > eccentricities that run against some features in C++. Would any of > this be considered normal? > > > 0: Using fprintf( stderr ...) instead of std::cerr Perhaps not normal but hardly unheard of either, especially in code that has evolved from C to C++. Besides, some people prefer fprint to C++ streams (I would use them too if they were not type unsafe). > 0: Making use of goto statements instead of brief functions (though I > do use this carefully). Sounds like a really bad idea, goto has been known for decades to produce bad code. I've seen some usage that might be acceptable, but those are far between. > 0: wrapping up large chunks of program behavior in a namespace > to avoid a class that is used only once. Sounds like what namespaces are for, as long as each namespace provides a limited set of functionality (so you do not get one namespace for just about anything). > 0: avoiding loops other than while. I can not see any reason why you would want to avoid the for-loop, though I seldom use the do-loop myself. > 0: Using C style strings where ever possible. Unless you have severe performance constraints this sounds like a bad idea, std::string is much easier to use and much safer too. -- Erik Wikström [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: What would be considered "unusual" C++ code?
RyanMcCoskrie wrote:
> I've been coding alone on pet projects for a while now and my code is > beginning to develop > eccentricities that run against some features in C++. Would any of > this be considered normal? > > > 0: Using fprintf( stderr ...) instead of std::cerr If you intend to have your code portable to C but otherwise it will just make it slower for other C++ programmers to read your intent from your code. > > 0: Making use of goto statements instead of brief functions (though I > do use this carefully). However carefully, do not do it because it causes a maintenance problem (any change to the code must now be checked to ensure that it still works as planned.) > > 0: wrapping up large chunks of program behavior in a namespace > to avoid a class that is used only once. Not sure exactly what you mean by this. If the code truly is only used once, fine but it adds work the moment you decide to use it a second time. > > 0: avoiding loops other than while. That seems eccentric. My rule is that any loop controlled by a counter of some form should be written as a for-loop whilst any loop where the exit is governed by a change of state is written as a while-loop > > 0: Using C style strings where ever possible. Sorry, that is really silly. C style strings are sources of buffer overflow errors. For most purposes they are inferior to std::string. If you need an immutable string a char const * set to point to a literal is acceptable but we are trying to address this kind of issue in the upcoming revision of the C++ Standard. > > > If people have good reasons that these practices are to be stopped I > will take their counsel > and I would like to know what other odd things people have seen or do > as well. > -- Note that robinton.demon.co.uk addresses are no longer valid. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: What would be considered "unusual" C++ code?
I should have mentioned...
1. I don't have any doc's on the STDL other than the (obfuscated) GNU headers. 2. I have a rare problem called Non Verbal-learning Disorder (the name doesn't do it justice, this is almost but not quite Asperger's disorder). People with this problem are no or little skills other than the two basic elements of language: codes and logic. To compensate for deficits everywhere else in the brain these two abilities are "off the charts". Any way. to those who don't approve of it, you're right: I shouldn't use fprintf the way that I have been and I'm definitely switching to C++ strings. As to gotos... I only use them in two ways: 1. if ( /*reason the program _will_ work*/) goto setup_foo; exit (EXIT_FAILURE); setup_foo: /*rest of code here*/ 2. switch( bar ){ case '1': goto handle_baz; case '2': goto handle_wible; default: return false; } handle_baz: /*deal with baz*/ return true; handle_wibble: /*deal with wibble*/ return true; Are those really so bad? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: What would be considered "unusual" C++ code?
On 2008-08-03 11:05, RyanMcCoskrie wrote:
> Any way. to those who don't approve of it, you're right: I shouldn't > use > fprintf the way that I have been and I'm definitely switching to C++ > strings. > > > As to gotos... I only use them in two ways: > > 1. > > if ( /*reason the program _will_ work*/) > goto setup_foo; > exit (EXIT_FAILURE); > > setup_foo: > /*rest of code here*/ Either use else: if ( /*reason the program _will_ work*/) { /*rest of code here*/ } else { exit (EXIT_FAILURE); } or if ( /*reason the program _will not_ work*/) { exit (EXIT_FAILURE); } /*rest of code here*/ Some people prefer structured programming (where there is only one entry and one exit from a function) in which case the second alternative is no good. Personally I rather like it (I call it "early return") since it keeps the indentation low and I know that I no longer have to worry about things once I've checked for them. Of course, like everything else I cannot and should not always be used. > 2. > > switch( bar ){ > case '1': goto handle_baz; > case '2': goto handle_wible; > default: return false; > } > > handle_baz: > /*deal with baz*/ > return true; > > handle_wibble: > /*deal with wibble*/ > return true; switch( bar ) { case '1': /*deal with baz*/ break; case '2': /*deal with wibble*/ break; default: return false; } return true; > Are those really so bad? Maybe not, but they are not the way it is usually done, which will confuse some people and many will find the code harder to read. Besides while careful use of goto can produce high quality code it also makes it very easy to make easy (and tempting) to do bad things too, by avoiding goto you minimise that risk. -- Erik Wikström [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|