News for hackers. Stuff that matters.
Re:How to C in 2016 by userbinator on Friday January 08, 2016 @07:26AM (#10864371)
I've noticed that "low-level" languages like C, or for a more extreme example, assembly languages, tend to encourage simpler code if for nothing other than the fact that everything takes many more lines than in higher-level languages. E.g. if you want to do OOP you'll have to allocate memory and call constructors manually, pass explicit 'this' parameters into all method calls, declare virtual function tables, etc. Certainly there are cases where this tempts too easy solutions like fixed-size overflow-prone buffers instead of dynamically allocating strings, but on the other hand it forces you to think more about whether you actually need to do something ("should this string ever be more than X bytes long? Maybe not, might as well use fixed-size array - but I should not forget to check that length!"), which I think is overall a good thing.
Re:How to C in 2016 by mrich on Friday January 08, 2016 @07:31AM (#10864394)
It also makes you think upfront about the design much more. With all the intricacies of writing correct low-level code you really want to write it once only and not try out different designs and refactor between them, where mistakes quickly happen.

Of course this is a disadvantage if you don't have sufficient experience in the problem domain. Sometimes I resort to writing prototypes in a script language in that case, translating the best design to the low-level language.

Re:How to C in 2016 by adrianN on Friday January 08, 2016 @10:08AM (#10865339)
A language that is "write it right the first time, refactoring is painful" is a poor fit for most problems. Requirements change, if your software can't be adapted easily your project will fail. The general failure of waterfall-style software development is a strong indicator that finding the "right" design up front usually doesn't work out.
Re:How to C in 2016 by kqr on Friday January 08, 2016 @08:09AM (#10864569)
> on the other hand it forces you to think more about whether you actually need to do something ("should this string ever be more than X bytes long? Maybe not, might as well use fixed-size array - but I should not forget to check that length!"), which I think is overall a good thing.

While that certainly can't be a bad thing, you just have to keep in mind that it forces you to think about a specific set of details – the ones that matter for the machine ("how many bytes will the binary representation of this username require") – and it may fool you into forgetting to think about higher-level concerns ("Does å compare equally to å?").

Re:How to C in 2016 by noobermin on Friday January 08, 2016 @10:50AM (#10865683)
Au contraire my friend, I've seen C code with "functions" that are >1000 lines long, probably ~10 parameters with lovely names like `lfp`, and where probably 40% of those lines are #ifdef and #endif.

The same code would be much simpler with C++ templates, but the "C being simple" really translates into limited, then you have that some have who never learned how to use macros and this unfounded and misunderstood fear of goto that gives you a nice long lines of error checking, each that that are duplicated if statements character by character where a goto and a single error handler would have worked.

Perhaps this is an exception, but low-level in my case did not correspond to simple code.

Re:How to C in 2016 by JoeAltmaier on Friday January 08, 2016 @10:55AM (#10865712)
In an extreme example, I replaced a 10,000 line C method with one template of less than a page of C++ code. It was marshaling different structures for transmission over a mailbox port. All that code copied and pasted and one or two numbers changed depending on the structure size.
Re:How to C in 2016 by moonchrome on Friday January 08, 2016 @08:36AM (#10864692)
Yeah, let's use copy & paste and C preprocessor macros as our abstraction tools ...

Avoiding unnecessary abstractions is important but at the same time those abstractions were invented for a reason. Basically the Go vs generics debate, except even more rudimentary. It's fine if your code doesn't need those abstractions, it sucks badly if it does (eg. gobject)

Re:How to C in 2016 by comex on Friday January 08, 2016 @10:56AM (#10865727)
Depends on your definition of rudimentary. The C preprocessor sucks in general, but you can use it to implement all sorts of abstractions Go basically isn't capable of.
Re:How to C in 2016 by moonchrome on Friday January 08, 2016 @04:05PM (#10868237)
If replacing standard high level programming concepts like generics/templates with C macros is your idea of a productive programming environment than I don't know what to say.

Just look at any C collection library for things like maps compared to C++.

Re:How to C in 2016 by pjmlp on Friday January 08, 2016 @08:59AM (#10864827)
This is only true of C.

Algol, PL/I, Mesa, Cedar, Modula-2 and many other languages of similar age or older than C, do offer both higher and lower level mechanisms.

Re:How to C in 2016 by ahoka on Friday January 08, 2016 @11:21AM (#10865943)
I think C does not make you write simpler code. It makes people take short-cuts. Static allocations, using a hand rolled linked list, skipping return value checks or boundary checks etc. are all typical to C programs.
Re:How to C in 2016 by vardump on Friday January 08, 2016 @04:44PM (#10868552)
Not sure why you've been voted down. I've seen too many lines of C and what you said is true most of the time.

Although developers skipping return value checks is true in most languages.

Re:How to C in 2016 by unscaled on Friday January 08, 2016 @09:38PM (#10869829)
> Although developers skipping return value checks is true in most languages.

Yes, which is why I disagree with Go's approach for error codes as return values. I'm not saying that exceptions are the right solution everywhere, but if you return a error code, you should make it pretty hard to ignore it. In languages with Algebraic Data Types it's very easy to use Maybe or a Error(code) | Success(result) type. You've then got to explicitly deconstruct that type to get the result, so it's a bit harder to just forget handling the error.

Re:How to C in 2016 by Gibbon1 on Friday January 08, 2016 @07:34PM (#10869398)
I have two things to say.

#1 The submitted article is very good. #2 I'd add to it, avoid pointer arithmetic when possible. Use array notation instead. Typically the difference in execution speed is zero to nil. #3 I'd really like a pragma that forces an exception for unchecked return values. Something like

    int DoSomeThing(/* whatever*/) #pragma exit "unchecked non_zero"
Meaning if DoSomething() doesn't return zero the program bails.
Re:How to C in 2016 by yoklov on Friday January 08, 2016 @09:09AM (#10864891)
I think it's mostly that C's lack of language features removes distractions. You have only a couple abstractions to work with if you're writing idiomatic C code, and so you can focus on the task at hand more easily.

The tradeoff is that the task at hand tends to have more implementation details in it than in higher level languages, but as you said, this isn't always bad.

Re:How to C in 2016 by shwouchk on Friday January 08, 2016 @10:16AM (#10865404)
Aaaand we got ourselves another site with an 8 or less char password
Re:How to C in 2016 by lmm on Friday January 08, 2016 @09:19AM (#10864971)
I think it only looks that way because people don't compare like with like. x lines of code C may well be simpler than x lines of a given alternative. But http://qdb.us/301922