{"id":12271,"date":"2011-07-11T06:45:05","date_gmt":"2011-07-11T11:45:05","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=12271"},"modified":"2011-07-11T07:42:00","modified_gmt":"2011-07-11T12:42:00","slug":"learning-to-program-vs-learning-to-write-software","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=12271","title":{"rendered":"Learning to Program vs. Learning to Write Software"},"content":{"rendered":"<p>There&#8217;s a book called <em>Teach Yourself C++ in 21 Days<\/em>, which can be read for free <a href=\"http:\/\/newdata.box.sk\/bx\/c\/\">online<\/a> if you have the inclination.  When people have come to me asking for suggestions about where to begin with C++, that&#8217;s usually where I send them.  Looking back, I&#8217;m not sure that was the best move. Although, I don&#8217;t really know where else to send them.  College maybe? That&#8217;s an excruciatingly expensive and time-consuming way to learn, and probably not a good choice for the curious person looking to test the waters.   Even that might be a bad idea.  A lot of people seem to read books and take courses and emerge on the other side having no idea how to <em>make useful software<\/em>.<\/p>\n<p>Imagine a book titled, &#8220;Become a novelist in 21 days.&#8221;  There&#8217;s also a college course that purports to teach you novel-writing skills.  Inside, you will be instructed in grammar, spelling, punctuation, proper capitalization, and page formatting.  Afterward, you march out into the wide world calling yourself a &#8220;novelist&#8221;.  I mean, that&#8217;s what writing a novel is, right?  Putting words together? And you totally know how to do that.  You got an A and everything. <\/p>\n<p>I&#8217;ll admit I&#8217;ve never seen the inside of a university as a student, and I&#8217;ll freely admit Ive got shameful gaps in my knowledge as a self-taught coder.  But the way C++ is taught seems to have the most alarming omissions. For example, I know people supposedly educated in C\/C++ who have never had to:<\/p>\n<p><!--more--><\/p>\n<ol>\n<li>Create header files.\n<\/li>\n<li>Use external libraries (outside of the default libraries) in their own project.\n<\/li>\n<li>Deal with or consider cross-platform issues.\n<\/li>\n<li>Do anything, anything at all, with a windowed interface.\n<\/li>\n<li>Use profiling tools.\n<\/li>\n<li>Use source code control.\n<\/li>\n<\/ol>\n<p>I&#8217;m sure these things <em>are<\/em> taught, somewhere. (People seem to know them, at any rate.) But they aren&#8217;t mentioned or covered in any of the books I&#8217;ve come across or inside of the heads of any second-year university students I&#8217;ve known.  Books seem to begin and end with, &#8220;Here is the syntax of the language.  Got it? Good job.  Off you go now.&#8221; <\/p>\n<p>I think just about everyone understands that programs come from source code. A programmer types some stuff, compiles it, and gets a program.  <\/p>\n<pre lang=\"c\">\/\/The classic \"hello world\" program in C++.\r\n\r\n#include <iostream>\r\nusing namespace std;\r\n\r\nint main ()\r\n{\r\n  cout < < \"Hello World!\";\r\n  return 0;\r\n}\r\n<\/pre>\n<p><\/iostream><\/p>\n<div class=\"dmnotes\">Aside to other programmers: I really think the \"hello world\" program for C++ is misguided.  The point of the simplest program is to make something so elemental that a complete newcomer can understand every part of it.  But here, the conversation is likely to go: <\/p>\n<p>Student: Hey, what's this 'cout' thing?\"<\/p>\n<p>Teacher: Oh, that's an object.  We'll cover that later in the course.<\/p>\n<p>Student: What about 'using namespace std'?  Is this related to sexually transmitted diseases? <\/p>\n<p>Teacher: Hopefully not. I'll cover namespaces after we do objects.<\/p>\n<p>Student: Okay then.  What about those two less-than signs?<\/p>\n<p>Teacher: Er. <em>Normally<\/em> two less than signs is a bitwise shift, but in this special case it's used to put items in a stream of the 'cout' object.<\/p>\n<p>Student: So, in this entire example program we have something that's too advanced to explain to me, a symbol that is a unique exception to a rule I don't yet understand, and the words \"Hello World\"?<\/p>\n<p>Teacher: Also, #include is usually a header file followed by a .h, but iostream is a special case. <\/p>\n<p>In my mind, the old <a href=\"http:\/\/groups.engin.umd.umich.edu\/CIS\/course.des\/cis400\/c\/hworld.html\">C Hello World<\/a> program is a far better place to begin learning, and I get the feeling it's not used simply because it doesn't have any C++ specific code in it. As if that was more important than clarity and comprehension to a first-day student. <\/div>\n<p>All C programs begin with main (), a function. If you're doing more than printing \"Hello World\", you'll likely need to add some other functions rather than cramming the entire program into main ().  So I'm writing this program to create killer robots and wipe out humanity:<\/p>\n<pre lang=\"c\">\/\/A small project I'm working on because I HATE THE WORLD!!!!!!!\r\n\r\nvoid KillAllHumans ()\r\n{\r\n  \/\/To do:  Add my human-killing code here.\r\n\r\n\r\n}\r\n\r\nint main ()\r\n{\r\n  KillAllHumans ();\r\n  return 0;\r\n}<\/pre>\n<p>Eventually I'll have code for rounding up members of the resistance, tricking them into revealing the location of their base, identifying objects that are being used as makeshift weapons against my deadly robot army, and so forth. Eventually I'll have more code than can reasonably fit in a single file.  It will be time to break the program into multiple files.  Maybe I'll add a new file, called \"Locate.cpp\". (the .cpp stands for 'C++', but it's just a text file like all source code.)  In Locate.cpp I'll put all the code related to finding humans and differentiating them.  Then in Exterminate.cpp I'll have the code that drives my army of merciless killbots. <\/p>\n<p>Another coder might have a different way of breaking up the project.  Maybe one file for locating, another file for classifying, another file for prioritizing targets, another file for killing.  My files will be larger.  The other coder's files will be smaller, but there will be more of them and more total lines of code.  Where is the balance?  What is the best way to break things up so that they make sense, are easy to maintain, avoid overly-convoluted program structure, reuse code where applicable, and still get the job done?<\/p>\n<p>And here is where the \"software engineering\" bit kicks in. This is where you move from \"learning grammar\" to \"writing a novel\".  This is the stuff that some people are never taught. I say this because I so rarely hear people discussing it.  I've seen endlessly pedantic arguments over code formatting, variable naming, and other stylistic choices that, in the long run, don't make a lick of difference to the end user, the compiler, or even other programmers.  But I hardly ever hear anyone discussing the theory and practice of designing a software project like this.  Some people seem to assume that once you can write syntactically correct code, you're a programmer.   You can find evidence of this dangerous line of thinking as you go out into the world looking to use code written by other people.  Some code will be neat, clear, organized, and have an interface only as complicated as it needs to be.  <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/holly_hop_drive.jpg' class='insetimage'   alt='holly_hop_drive.jpg' title='holly_hop_drive.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Your code should allow another programmer to come in and begin using it without needing to read and understand everything you've written.  If you write a library to (say) load 3DS files, I shouldn't need to know about how the files themselves are constructed or about how you organized your code. I shouldn't have to make choices about options I don't understand and I shouldn't have to deal with features I don't need.  <\/p>\n<p>Here, let me give you a concrete example.  Let's say you're writing some program for Microsoft Windows.  You want to create a window for the user.  Here is how to do that using the Microsoft Windows API:<\/p>\n<pre lang=\"c\">\r\n    \/\/For clarity and fairness, all error checking has been removed. \r\n    WNDCLASSEX wc;\r\n    HWND hwnd;\r\n    MSG Msg;\r\n\r\n    \/\/Step 1: Registering the Window Class\r\n    wc.cbSize        = sizeof(WNDCLASSEX);\r\n    wc.style         = 0;\r\n    wc.lpfnWndProc   = WndProc;\r\n    wc.cbClsExtra    = 0;\r\n    wc.cbWndExtra    = 0;\r\n    wc.hInstance     = hInstance;\r\n    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);\r\n    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);\r\n    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);\r\n    wc.lpszMenuName  = NULL;\r\n    wc.lpszClassName = g_szClassName;\r\n    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);\r\n\r\n    RegisterClassEx(&wc);\r\n    \/\/ Step 2: Creating the Window\r\n    hwnd = CreateWindowEx(\r\n        WS_EX_CLIENTEDGE,\r\n        g_szClassName,\r\n        \"Sample Window\",\r\n        WS_OVERLAPPEDWINDOW,\r\n        CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,\r\n        NULL, NULL, hInstance, NULL);\r\n    ShowWindow(hwnd, nCmdShow);\r\n<\/pre>\n<p>Now here is the same code if you're using SDL:<\/p>\n<pre lang=\"c\">screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);\r\nSDL_WM_SetCaption(\"Simple Window\", \"Simple Window\");<\/pre>\n<p>Now, someone might defend the windows code by pointing out that there are a lot more controls there. Basically, there are more knobs and dials and switches for you to use.  But this front-loads all of that complexity so that I have to flip all those switches and turn all of those dials before I can do something simple. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/reactor_controls.jpg' class='insetimage'   alt='reactor_controls.jpg' title='reactor_controls.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Note the part where it says \"CW_USEDEFAULT\".  That's where I specify where the window will appear on the desktop.  The vast majority of windows simply accept the system default. (Try opening up a bunch of windows at once. Notepad, minesweeper, whatever.  Note how each one begins down and to the right of the previous one, cascading down the screen.  That's what the default value does.) Some windows are designed to appear minimized, and so this option doesn't apply.  Other windows appear maximized, so this option doesn't apply.  But whether or not you care or regardless if these options have any meaning for you, you must always specify that you don't care.  It would be like a car that you couldn't start until you decided if you wanted to use the radio or not.  Even if you want the radio off, you have to explicitly say so (and the windshield wipers, rear defog, headlights, heater, air conditioning, and dome light)  before you can turn the engine over. This exposed complexity is bad design.  (In defense of the Windows API, it's friggin' <em>old<\/em> and was created while the windowed interface was still evolving.)  A better design would let you do the straightforward, obvious thing in just a couple of lines of code, and then give you additional features to do things outside of the norm.  <\/p>\n<p>So a more honest answer to the question of \"Where should I go to learn programming?\" is \"I don't know\".  I did it for a couple of years before these concepts dawned on me, and even longer before I began to form a framework in my mind of how things should work and how this code would appear to another coder. And it wasn't until I began using code from other people that I began to understand how important this was, and that it was more critical than just about anything in any of the books I'd read. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s a book called Teach Yourself C++ in 21 Days, which can be read for free online if you have the inclination. When people have come to me asking for suggestions about where to begin with C++, that&#8217;s usually where I send them. Looking back, I&#8217;m not sure that was the best move. Although, I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-12271","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/12271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12271"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/12271\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}