Live data from Hacker News

Want to learn to code? Don't copy and paste, type out other people's code

shockoe.com

171–180 of 184 posts

Re: Want to learn to code? Don't copy and paste, type out other people's code

#171
post #72

I agree with this especially when you are doing tutorials for this reason: when you type out code, you inevitably make typos that cause bugs. This forces you to learn what some of the common possible error conditions are and forces you to learn to find and resolve them. Learning these things when you have the safety net of a tutorial is ideal, because if you can't figure it out you can always compare with the tutoria…

Can anyone else relate to this? Back in the day (early 90s), I would go down to the public library, pick out a thick book full of computer games written qBASIC, and of course I had to type them in all by hand. We didn't have internet yet, the idea of copy-and-paste from somewhere did not even exist in my mind. So I would copy these 10,000-line BASIC programs in by hand into our 386 computer, one line at a time. For s…

Sure, I can relate to that. In the early eighties computer magazines had huge listings of code which I dutifully typed in.

They never worked worked first time as I'd invariably mistyped various characters, but the subsequent bug finding taught me about how the code worked.

The bonus was you got a playable game at the end of it.

Re: Want to learn to code? Don't copy and paste, type out other people's code

#172
I too felt the same and said it here: http://www.codinggarage.com/2012/07/how-to-be-better-program...

"The right approach is to get your hands dirty, get inside the core of the code and understand it, thus implement it well. We should also try to remember the code as much as we can at the first place so if we face a similar problem later, we can solve it in no time. Also I would suggest not to copy paste it and try to type it on your own (if the code consists of a few lines), this approach will definitely help you to remember it for later use. The coding ninjas, the coding beasts, the rock start programmers, whatever you call them, all the great programmers definitely have one thing in common and that is they are like living library of the programming language and framework they use. They spend maximum time in getting things done (not finding the solutions to the problems they have already worked on before)."

Re: Want to learn to code? Don't copy and paste, type out other people's code

#173
post #69

Earlier quoted context omitted.

I'm a high school computer science teacher, and this is a WONDERFUL idea. I'm totally going to steal it and create a few assignments based on the concept.

Please do teach them the value of data representation. I don't think this is obvious but it can make the difference between having to write a mountain of code or finding a simple solution. Super-simple silly example: You have to write a program to work with four colors: Red, Green, Blue, Black. Naive representation will use strings: "red", "green", "blue", "black". A little smarter would be an enum type where red=0,…

That's very variable by language. If strings are interned they are equivalent to enums anyway for example.

Re: Want to learn to code? Don't copy and paste, type out other people's code

#174
I'm a CS major and coded all my life. The best way to learn is just to give yourself tasks, like building something specific and then try to make it. From copy and paste you just learn the syntax, which is useful but not that much. From building something from scratch you learn the principles which is far more important.

Re: Want to learn to code? Don't copy and paste, type out other people's code

#175

Earlier quoted context omitted.

Please do teach them the value of data representation. I don't think this is obvious but it can make the difference between having to write a mountain of code or finding a simple solution. Super-simple silly example: You have to write a program to work with four colors: Red, Green, Blue, Black. Naive representation will use strings: "red", "green", "blue", "black". A little smarter would be an enum type where red=0,…

That's very variable by language. If strings are interned they are equivalent to enums anyway for example.

Color.Bleu will give a compiler error. "Bleu" will not. And is "White" a valid color? No idea, need to check the docs. Try Color.White and the compiler or editor will tell you.

Re: Want to learn to code? Don't copy and paste, type out other people's code

#176

Earlier quoted context omitted.

Please do teach them the value of data representation. I don't think this is obvious but it can make the difference between having to write a mountain of code or finding a simple solution. Super-simple silly example: You have to write a program to work with four colors: Red, Green, Blue, Black. Naive representation will use strings: "red", "green", "blue", "black". A little smarter would be an enum type where red=0,…

I think it is important to learn enum's but it is a bad example. You really should use the built-in types when they're available. Color is a particular example (in both .Net and Java) where you should never re-invent the wheel. In general teaching enum's is very important and something I've not really seen schools do (although their "learn to program" programs are typically 101 to newbie, I've never seen a program th…

While I generally agree with what you are saying, it is very important to understand that built-in types are not magical. This is where coming from a low-level embedded C or assembly background can be very useful.

As an example, there are a number of sites showing the performance hit you take if you use some of the NS types in Objective-C. If I remember correctly, in some cases you are talking about NS types running 400 times slower than alternative code.

In my mind, the question of data representation could also include this very choice: Do I use an NSArray or do I do it "by hand", allocate memory and use a "simple" array?

Re: Want to learn to code? Don't copy and paste, type out other people's code

#177

Earlier quoted context omitted.

Please do teach them the value of data representation. I don't think this is obvious but it can make the difference between having to write a mountain of code or finding a simple solution. Super-simple silly example: You have to write a program to work with four colors: Red, Green, Blue, Black. Naive representation will use strings: "red", "green", "blue", "black". A little smarter would be an enum type where red=0,…

COuld you elaborate a bit on what the benefit of the other representations in your example. I don't get it.

OK, I pulled colors as an example out of thin air but I'll see if I can make it work.

Let's say you have to write a routine that does something based on a color as the input. You have a few choices in terms of how to represent the colors:

    - The name of the color in a string
    - A typdef enum for your colors (integers)
    - A color-per-bit scheme (U8, U16, U32)
    - Channel-per-bit scheme (U8)
    - 4 or 6 bit packed RGB values (U16 or U32)
    - 8 bit packed RGB values (U32)
    - 8 bit unpacked RGB values (struct of three U8)
    - 16 or 32 bit unpacked RGB values (struct of three U16 or U32)
    - Unpacked RGB floats (struct of three floats)
    - and more...
I won't go into the implications of each of the above. Some of it is highly dependent on both the system and the objectives of the work being done.

Say, for example, that you choose to use the names of colors stored in strings as your color representation. Now you have to compare strings in order to identify the colors:

    if(strcmp(input_color, "red") == 0)
    {
      // Do something with red
    }
    else if (strcmp(input_color, "green") == 0)
    {
      // Do something with green
    }
    else if (strcmp(input_color, "blue") == 0)
    {
      // Do something with blue
    }
    ... etc
Regardless of language the strings need to be compared character by character. Even if a language or OO framework allows you to say something like if(string1 == string2) you have to keep in mind that what is going on behind the scenes is pretty much exactly what strcmp() has to do. Which means that the above is, at the very least, slow.

And, of course, it isn't very portable. What happens if the input has to be in German or Japanese?

The typdef enum representation gives you the ability to use a far more efficient construct to identify your colors:

    switch(color)
    {
      case COLOR_RED:
        // Do something with red
        break;
      case COLOR_GREEN:
        // Do something with green
        break;
      case COLOR_BLUE:
        // Do something with blue
        break;
    ... etc
This is much, much faster. It is, at the core, an if/else-if structure that is only comparing integers, which is a single machine language instruction. Fast and clean and language-portable by means of the proper text-to-integer function somewhere to deal with different languages.

If you are on an embedded system that can do bit testing in machine language it might make sense to encode one color per bit or one color channel per bit. for example, in some embedded C dialects you might be able to do something like this:

    if(color.0) // Select and test bit 0
    {
      // This is red
    }
    else if(color.1) // Select and test bit 1
    {
      // This is green
    }
    ... etc
At this level the advantages of doing this are tightly linked to the platform and the goals of the application.

If, for example, one needs to be able to expand the available range of color inputs beyond what can be described with simple words a discrete RGB representation might be the best choice. This is also the case if you wanted to future-proof the program and be ready for when more colors arrive.

Here you have several choices, two of which are to represent each channel with an 8 bit value or choose floats instead.

The 8 bit values can be packed nicely into a U32, making it very efficient. You could also create a struct to facilitate access to the components and let the compiler optimize for you.

The float example is interesting because the conversion from float to whatever (if necessary) can be of any bit width. So, for example, if the color needs to ultimately be mapped to an 8-bit-per-channel display device you can translate from float to 8 bits on output. All of your intermediate math and color manipulation would be done in full-resolution floats which means that you are not going to accumulate errors. This, for example, is important if you are applying FIR filters to calculate missing color sample data from certain video data formats.

Packing has its issues as well. If you are dealing with little-endian vs. big-endian systems there might be overhead associated with unpacking and possibly rearranging a packed RGB value. If you are dealing with processing colors at a massive scale this can have performance and even power consumption implications.

I may have been lucky in that my very first CS professor was hell-bent to teach the importance of thinking deeply about data representation BEFORE thinking about code. He'd repeat this mantra 'till you were sick from hearing it. Years later I'd learn to appreciate this bit of wisdom in more ways than one.

Re: Want to learn to code? Don't copy and paste, type out other people's code

#178

Earlier quoted context omitted.

COuld you elaborate a bit on what the benefit of the other representations in your example. I don't get it.

OK, I pulled colors as an example out of thin air but I'll see if I can make it work. Let's say you have to write a routine that does something based on a color as the input. You have a few choices in terms of how to represent the colors: - The name of the color in a string - A typdef enum for your colors (integers) - A color-per-bit scheme (U8, U16, U32) - Channel-per-bit scheme (U8) - 4 or 6 bit packed RGB values (…

Can I just say that THIS is why I love Hacker News. People are serious about sharing their ideas on a deep level.

Thanks for sharing!

Re: Want to learn to code? Don't copy and paste, type out other people's code

#179
Now that there's 171 comments on this thread, I hope it won't bother anyone for me to ask that at a few of you help us out by Beta testing a game we're releasing.

letterlasso.com signup with your email and we'll send you a testflight email in a couple weeks. Anyone who happens to see this and is willing to help us, it would really mean a lot!

Post reply on HN