Misled from the Start

Friday August 3rdBeing in the Industry, Software Development, WTF Category

I find myself frequently concerned with the lack of ability of a lot of people in our industry. Today I found an example of at least one of the possible reasons why software development professionals turn out to be crap.

They start by reading tutorials like this.

How can we expect people to become good at what they do if the tutorials they’re learning from are like that? If you learn the bad stuff from the outset then you’re destined to be writing bad code for many years to come.

The tutorial above has the following flaws:

The compiler takes the code you’ll write later on and translates it into binary code which the computer can understand and execute.

Actually, no it doesn’t. The compiler translates your source files from one language to another. In this case, the C compiler translates C source code into object files which are relocatable blocks of machine code. At this point none of your code is executable. When the source files are compiled to object files, the linker is then invoked to link the object files into a working executable or library. For more information on this process have a read of this, as it’s much more thorough, and (more importantly) it’s correct.

If you have a compiler without a built in editor you can just use any text editor.

Compilers do not have built-in text editors. Compilers have the job of translation. An IDE generally comes with some kind of source code editor. It also makes it easy to invoke the compiler and linker when your source has been modified. Do not get confused between a compiler, a text editor and an IDE.

Every C program must have a function called “main”.

No. Every C program that is to be compiled to an executable must have an entry point. In console C programs, the entry point is generally called main, but this is configurable with most compilers/linkers/IDEs. With GUI applications, such as Win32, the default entry point can be different (eg. WinMain). If your C code is going to form a library (lib, o, dll, etc) the rules are different.

A function is just a container that holds a series of commands that tells your computer what do to.

This isn’t totally wrong, but it’s not really right either. Functions are blocks of callable code which perform a specific task inside your program.

The next line is your main function.

Actually, this line is the function’s signature and the beginning of the function’s definition. The function body is everything between the { and } that follows the function’s signature.

The int in this line can either be left or or changed to another basic datatype.

This is very, very wrong! First of all, ‘int’ specifies the type of the function’s return value.

Secondly, the entry point is the first part of your program that is executed, and the operating system expects the result of that function to be meaningful. Passing anything other than an integer back to the operating system is completely wrong. Meaningful return codes should be used instead. Do not use anything other than int for your main function’s return type.

The final line, return 1; Tells the compiler that the program is finished and returns an integer.

The last line does not return an integer. The last line specifies that the value of ‘1′ should be returned to the caller. This statement would be perfectly legal for other return types such as char. The value that you return must be of the same type that you’ve specified as your return type in your function signature.

Not just that, but do not use magic numbers. You should use constants that give the code meaning. If you’re returning a value to the operating system, you need to understand what that number means. In most cases, the value of 1 indicates that an error has occurred in your program. This means that if your program is called as part of a greater set of commands, the likelihood of the process failing increases because the caller will believe that your program has failed. This return statement not only contains a magic number, but it contains the wrong magic number. If the developer had used the predefined constants that are part of stdlib.h then not only would he have avoided this problem, but his code would also be more portable across platforms and operating systems.

printf(”string”); is used to output a string onto the console, you can substitute any string into it (as well as variables).

Do not use variables as the first parameter to any printf()-like function. There’s no excuse for it. It’s lazy, and it exposes a security hole in your program, namely format string attacks.

{ and } are used to encapsulate logical blocks of code (such as functions). All of the code inside these braces are grouped together.

Curley braces define scope. It’s as simple as that. They don’t “group” code together.

The Point

Too many people are writing misleading or meaningless tutorials which don’t aid in helping people write proper code. They crank out examples without understanding what they’re teaching. They don’t think about the long term effect on those learning. The result? An industry full of plebs who don’t really know how to do their job properly.

In case you didn’t notice, it shits me up the wall :)

So, for the sake of clarity, this is what the example should have looked like:[source:c]#include /* contains EXIT_SUCCESS */
#include /* contains printf() */

/* main() is the program’s entry point */
int main()
{
/* write some text to the screen */
printf( “Hello, World!\n” );

/* indicate successful completion to the
operating sytem. On most platforms this
value is 0. */
return EXIT_SUCCESS;
}[/source]

15 Comments

  1. Vault-Co
    August 3, 2007

    My question is : where is the market incentive for people to train with great dedication for many years to become master coders, when it is so easy to make a good living just by faking it?

    If that is how the market system works in reality nowadays, then isn’t a boffin (say, like me for example) really just a born loser for wasting that much time trying to hone his/her craft? Why bother, when you can make twice as much money by just littering your speech with an occasional cliched phrase like “enterprise” or “agile” or “best practice?”

    Then instead of spending your weekends for years holed up in your study meticulously crafting code, you can go do what you really want in life - ride jetskis and have sex with strange women, watch spectator sports and drink beer and laugh and all that crap.

    (A reasonable line of inquiry. Just being Devil’s Advocate)

  2. OJ
    August 3, 2007

    There is no market incentive until a company goes bust maintaining shit code. But by then it’s too late because the experience dies with the company. This is why we need to nip it in the bud and at least do what we can to point out the flaws to aid in increasing the general level of quality.

  3. K.
    August 3, 2007

    Attacking another website isn’t the way to do it. While their article isn’t the best it does help a beginner start to code. While it doesn’t necessarily explain the details of everything a beginner doesn’t need it.

    “Not just that, but do not use magic numbers. You should use constants that give the code meaning.” 1 has historically been returned because it’s used as “true” or “on” in binary senses. If you had ever taken a class in Computer Science you would have known that. Returning “EXIT_SUCCESS” returns 1. While I agree that the author should have written that it’s totally unnecessary and would just confuse a beginner.

  4. OJ
    August 3, 2007

    I wouldn’t say that I attacked the other website. I’m merely highlighting that there are quite a few flaws in what was said, and that for a tutorial aimed at beginners they should be making sure what they’re saying is true.

    I have done a computer science source and I am well aware of the meaning of 1 and 0 in the sense of true and false. But that’s beside the point when the return value of the function is not intended to be true/false.

    In VC++ EXIT_SUCCESS is defined as 0.
    On my Gentoo box with GCC, EXIT_SUCCESS is defined as 0.
    On my Ubuntu box with GCC, EXIT_SUCCESS is defined as 0.

    In all three cases, EXIT_FAILURE is defined as 1. So I’m afraid you’re wrong in that regard.

    Aside from that it makes a lot more sense to have errors being non-zero and success being zero in the case of returning values to the OS. There are many types of failure, and only one type of success. Having 0 indicating success caters for simple checking of success while still be able to get access to the exact reason for the failure.

    Part of the reason I find this frustrating is because links like that which end up on dzone spread like wildfire and the misleading information is spread across the planet very quickly. It’s not good for those looking to learn.

    I appreciate your comment.

  5. Vault-Co
    August 3, 2007

    There is no market incentive until a company goes bust maintaining shit code. But by then it’s too late because the experience dies with the company. This is why we need to nip it in the bud and at least do what we can to point out the flaws to aid in increasing the general level of quality.

    … and over the years, I don’t know how many outfits I have seen go bust in New York, Los Angeles, Texas, Sydney and Brisbane for this very reason.

    In every single one of these organizations, on the last day before the repossession agents arrive and begin to strip off the furnishings, you’ll always find a sort of nerdy, boffinish type like me wheedling away in the morning meetings … “We’ve got to clean up this code! It’s unmaintainable junk. Our processes stink. The entire company is just a one-off disaster where nobody has thought ahead more than the end of the business working day. Seriously, you need to listen to me. I’m no social engineer but I know what I’m talking about.”

    There’s always a guy in that company with sunglasses on and a bikini babe under each arm going “You talk like a fag! I don’t know what all that fancy schmanzy talk is even supposed to mean. Listen son, I spilled some coffee in the kitchen, why don’t you get yourself some rubber gloves and an apron and go in there and see if you can get it up. Make yourself useful instead of standing around bringing down company morale.”

    The next day, this guy is seen in the parking lot with the bikini babes packing suitcases stuffed with money into the trunk of his car. Meanwhile your basic melvinish types like myself continue whittling away in their cubicles trying to introduce good practice, even as we can see the demolition crews bringing up the crane outside our windows.

    So again … what is the market incentive for writing good code and learning real excellence in your craft, when the company sociopaths can make three times what you do by talking gibberish and making no semantic sense at all, much less developing any real technical prowess?

  6. Alex
    August 3, 2007

    While I’m not a C developer, I am an Actionscript developer, and many of the same rules apply. If you rip an article for showing some basics to people who presumably have little to no coding experience (since it is a tutorial on how to write your first program) it serves its purpose well. You lay the foundation for more knowledge to be built on.

    Dropping all sorts of concepts that aren’t needed in the initial stages of learning how to develop will only serve to confuse and discourage new developers.

    Typically the people who will actually make a decent living as a developer are not going to be learning their skills from tutorials. Chances are they’ll be going to school, or they are a developer who uses another language and teaches themself the syntax of another.

  7. OJ
    August 4, 2007

    Thanks for your comment Alex.

    You raise an interesting point. One that I feel needs to be discussed further.

    Before I cover that I’d just like to go over your other point in a bit more detail. In my time as a Dev and a Teacher, never once have I felt the need to scrimp on the small details from the start. Not only is it not confusing, it aids in creating a better foundation that a solid developer can build on. Learning ‘EXIT_SUCCESS’ instead of ‘0′ not only makes more sense to someone who hasn’t written code before, but it makes it easier for them to read when they come back to it. The question here is, do they need to know what ‘EXIT_SUCCESS’ actually represents? In their first program, not they don’t. It’s a detail they can learn later on, but the important thing is that they’re already doing the right thing. Tell me if you think this sounds any more or less confusing than what the original article states:

    To indicate that the program has completed successfully, we type return EXIT_SUCCESS; as this informs the operating system that there weren’t any errors.

    Personally, I think this is a lot easier to understand, as it contains contextual information that ‘return 0;’ doesn’t.

    Most of the people that I’ve spent teaching have never had problems learning this stuff from the beginning, and they prove to work better and keep their code cleaner as a result of being shown the proper methods/rules from the start.

    So my point really is that I feel that those who skip over details they don’t consider to be important actually end up confusing the student more than if they’d actually done things properly.

    I agree completely with your comments about dropping concepts in the intial stages. It’s totally unnecessary, and increases confusion.

    Now, to the more interesting point :) To quote you:

    Typically the people who will actually make a decent living as a developer are not going to be learning their skills from tutorials. Chances are they’ll be going to school, or they are a developer who uses another language and teaches themself the syntax of another.

    I wish you were right, but my experience says otherwise. I have worked, and so still work, with many developers who do not have formal qualifications in development. They didn’t learn from someone who knew what they were doing. They learned from online tutorials, books and forums.

    Off the top of my head I can easily count another 5 developers I have worked with who have got college or university degrees, but still learned most of their development skills via Google.

    Then there’s those who cross industries. Designers who start writing code, coders who start doing design, etc. These people may or may not have a background in development. But if they do, chances are they’ll believe that they know enough about general development to get by through reading a few online tutorials.

    So as you can see, there are still quite a few ways for badly-written tutorials to find their way into production code. This is just as big a concern for those of us who have to share and maintain the code. It’s the same as having to work with someone who has learned to write bad code from the start.

    The issue is very real. The development world is full of people who think they are doing things properly. It results in lots of bad code that’s a bitch to maintain that eventually costs the companies a lot of money.

    So to sum up. I think that both new and experienced developers are susceptable to bad tutorials when they don’t necessarily have a grounding in the language or technology. We need to make sure what we write in our tutorials not only compiles and runs, but actually provides enough information to the reader about what best practice for that language/technology is.

    Thanks again for your comment Alex :)

  8. Vault-Co
    August 4, 2007

    The incentive for good form or an appreciation thereof? Where’s the babes and money in it?

  9. OJ
    August 4, 2007

    Money will come long term when the code and company survives to make a profit without drowning in the debt of maintenance.

    “Babes” are insignificant. You live and work to what suits you. As long as I feel I’m making a good effort to improve the quality of software wherever I find it while being a generally good and ethical person at work, then I’m happy!

  10. Dan
    August 4, 2007

    I too am an Actionscriptr, and straddle the very world of coders who start off at anything but, and have had long discussions with OJ in the past and present about what im doing wrong/right with my code.

    I think there are two points here (but im sure there are more). 1. If the information in the tutorial is correct. and 2, if the tutorial is written in a way that is going to be of benefit to the reader.

    Im not in a position to answer 1, except to say that at times, ive been guilty of dumbing down examples I give to students (I teach HTML/CSS/AS to design students at UTS) - but I try wherever possible to at least get the fundamentals right. And 2, the stuff I teach is for designers, and not application developers - so I feel that I’m able to tailor the tutorial to the students.

    But there is a HUGE difference between teaching someone how to iterate through a list of values to maximise code reuse and teaching someone fundamentals about computer science.

    IMHO, this is stuff that unless you are supremely naturally gifted in, you need some sort of top notch tuition in. If I only knew half the stuff that you guys know from your comp science degrees, some of my hacks from the internet would make sooo much more sense….

    Back to the main point. If I am teaching something, I feel that I bloody better well teach it right, and if I am gonna water down the tutorial, I try not to leave anything out - I just explain it later.

  11. OJ
    August 5, 2007

    Great feedback Dan.

    I failed to make an important point in my post and follow-up comments. That is, there’s nothing wrong with dumbing-down a tutorial or lesson to make it easier for students to understand. I am all for it if it’s going to help make things clear. But if it misleads them in any way, is downright wrong or implies any form of bad practice then I think that another tact should be taken.

    I’m with you Dan. If I’m going to teach, I’m going to teach right, or not at all! If I’m not sure about something, then I will either avoid the topic altogether, or I’ll make sure that the students know that I’m unsure. Generally I will research what I’m teaching quite thoroughly before the lessons.

    There is a huge difference between teaching basics such as iteration and the fundamentals of computer science - but the principle remains the same. Do not teach if your teaching bad or incorrect stuff!

    Cheers for the comment.

  12. Vorlath
    August 26, 2007

    The problem is that most of what you call misleading isn’t. Like every program needs a main function. How is that misleading? Can they not get working programs by using main? And about what the compiler does, the tutorial was actually correct. You are the one in error. The code most definitely IS executable while the object file is not. I’ve written enough tools for debugging format conversion (especially to CodeView) to know. About what a function does, you said the exact same thing as the tutorial using equivalent terms. I could go on like the scope thing or the printf thing.

    You got more things wrong than the tutorial did. I’m surprised you didn’t take your own advise and “Do not teach if your teaching bad or incorrect stuff!”, or in this case not say anything about the teaching if you’re incorrect.

    BTW, I like the expression “shits me up the wall”, but I find it ironic that you’d be the one using it in this case.

  13. OJ
    August 26, 2007

    You’re STILL going on about this?

    You got more things wrong than the tutorial did.

    *sigh* Yup. Whatever.

  14. Adam
    August 28, 2007

    Just so you know. The link for the tutorial has changed (mezzoscorner no longer exists). http://easyctutorials.com/your-first-c-program/” is where the tutorial moved to.

  15. OJ
    August 28, 2007

    Thanks for the updated link, and the updated content.

Leave a comment

Size

Colors