229 points · by signa11 · 8y ago · 124 comments
He had replaced Win 10 on his tower with Ubuntu and was writing a pretty complex program in C++. Doing some AI/ML primitives.
Thought to myself I am old yet went to the same University and would have been coding in C++ on Unix (Ultrix or OSF or Solaris) at that point in my life.
How in the world have we not moved on?
BTW, his code was so, so pretty. Mine would have been ugly. Kids are far better coders than we were 30 years ago. I was crazy proud.
C is likewise on a plateau. There is nothing categorically better than C for what C does; just different points in the design space that make different trade offs. E.g. Rust gives you memory safety through ownership, but you can't even make a doubly-linked list while staying within the ownership system. In a different point in the design space, Go has garbage collection, but that comes with its own trade offs. All of these might become obsolete when we have e.g. quantum computers. But in the meantime we are just playing with different trade offs, because we've captured all the low-hanging fruit decades ago.
Our computers and IoT devices will never be properly safe while the underlying kernel and bare metal stuff keeps being written in C.
C is the job safety of black hat hackers, security experts, companies selling memory protection utilities and anti-virus.
It was the explosion of free UNIX clones with *BSD and GNU/Linux that spread it around, until then it was just yet another systems programming language, already being replaced by C++ on desktop computer systems on the mid-90's.
Your otherwise good comment presents this like all-or-nothing when comparing it to C. To start with, C and Rust are equivalent for anything it cant borrow-check. Then, it's better than C in safety or expressiveness from there. Second, C developers use plenty external libraries to supplement the language's capabilities, including with ASM breaking abstraction gap. Likewise, whatever Rust cant handle can be proven safe with external tool, wrapped in safe interface, and used from there. This has been done in languages from C to Haskell.
So, there's different design points for sure but doubly-linked-list isnt an example of unfit for purpose vs C. If anything, that it's equal or better in safety making it a better instance in that design space. Likewise for Clay which was more C-like and used in device drivers.
Feature wise it was just plain inferior, in safety, modularity, compile time, OOP support.
Apple was on Object Pascal, then moved into C++. IBM was adopting C++ on their OS/2 tooling, everyone was building C++, Pascal and Basic frameworks on top of Win16, later BeOS also went C++.
Then free UNIX clones happened.
* distributed version control,
* open source culture and infrastructure (GitHub, GPL and Apache licenses...),
* dependency management and build tools (pull in many man years of effort into your project with a single declaration),
* HTTP and REST APIs (accidentally taught developers the importance of idempotency, statelessness, uniform interfaces, etc.),
* cloud infrastructure (no more need to purchase, install, configure, and secure expensive workstations before starting your project and business).
As for programming languages, there have been very few actual productivity gains since the standardization of Common Lisp.
Although this is mostly true, I think you forgot to mention that the latest trend of repositories with tens of thousands of libraries are a boost in productivity as well.
Even the Common Lisp world received a big boost in productivity when the Quicklisp library/system repository was created and consolidated.
This isn't true. Common Lisp had more than a few problems which later languages fixed:
1) Module system/encapsulation
Common Lisp has a notoriously fickle and opaque system for importing other code into your space.
2) Sequences as sequences rather than linked lists terminated by null.
Dealing with the fact that a list is a null-terminated linked list instead of an actual sequence/vector/tree is a big deal. Try implementing a printer for dotted pairs at some point. Talk about annoying and it costs you enormous performance.
I could keep going, but there are far better qualified people to talk about the many shortcoming of Common Lisp.
If you need a vector, just use a vector, don't use a list. Common Lisp has very flexible vectors and arrays.
Lists are the way they are because they're the fundamental block for manipulating/constructing lisp code itself.
>Common Lisp has a notoriously fickle and opaque system for importing other code into your space.
Care to elaborate? You just load it; everything is in its own, separate namespace.
The low level C/Unix layer is the foundation for everything else, so it has not changed very much. That's actually a good thing! It's stable and incrementally improving.
The higher levels of the stack have wildly changed -- JS, Python/Ruby/Perl, web frameworks, R and pandas, TypeScript, etc. not to mention all this cloud stuff. JS and Python are on microcontrollers!
I think that is working as intended. But if anyone had the misconception: we don't have the bandwidth to replace the lower levels of the stack! Microsoft doesn't either, and they have billions of dollars.
Nobody rewrites stuff "just because". That's why C++ is still relevant. (It's also why I think Rust is not really a C++ "replacement". It might be used for NEW stuff, but there will still be tons of C/C++ code around for decades. Again, the rate of rewriting/replacing is slower than you think.)
Now, to argue the opposite side: writing applications (as opposed to low-level libraries) in C++ does feel like a smell. Sometimes C++ is still a justified application language.
But it does feel like its territory has been encoached about by Obj C / Swift on iOS, Java/Kotlin on Android, and JS on the web. So there are not as many new C++ applications as there used to be, but there are still some.
And UWP, written in a mix of C++ and .NET Native is the future of the Windows desktop applications, even if it takes the next couple of years to eventually get there. All new APIs are UWP based.
This week's BUILD 2018 has proven that they are still on that direction, one day Win32 will join Win16.
And in Azure, well the underlying OS doesn't really matter for most languages with rich runtimes.
As for your remark regarding apps, there I fully agree.
C++ has been pushed down the stack, left for GPGPU shaders and low level OS features, with Microsoft being the only exception of having tooling parity with the other languages on their OS SDK.
Here Apple and Google have C++ on a little box bounded by Objective-C++ and JNI.
And on UWP, even if many high performance components are written in C++, most teams just use .NET Native.
When I saw my son coding in C++ I said to him why not Go or Rust?
But on the ride home thought doing it C++ makes total sense as no matter what he uses when gets a job knowing C++ is required today. So much older code still in C++.
BTW, he was working on lower level primitives as for higher level I would have suggested Python and a backup of R.
We tried but have yet to give up.
"It looks like Plan 9 failed simply because it fell short of being a compelling enough improvement on Unix to displace its ancestor. Compared to Plan 9, Unix creaks and clanks and has obvious rust spots, but it gets the job done well enough to hold its position. There is a lesson here for ambitious system architects: the most dangerous enemy of a better solution is an existing codebase that is just good enough."
— Eric S. Raymond
I suspect that 'us' coders might overvalue the language or tools we use, and undervalue the progress that might take place in the context of them. Not just when it comes to the next generation(s), but also in our own work.
For example, over the past decade or two I've 'improved' my toolage by learning vim and emacs, figuring out how to get Sourcemaps and live reloading working, and improving my command-line-fu. And yet, as a programmer, I feel all of these improvements pale in comparison to the stuff I learned that would be equally useful in Notepad as it would be in a decked out Sublime Text/VSCode/Emacs.
This becomes evident when I work on one-off scripts in the Chrome developer tools, or editing stuff using vim on a server. I'm reduced to the most basic tooling, and yet I can get stuff done in a fraction of the time that I'd have needed a few years ago.
As someone who's 5 years into my career and interested in moving past the not quite a senior engineer plateau, what would you say are the important things you've learned?
Never used Emacs.
I don't know what compiler you were using on Ultrix but C++ in that era was insanely bad.
People who think "goto" is evil should also give up the other jump statements: continue, break, and return (and also switch, though its not listed as a jump instruction in the C standard, at least not in '89 or '99).
You can see some contradictions in the paper regarding goto. For example, they state that deep nesting should be avoided, but goto should be avoided as well, even though one benefit of using goto is to limit nesting depth. From the Linux Kernel coding style doc: - unconditional statements are easier to understand and follow - nesting is reduced - errors by not updating individual exit points when making modifications are prevented - saves the compiler work to optimize redundant code away ;)
When Dijkstra wrote his famous essay "Go To Statement Considered Harmful" (1968), it was a manifesto against unstructured programming i.e. the spaghetti code. However, the use of "goto" per se does not imply unstructured programming. Donald Knuth wrote a wonderful essay "Structured Programming with go to Statements" (1974) to make this point.
Availability of "goto" in C merely gives us more flexibility, but it does not mean that we should start writing unstructured code.
Few people have worked on real spaghetti code, thousands of lines with no functions, no modules, just spectacular leaps forward, backwards, leaping forward into the middle of huge loops, leaping backwards into the middle of loops, giant loops nested with and partially overlapping other loops.
I worked on such code, trying to decompose it in order to organize it into subroutines. It resisted my efforts almost completely. Fortran IV I think.
Users were agitating for `continue` to join `break` for control flow interruption. Lua instead chose to provide all the non-structured control flows you would like as a primitive; scoping it lexically keeps it from breaking composition.
The closest I've come was my own code: minsweeper on my Ti-82 graphical calculator. I Separated the program in various sections, then used `goto` to jump where I needed. I was tempted at some point to use the "call program" facility instead, but that would have meant exposing those programs to the end users, so I just lumped everything in one file.
Reminds me why I love functions.
That makes no sense. goto is maligned because it's unstructured, the other constructs you list are structured and restricted. Much like loops, conditionals and function calls they're specific and "tamed".
Not only that, but the historical movement against goto happened in a context were goto was not just unstructured but unrestricted (to local functions).
Even K&R warns that goto is "infinitely abusable", and recommends against it when possible (aka outside of error handling & breaking from multiple loops as C does not have multi-level break).
With goto that's not guaranteed to be the case -- that was the spaghetti part.
vs.
if (False) throw ExceptionE;
switch (val)
while (cond)
{
case 42: /* wee: if val is 42, we go straight here */
break; /* this belongs to the while! */
}
I have written this kind of jig the past: if (...) {
} else switch (val) for(;;) {
}
:)(No, you can't use tail calls to represent state machines in C; that is not a feature of the C language but rather of a given implementation. And yes, you could model state machines with an enum variable and a big switch statement, but that's even harder to follow.)
What trips people up is when goto is used to jump across resource lifetime boundaries (which C++ addresses with RAII), when they maintain too much state in cross-state variables that they forget what their invariants are, and when they use a goto as a replacement for what should be a function call.
Using goto to implement e.g. a data processing loop, a non-recursive graph traversal algorithm, or a parsing state machine are all perfectly valid uses.
At a high level, this:
goto NewState;
is no less comprehensible or spaghetti-prone than this: state = NEW_STATE;
break;My uses of goto can be replaced with nested functions! The code is nicer, cleaner, and the equivalent code is generated (the nested functions get inlined).
Of course, nested functions aren't part of Standard C, but they are part of D-as-BetterC. (D has goto's too, but I don't need them anymore.)
https://dlang.org/spec/statement.html#continue-statement
also, what do you think of a partial compilation feature / tree shaking https://webpack.js.org/guides/tree-shaking/
YES it is dangerous due to stacks etc etc but hey, we're grown up adults, not script kiddies.
In practice, it doesn't. (The compiler inlines the code.) I know because I'm pretty picky about this sort of thing - it was why I was using goto in the first place.
> RAII
RAII can work, but it's a bit clunky compared to a nested function call. Additionally, if the code being factored is not necessarily the same on all paths, that doesn't fit well with RAII.
I guess you are not using goto as flow-control then.
That's a statement about what people say about the feature, not about the feature itself. The subtlety here is that malign as an adjective refers to something evil or ill-intentioned, while as a verb it means something closer to slander or defame. It's frequently (mostly?) used in a context of skepticism regarding the claims in question, especially in the construction much-maligned.
You just replace this:
void f(int n)
{
float *x = malloc(n * sizeof*x);
// ...
free(x);
}
with this: void f(int n)
{
float x[n];
// ...
}
edit: and you can exit your function anywhere without leaking.I just need to call your example function with the right n to corrupt the stack.
As for continue, break, and return, they have an explicit workflow, existing or resuming the current scope.
The only language where unconditional jumps make sense is Assembly.
The last time I actually wrote a goto statement was probably in some BASIC dialect.
In Scheme, because a lambda expression is a closure and shall be optimized with tail call elimination, it is considered as the ultimate goto. It's goto but with procedural abstraction.
Even call/cc is more structured than plain gotos.
On the other hand, using goto to jump back in the program flow, or multiple branching gotos are both asking for trouble.
I understand the how hamful goto statement is in general all of us know that. But there is very specific useful area in C. When Dijkstra wrote "GOTO Statement Considered Harmful", even before C was born, people tended to use goto statements everywhere because they were used to use assembly jump instructions. But we don't abuse goto statments anymore.
[1]: https://github.com/torvalds/linux/blob/master/net/socket.c#L...
goto is ok only for local error cleanup (or auto-generated code, e.g. for parsers etc).
EDIT: Or, for that matter, trusting the compiler to inline small-enough function calls?
A gcc extension that gets around macros is nested functions.
> trusting the compiler
99.99% of the time you should just trust the compiler to do the right thing. That 0.01% of the time use a well tested and maintained library.
Hating on goto in c is just cargo cult programming. goto in BASIC and FORTRAN are evil. Though back in the 1960's 1970's programmers often had no choice. Programmers used evil assembly goto hacks in order to get their programs to fit in memory/disk.
Problem with Dijkstra was he was a academic mathematician who despised practical programming where the program needed to run on the hardware available. Hint back in the 1970's professors like Dijkstra had unlimited accounts on the schools mainframe where everyone else had extremely limited accounts.
Perhaps goto is too valuable to let go; or letting it go results in code that is more complex than simply using it judiciously.
What strikes me as odd is that the very next minor release (2.0.5) is listed as 2.1M LOC, while 2.0 is listed as 6.1 MLOC. That's a reduction of 66%. Looking at a diff between the 2 releases, I do not see anything to explain that.
I know basics of C and C++ !!
https://www.us-cert.gov/bsi/articles/tools/source-code-analy...