MegaSack DRAW - This year's winner is user - rgwb
We will be in touch
It's late. I'd like to be at home. But instead I'm reading through mountains of poorly written jumbled up and incomprehensible C++ (looking at you, Qt).
What is it about this language that encourages people to write utterly opaque code?
I've got a function with what seems to be a variable called 'p'. But it's clearly not a local, and it's not in the class, so I have to go hunting around for it in the fricking class hierarchy. And by the time I've found it, I've forgotten why I cared in the first place!
I hate C++. It's a massive, kludged together, bodged lean-to-upon-a-lean-to mish mash of ideas bolted together. It seems to encourage people to experiment with all its new shiny clever features when they really should be just getting on with engineering something.
It's a bit like democracy in that it's the worst option out there, apart from all the others... 🙂
convert it to this...
http://en.wikipedia.org/wiki/Brain****
wondering if (a) that'll get past rude word filters, and (b) if I earn an infraction for attempting to get rude words on to the thread.
c++ is better than java
gimme plain ansi c any day though
Sounds more a case of poor documenting/commenting in existing code.
I enjoyed c++ and Java.
I love C++.
But if a solution to your problem is looking up a class hierarchy for a variable 'p' then its not c++'s fault but over enthiusiastic use of (deep) inheritance and bad variable naming. I'd find whoever wrote it and tell him he's a ****
I use it day in day out and it's not so bad (Python & C are tops). With a good coding-standard, code reviews, static-analysis (etc) and avoiding all the whizz-bang features it's ok. Without those checks in place it's a good language to make a mess with. It could always be worse, you could be using PHP.
Sounds more a case of poor documenting/commenting in existing code.
Prob already been linked to but examples of how commenting should be done:
[url= http://stackoverflow.com/questions/184618/what-is-the-best-comment-in-source-code-you-have-ever-encountered ]commentitus[/url]
instead I'm reading through mountains of poorly written jumbled up and incomprehensible C++
Or in other words normal C++ 🙂
At least with C you know where you stand e.g. foo[5] == 5[foo] and other such nonsense, but C++ gulls you into thinking this might be a fully fledged language, but no..
[quote=oldnpastit ]What is it about this language that encourages people to write utterly opaque code?
Because they can.
The only variable I can recall from the C++ modules at uni was at which point to fall asleep. 🙂
C++ is almost designed be to opaque by its very nature, geekiness in the extreme for geekiness's sake. Combine that with the programmers who make no effort to document what they've done (job preservation ?) and you have a recipe for an impenetrable jungle
..?..
and
..?..
What you all talking about..?..
C++ makes me feel like a real programmer.
:(){ :|: & };:
[quote=IanMunro ]At least with C you know where you stand e.g. foo[5] == 5[foo] and other such nonsense
for(int i=0;i<100;printf((++i%15)?(i%5)?(i%3)?"%d\n":"fizz\n":"buzz\n":"fizzbuzz\n",i));
I'd agree with jonnock. I'm not a c++ master but I think many of the problems are caused by people tryign to be too clever and use every feature. KISS. Give classes and functions good interfaces and it should be easy.
🙂 @ aracer
many of the problems are caused by people tryign to be too clever and use every feature.
It seems to be encouraged by the language though. With C you have to keep things fairly simple. With C++ there seems to be a tendency to create wildly over-elaborate frameworks. The code I'm looking at has just vast pits full of pointless glue code. I spend my days wading through this gloop looking for the nuggets of code that actually do anything useful. It's so baffling that even though there's this wonderful framework for making everything super generic and flexible, it's impossible to change it. All I want is to draw some fairly simple widgets on a screen reasonably quickly.
KISS
+1
[quote=oldnpastit said] With C++ there seems to be a tendency to create wildly over-elaborate frameworks.
The curse of OO ? (see Java for this also)
KISS
Note this applies to all languages not just c++. I can make code that is hell to debug in any language. Just employ me and give me a month or so without any code reviews.
The OP's problem is all oop's fault, see post above.
c++'s template metaprogramming allows the creation of fantastic interfaces - see std::vector (or anything in the stl) for example. Try doing that with a messy deep nest of inheritance and overloading.
[quote=oldnpastit ]It seems to be encouraged by the language though. With C you have to keep things fairly simple. With C++ there seems to be a tendency to create wildly over-elaborate frameworks.
Maybe I benefit from being a C programmer who writes C++ code then. I know full well how to do it horribly in C (see above) and simply use the added features of C++ to add some structure rather than the structure being the whole point.
Though as always, and as discussed above the issue is control over the environment in which coding takes place rather than anything intrinsic to the language.
C++ is almost designed be to opaque by its very nature, geekiness in the extreme for geekiness's sake
and languages like Perl are OK then ?
C++ is like it is for very valid reasons - wanting to extend the power of C and utilise the standard C compilers and linkers that were on all the different flavours of Unix around at the time.
And it has evolved with time and has let people experiment with different design and programming paradigms - a vast amount of what is in C# and Java now is based on ideas that were prototyped in the C++ language.
Some old code has suffered because of this - for a while it was considered good design to have massive and deep class hierarchies and then design ideas changed to more aggregation and composition.
it is perfectly possible to write modern style code in C++ which is very similar to modern Java or C#, but because of the legacy growth of the language it is also possible to write sloppy code in a mix of poor styles, and in my experience there are a lot of very mediocre C++ programmers around who do this.
The transactional nature of object construction and destruction in C++ is something I still miss when working with C# and Java and the expertise you need to overcome some of the memory and non-deterministic performance characteristics in those languages means that they aren't much of an advantage at all over C++.
As a programmer that has progressed from assembler to c, and looking at potentially moving to C++ in the future. Anyone know of any good books that describe the way C++ can be used in a KISS fashion? Most books I come across just explain what C++ does, rather than how best to use it. I guess I'm really asking about OO, rather than C++. What I'm after is some guiding text that will help me use it wisely rather than just use it the first way that springs to mind.
PS before anyone suggests not using C++, and going for a different language, that is out of the question for a multitude of reasons. Not least that in the embedded world that I live, its assembler, C or C++!
I haven't coded in C++ since university. Since then I have used Delphi and C# with a few bits and pieces thrown in along the way. Is it not possible (even easy) to write spaghetti code in all languages?
Or do you think C++ and the framework lends itself spectacularly well to this outcome?
Favourite comment from link above:
//Dear future me. Please forgive me.
//I can't even begin to express how sorry I am.
Urbanhiker have a look at [url= http://www.amazon.co.uk/gp/aw/d/0201704315?pc_redir=1414054070&robot_redir=1 ]Modern C++ Design[/url]
Note: This will not satisfy your OO needs - maybe look at [url= http://www.amazon.co.uk/gp/aw/d/0201633612?pc_redir=1414237553&robot_redir=1 ]Design Patterns[/url] for that.
C++ is a powerful language for making things do exactly what you want them to do
By its nature it doesn't tolerate fools
I love it
Anyone know of any good books that describe the way C++ can be used in a KISS fashion?
[url=
in c++[/url]
[url=
2[/url]
Urbanhiker try Accelerated C++, despite being 13 years old it's still fairly current.
Isto,
That stackoverflow thread is the funniest thing I have seen for a long, long time.
Thanks!
Never been a fan of C++, fine with C, and C# as well - but not C++. Not sure exactly why..
Anyone know of any good books that describe the way C++ can be used in a KISS fashion?
Generally speaking you don't need a good book to do that.
The only rule I've ever gone by (many years ago now, as most embedded still seems to live on C) was "if you can do it simpler in C, don't use the C++ feature".
Yes, Modern C++ might not be a good choice as it is heavy on the template metaprogramming style, which is important and useful, but more advanced.
Accelerated C++ might be a good bet - I haven't read it but it is by Andrew Koenig and Barbera Moo and their other books are excellent.
Also Bjarne Stroustrup has a Tour of C++ and also the C++ programming language, I have read a few editions of the latter and, although it is not a great a book as a lot of the AT&T derived books on C were, it is worth reading as he was the inventor and you can see an essence of why he designed the language.
The Herb Sutter books are excellent as well.
That stackoverflow thread is the funniest thing I have seen for a long, long time.
No problem, first time I read it in work I was laughing like a maniac. It does make you want to get more creative with your comments....after all they are going to be viewed by a somewhat limited audience.
I think C++ and the like is behind the existence of so many "Esoteric" programming languages - invented to keep programmers human and sane. Brain**** already mentioned above but my favourite was always INTERCAL with the wonderful COME FROM instruction and the invitation to preface instructions with PLEASE.
Yes you can write horrible code in any language, but C++ seems to be specifically designed around that goal!
And it is only getting better at it with each new standard.
Ah... template meta-programming that'll screw them up.
Ooh for [url= http://en.wikipedia.org/wiki/C%2B%2B11 ]C++11[/url] lets add automatic types. And lambda functions. And attributes.
Still not complex enough? Ok. For [url= http://en.wikipedia.org/wiki/C%2B%2B14 ]C++14[/url] let's add automatic return types, generic lambdas, and lambda capture expressions and variable templates.
That should keep them busy while we work on [url= http://en.wikipedia.org/wiki/C%2B%2B17 ]C++17[/url]...
The software we sell and work with uses C++ under it's own language based on C++, if there is one thing I teach on training courses is if you send me something without comments for support I'll send it back. Of course I always give up half way through a project. I'm currently working with a guy in France on something, I'm just commenting
///MIKES BIT
////END MIKES BIT
when I step into his stuff.
Qt was a revelation for us allowing nice clean code and a lot of highly abstracted classes to do neat stuff. Seems a shame the OP's colleague cocked it all up.
And it is only getting better at it with each new standard.
Yes other languages never add new features with new releases.
Never been afan of C++,fine with C, and C# as well - but not C++. Not sure exactly why..
You should try Managed C++ 😈
Yes other languages never add new features with new releases.
Yes but the motivation seems different.
Other languages [i]try[/i] to add features which improve them, that make them simpler, more elegant or more powerful.
The C++ Working Group seem intent on adding features that are impenetrable to average programmers and only fully understood by people on the C++ Working Group. (Who then write books explaining it!)
You should try Managed C++
We use Embedded C++ with no heap allocation.
I think it actually makes it easier 😀
Oh I wasn't suggesting it was easier - once had the pleasure of having to use it to interface hardware with C style interfaces into .Net system. Interesting because it gives you full access to all the standard low level C stuff, so allows you to break managed .Net code if you're not careful!
On the comments front, I'm doing some moonlighting for an old employer at the moment and stumbled across a bit of code (in the middle of a trivial function to do with multiple currency support) which I'd commented as "I don't know what this does but if I take it out nothing works". I thought that was a silly situation so I figured I'd work out what was going on. So it was that after 6 billable hours I've amended it to "I still don't know what this does but if I take it out nothing works".
Bloody Mivascript.
There are worse things than C++.
I have some prototype code that features a webserver implemented in Matlab.
you dont blame the dog for dogsh*t on the path, blame the owner.
any issues with any programming language is squarley laid at the Programmer, and their attitude to their craft.
aracer: yeah been there, done that, got the mental scarring to prove it 😀
We actually do stuff like that fairly often as we have codebases in a mix of legacy C and Embedded C++ that interface with C# test harnesses.
On the bright side it is fairly easy to sweep it all under a virtual carpet and present a usable interface on top 🙂
you dont blame the dog for dogsh*t on the path, blame the owner.
Fair enough, but in this particular case the creator of the dog (DogGod?) decided to give it a vicious temper, chronic diarrhoea and fourteen bum holes.
stumbled across a bit of code (in the middle of a trivial function to do with multiple currency support) which I'd commented as "I don't know what this does but if I take it out nothing works".
We had a project delivered once where the dev was on the edge of some issues and was about to run off to live with his german girlfriend, all the comments were in German.
Asked a mate for some assistance (he's in here) web page comments came back suitably expletive laden 🙂
Pfft comments. Only losers use comments 🙂
Used to work with someone who was heavily into Gilbert and Sullivan and he used the character's names for variables and functions
stumbled across a bit of code (in the middle of a trivial function to do with multiple currency support) which I'd commented as "I don't know what this does but if I take it out nothing works".
hey i wrote that!!!!!!!!!!!!!!!!!! 🙂
Pfft comments. Only losers use comments
As per the Stackoverflow thread linked to earlier:
[code]// no comments for you
// it was hard to write
// so it should be hard to read[/code]
😀
We had a developer who was meticulous about commenting his code, but without imparting any useful information whatsoever
//////////////////////////////////
// function : DoCalc
// parameters :
// parm1 : integer
// parm2 : integer
// return value : integer
int DoCalc(int parm1, int parm2) {
Yep that's what happens when you have a coding standard that says every function and every argument must have inline documentation, but no one enforces the [i]quality[/i] of those comments.
Still a marginal improvement on the old favourite:
[code]i++; // increment i by one[/code]
(Though the trouble with C++ is you [i]might[/i] actually need that comment, because the ++ operator [i]might[/i] have been overloaded to do something else entirely!)
current thoughts are that comments are only advisable at the prologue level, otherwise variable/method/class names should be sufficient - and if they aren't then refactor until they are.
I had some code to give interviewees and it was all named to be obvious, like currentNode, and previousNode, and the guy suggested the names were giving him problems and he would rename them to something like A1 and A2!
We had a developer who was meticulous about commenting his code, but without imparting any useful information whatsoever
Maybe it was done for an auto documentation creator like javadoc?
^^ some all time classics of all time there
it does remind me now to remove the stuff in the
//Take Out before Finishing or it will run like a dog
//To here
comments
That link is brilliant.
[code]
//
// Dear maintainer:
//
// Once you are done trying to 'optimize' this routine,
// and have realized what a terrible mistake that was,
// please increment the following counter as a warning
// to the next guy:
//
// total_hours_wasted_here = 42
//
[/code]
/* 06/03/1984
just use 2 digits in the year here to save some space
*/
yeah we used to have a coding standard that specified almost exactly the same as that DoCalc() function above.
some code would look like that. some would have an essay explain the intricacies of exactly what each parameter meant.
if the parameters use proper words and not i then it's totally unnecessary.
but then our coding standard also specified a unified layout of where the squiggly brackets go, that was different to almost every normal convention.
and it also specified that C was dangerous and should never be used for embedded code.
we broke almost every recommendation in the standard. includign using C for embedded, writing our own runtime in defiance at being told we had to use VXworks.
and GOTO is a perfectly valid statement tyvm.
edit: oh and one guy would explain exactly what every function does in pseudocode that was essentially syntactically correct Pascal, for every C and Fortran function.
Sadly not, all lovingly handcrafted.Maybe it was done for an auto documentation creator like javadoc?
I survived the great curly bracket war of '96. Grown men almost coming to blows over indentation? Yep.our coding standard also specified a unified layout of where the squiggly brackets go
Still howling at that StackOverflow page.
I'm tempted to try and dig out some of my old code, in a past life I'd to write code which used one language embedded inline in another, which was wrappered in a third.
Knowing that I was the only one ever likely to read it, I used to be prone to leaving myself notes in part to remind myself what the hell I was doing but largely to keep myself amused when writing mundane routines.
I miss const 🙁 and everyone seems to use the form i++ instead of ++i even when they don't need the value of i - winds me up 🙁
auto-generated code featured a lot in the work we did too. but only one level of language wrappering.
that'll be another coding standard infringement 😈
but then our coding standard also specified a unified layout of where the squiggly brackets go, that was different to almost every normal convention.
Ours has normal squiggly brackets but dictates that round and square brackets must always be preceded by a space unless the previous character was a bracket.
Thus producing code that looks different from anything you'd find in a textbook or online, and that every editor you use tries to automatically reformat.
[code]int some_function (int aparam)
{
array_a [1] = other_function (2, 3);
if (multi_array [2][3] == Aclass.thing.othermethod (array_a [1])
{
// etc etc[/code]
Oh and this is enforced for [i]every[/i] language we use.
and GOTO is a perfectly valid statement tyvm.
You are wrong.
[url= https://www.thc.org/root/phun/unmaintain.html ]This[/url] is very useful wrt coding standards.
I survived the great curly bracket war of '96. Grown men almost coming to blows over indentation? Yep
We had a dyslexic C++ programmer which was kind of challenging for him as well as us
Indentation meant nothing to him. Providing the braces matched it didn't matter what it looked like
OK then, GOTO is half a perfectly valid statement 😉
// This shouldn't happen, if it does, then the bits that automagically
// worked when I wrote it have stopped working
Love it when stuff automagically works 😀
OK then, GOTO is half a perfectly valid statement
Can't think of any good reason to ever use (a traditional C-style unscoped) GOTO to be honest.
I'm sure there are some edge-cases on really really tightly optimised stuff, but I suspect even that is pretty dubious given how good compilers are at optimising these days.
It's not inherently evil - but there are usually better ways to do it.
[code]
int SomeFunction ( int aparam )
{
array_a [1] = OtherFunction ( 2, 3 );
if ( multi_array [2][3] == Aclass.thing.othermethod ( array_a [1] )
{
// etc etc
}
}
[/code]
ours would be more like that.
exactly 4 spaces indent. { and } indented in line with the content. 1 clear space in far too many places. CAPS use all set to the same, regardless of typical conventions.
I think only Fortran escpaed the standard, since the starting column for th text is fixed, and you may as well have CAPS LOCK on permamently.
edit: hmm that didn't work as intended... let's try again... edit, sod it, /me blames windows... edit: might have worked, but still blame windows.
Yeah the [code ] blocks don't preserve spaces for some reason - you need to put them in as non-breaking space HTML entities: amperspand followed by nbsp;
*Narrows eyes*
You've missed out the closing bracket of the if on purpose haven't you?
bugger I must be tired, I felt the need to drop it into something and check the brackets....
also got something sent over by a mate that I did a very long time ago now including the following gem
NOT FINISHED - to be completed by Mike Smith
[quote=GrahamS ]On the bright side it is fairly easy to sweep it all under a virtual carpet and present a usable interface on top
A most excellent summary of Managed C++!
[quote=GrahamS ]Can't think of any good reason to ever use (a traditional C-style unscoped) GOTO to be honest.
Oh goodie, I do like a GOTO war. Shall dig out the code somebody used as an example of where one made the code better last time I had this discussion elsewhere...
Yes please Mike 😀
jeeez are we going to get on the
Vi
'v'
Emacs
debate soon and we all know where that will lead
VI RULES!!!!!!!!!!!

