Live data from Hacker News

On becoming an expert C programmer

isthe.com

51–60 of 80 posts

Re: On becoming an expert C programmer

#52

Whenever I've looked at an open source C programs, they go well beyond what I've learned in K&R. Learning C these days means mastering Make, autotools, macros, POSIX and Glib. Knowing these is the difference between your contrived linked-list example and creating software that can actually be deployed and is useful.

Those don't seem like "these days" things. In fact, some is much more "old days". Glib is just another library like any other, and POSIX is just a library plus specification of the details any platform would have to specify. And if you're not on something that tries hard to look like a *nix, POSIX mastery isn't going to help you. Autotools is a plague slowly being eradicated, the problems it ostensibly solved have be…

I wouldn't regard POSIX as "just another library". It is the interface between your program and the operating system. In order to gain a healthy understanding of things like memory mapping, signals and process control, you have to go beyond the corresponding man pages.

Re: On becoming an expert C programmer

#53

Earlier quoted context omitted.

> I can't understand what undefined behaviors C has, because it is the most simple and defined language I do know of. C has lots of undefined behaviors because the language was designed to be easy for compiler writers to implement. As such, a lot of decisions were left to the compiler writers, which is what "undefined" means. Here is a (probably partial) list of undefined behaviors in C. (For a full list you'd have t…

Sure, there is a lot of code you could write that results in undefined behaviour. However, none of it is code that you should write, nor is it code that an expert would write.

While your sentence can be true until we get to defining “expert”, the problem is that there are not going to be many experts if to be an expert at C, one has to be able to write useful code that does not accidentally invoke undefined behavior.

- these four bug reports in NTP are for undefined behaviors. You can say that the NTP maintainers are not experts, the problem is that they are the maintainers of NTP: http://bugs.ntp.org/buglist.cgi?emailreporter2=1&emailtype2=...

- a famous undefined behavior was found in OpenSSL recently, but there were and remain plenty more. Sure, the authors of OpenSSL aren't experts, the problem is, they are the people writing and maintaining OpenSSL.

- The Linux kernel has had its share of undefined behaviors. Usually, the developers blame it on the compiler, of which the kernel admits only one (GCC—at least as of recently). There was the time when GCC was blamed for taking advantage of strict aliasing rules, and there was GCC removing of a NULL test on an execution path that dereferenced NULL. If the developers wrote for more compilers, they would realize that the optimizations practiced by GCC are practiced by other compilers too, because they are justified by undefined behavior in the source code.

- I could go on.

I have quoted three useful, widely deployed C pieces of software that have contained undefined behavior, and likely still contain more. Can you name one nontrivial C program, written by an expert according to your definition, that you are confident does not invoke undefined behavior?

Re: On becoming an expert C programmer

#54

   Look at some of DBell's code:

   http://www.tip.net.au/~dbell/
   http://www.isthe.com/chongo/tech/comp/calc/index.html

   Learn from it. DBell is one of the best programmers on the planet.
I have nothing against Mr DBell, whom I don't know, but... don't learn from it, please.

   Weather 1.9 - Java application to plot weather observations from the Australian Government's Bureau of Meteorology (BOM) web site.
I did look at it.

Controller.java is over 1200 lines long. Don't learn from people who create classes that big (aka God objects).

SunriseSunset.java contains nearly 100 lines of commented out code. That's another bad practice.

What's more, it consists mostly of a main method commented as "Simple main to test the class". This is a so-called poor man's unit test, it doesn't use assertions, it just prints some results out to be verified by the programmer manually. Which is another antipattern.

His FileInfo.java (part of his FileSelection 2.0) contains tautological comments like:

    //
    // Get the suffix path.
    //
    String suffixPath = fullPath.substring(prefixPath.length());
Gee, you don't say. Redundant comments, another antipattern.

isDirectoryEmpty (in the same file) happily ignores an exception:

    boolean isEmpty = true;

    try
    {
        stream = Files.newDirectoryStream(directoryPath);

        for (Path path : stream)
        {
            String name = path.getFileName().toString();

            if (!name.equals(".") && !name.equals(".."))
            {
                isEmpty = false;
                break;
            }
        }

	stream.close();
	stream = null;
    }
    catch (IOException e)
    {
    }
    finally
    {
        Utils.safeClose(stream);
    }

    return isEmpty;
Meaning that if an IOException occurs (for whatever reason), the method will tell you that the directory is empty, even if that's not true.

Setting stream to null after closing it makes no sense whatsoever (it's a local variable, it's not going to survive exiting the method).

Closing it in the try block makes no sense to me either, because Utils.safeClose is going to execute either way; that's how the finally clause works. It looks like the author assumed that finally only executes if an exception happens. No, it is run in either case.

So, what's the point of closing the stream twice? And if it does serve some purpose, now that would require leaving a comment, because it's totally unobvious. Not "getting the suffix path", which is blatantly obvious.

I could go on. This is poor quality code, I'm hardly a great programmer (working on it!), but it's not up to my production standards.

If the man is "one of the best programmers on the planet", the planet can't be Earth.

Re: On becoming an expert C programmer

#55

Earlier quoted context omitted.

> I can't understand what undefined behaviors C has, because it is the most simple and defined language I do know of. C has lots of undefined behaviors because the language was designed to be easy for compiler writers to implement. As such, a lot of decisions were left to the compiler writers, which is what "undefined" means. Here is a (probably partial) list of undefined behaviors in C. (For a full list you'd have t…

Sure, there is a lot of code you could write that results in undefined behaviour. However, none of it is code that you should write, nor is it code that an expert would write.

Then the only 'expert' is a computer, for even the best human will occasionally make undefined behavior inducing mistakes. And 'occasionally' is frequent enough to be a problem.

Re: On becoming an expert C programmer

#56
post #40

"DBell is one of the best programmers on the planet." How would one verify this? I tried a couple of his sample C programs. One of them was one source and one header file and compiled easily and quickly. A+. But then I looked at what the program did and realized I had written several iterations of the same utility myself years ago, using only the shell, sed, tr and ed or vi. I guess maybe his point of writing this in…

It would be good if you could mention precisely what programs have you tested and perhaps provide some links.

I agree with 101914. See my comment: https://news.ycombinator.com/item?id=8511826

Re: On becoming an expert C programmer

#57
post #54

Look at some of DBell's code: http://www.tip.net.au/~dbell/ http://www.isthe.com/chongo/tech/comp/calc/index.html Learn from it. DBell is one of the best programmers on the planet. I have nothing against Mr DBell, whom I don't know, but... don't learn from it, please. Weather 1.9 - Java application to plot weather observations from the Australian Government's Bureau of Meteorology (BOM) web site. I did look at it. Co…

The C sources are quite baroque as well -- inconsistent naming, lots of repetition. Things that could have been neatly done with a lookup table are coded into enormous repetitive switch statements. No goto for error handling, hence a repetition of the same error handling code before each return.

If you want to see examples of consistent and disciplined C programming, look at Git and Nginx.

Re: On becoming an expert C programmer

#58
post #54

Look at some of DBell's code: http://www.tip.net.au/~dbell/ http://www.isthe.com/chongo/tech/comp/calc/index.html Learn from it. DBell is one of the best programmers on the planet. I have nothing against Mr DBell, whom I don't know, but... don't learn from it, please. Weather 1.9 - Java application to plot weather observations from the Australian Government's Bureau of Meteorology (BOM) web site. I did look at it. Co…

[deleted]

Re: On becoming an expert C programmer

#59
post #12

There's nothing magic about learning C versus any other language. - Read an authoritative source (K&R is good; there are better ones) - Read a bunch of good code (I mostly read tools and kernel sources) - Write crappy code and get better Generally I want to write 10K lines of code in a new language before I probably don't suck at it. Varies on the language and paradigm, going to C++ from C took like five years (figur…

I'm a self-taught programmer. I started with Pascal (what seems like an age ago), then C, then VB5/6, then C# and JS and a bunch of other programming languages. At this point I felt like I could pick up about any mainstream programming language very quickly. And then I tried to tackle Haskell. That was about five years ago, and I think I'm still at beginner/intermediate level with it. But I realized something. These…

Modern C# does provide some FP constructs (in the form of LINQ, lambdas etc.). I know that having used them a lot made it easier for me to grasp Scala.

Re: On becoming an expert C programmer

#60

Not mentionned, about being an expert C programmer, is knowing the pitfalls of C, (cf. undefined behaviors), and reading and knowing the ANSI C standard. Of course, just knowing the language in and out is not enough, you also have to be a good programmer in general (algorithms, "design patterns", software architecture, software engineering, etc). But writing C code without undefined behavior, and avoiding its numerou…

"Not mentionned, about being an expert C programmer, is knowing the pitfalls of C, (cf. undefined behaviors)" I am an expert in C, I had been decades writing on it and other languages, and managing teams of coders. We created a company that used it a lot. I can't understand what undefined behaviors C has, because it is the most simple and defined language I do know of. I have lots of experience writing assembler,fort…

> But c? c is basically portable assembler.

I wouldn't call myself an expert, but I avoid calling C portable assembler. I tend to think of C as having low level data manipulation and high level flow.

Post reply on HN