Discover the best of the web!
Learn more about Digg by taking the tour.
A Mental Trick for Programmers & Coders
withoutane.com — Whenever you are confronted with some code that you are trying to understand, imagine yourself re-typing it by hand. This is a neat mental trick you can use to make sure you're really understand the code you're looking at, and not just glancing over it.
- 1379 diggs
- digg it
- iamhumble, on 10/12/2007, -3/+14Never thought of doing that. Sounds obvious. When you type it out I think you trick your brain to understand it. It is kind of like the saying that you really know about something when you teach it to someone else. Good tip. I have to try this out next week.
- hackwrench, on 10/12/2007, -17/+8Why think of it in terms of "tricking" your brain? Your brain is a neural net. You are just sending data through that neural net for processing.
- killinger777, on 10/12/2007, -17/+3Get this:
http://www.wholetomato.com/ - ISVDamocles, on 10/12/2007, -12/+3@killinger777: You're kidding me that Visual Studio is that backwards! Even vim has a useful color-coding scheme, and it's a command-line text editor.
Something that simple is not worth $150! (I see the other features, but they're only for monkey-coders that barely comprehend include files.) - EXreaction, on 10/12/2007, -11/+2Go ahead and imagine that you are typing it...that won't help you figure out what it does.
I will continue to imagine what the program is doing working backwards with outputted variables...sounds complex, but at least you can find how X variable got to be what it is. - ricree, on 10/12/2007, -1/+10@ISVDamocles
Actually, as far as I can see, most of those look to be things that visual studio already does. Perhaps they do it a bit better, but it hardly looks to be $150 better. Maybe they are just really bad at marketting their product, but it seems kind of lame to me. - Splizxer, on 10/12/2007, -0/+1I do that quite a bit sometimes too, I think it's something to do with hand eye coordination, I know that sometimes when I'm typing without looking, I can tell if I miss typed it half way through the word even if I don't see what I'm typing.
- alex4u2nv, on 10/12/2007, -0/+0But don't do it too often (especially if it's poor code quality), because you might subconsciously taint your coding standards.
Personally, I take the stance of critiquing the code and at the same time understanding it. Takes a bit longer, but I pick up some good tips and denounce the poor codes if there are any. - Jonty, on 10/12/2007, -0/+1It may just be that you're slowing yourself down enough to fully understand what's going on rather than just skimming.
- trib4lmaniac, on 10/12/2007, -2/+17So is anyone going to say what the difference between semi-colons and periods is?
- technoboy, on 10/12/2007, -20/+2Depends on the language really, semi-colons are usually to seperate commands, while periods are used inside commands. Its quite easy to get the both mixed up if the source code is not formatted properly.
- Whoblah, on 10/12/2007, -5/+11It seems to be that when you define functions, you separate different cases by semicolons. And when you want to signal the end of a function definition, you put a period.
- Louis11, on 10/12/2007, -6/+14Wouldn't it really depend on the language?
- Eliasar, on 10/12/2007, -16/+4@whoblah
No? Semi-colons are used at the ends of "sentences" while periods define subclasses and subsequent functions of a different class.
Of course, this is only in C, C++, and Java AFAIK. Most scripting languages use their own set of rules, and there really is no universal language. - Nougat, on 10/12/2007, -3/+77A semicolon is what you have left after suffering from colon cancer. Only women have periods. Both are biological conditions, but they're really quite different. I can't believe you asked such a stupid question.
- FrankieB078, on 10/12/2007, -12/+2Correct me if I'm wrong (I am still learning) but a period in PHP is used to concatenate two strings, or group them together. The semi-colon in PHP means to end a line of code.
- hackwrench, on 10/12/2007, -11/+1I'm used to a period indicating that what follows is an element of a type. Ex: (using a basic dialect)
TYPE Coord3d
X AS INTEGER
Y AS INTEGER
Z AS INTEGER
END TYPE
DIM Player AS Coord3d
Player.X = 1
Player.Y = 2
Player.Z = 3 - Whoblah, on 10/12/2007, -1/+11@Eliasar
I'm very aware of how semicolons are used at the end of statements in C, but I assumed trib4lmaniac was asking what the differences were between the semicolons and periods in the Erlang example. - molotovbliss, on 10/12/2007, -0/+2" Also notice that you have to tell it you are done entering code by finishing with a full stop "." "
...
"says that the factorial of 1 is 1. Note that we end this part with a ";" which indicates that there is more of this function to come."
...
"says that the factorial of N is N multiplied by the factorial of N - 1. Note that this part ends with a "." saying that there are no more parts of this function. "
I don't know ERLang but poking around the docs here's what I've found. Hope it helps.
http://www.erlang.org/doc/doc-5.5.2/doc/getting_started/seq_prog.html#2.2 - synae, on 10/12/2007, -1/+3omfg erlang is such a crazy language. Coming from a C/Java/Python background and never touching Prolog in my life, Erlang was a ***** trying to learn it. It has really cool built-in language features (concurrency, clustering capabilities, pattern-maching, guard statements, etc.) but trying to learn it was a bit too much for me.
As far as semicolons vs. periods vs. commas, my understanding is that a period = end of the block. semicolon = end of this portion block (statement), followed by another statement. commas separate parts of statements.
this is by no means definitive... - WomunOfColour, on 10/12/2007, -1/+2Using that trick, it seems like the semicolon is used as an 'elseif'. The semicolons seem to separate each if statement, or each type of case.
- Nougat, on 10/12/2007, -24/+12bury me
- fyre2012, on 10/12/2007, -2/+30How deep?
- monkeychewtoy, on 10/12/2007, -11/+5Screw you. I'm digging your fool ass up.
- Alystair, on 10/12/2007, -0/+1I think there should be a standardized amount that Digger's bury people who ACCIDENTLY put their text someplace, maybe -6 feet deep? :)
- BigBrother87, on 10/12/2007, -0/+1I'll bet a story worth digging doesn't fit entirely in the description.
- molotovbliss, on 10/12/2007, -0/+5Just use an abundance of plain English comments in your code, makes your life as well as others easier. Especially when it comes time to pick it back up again. Although this sounds like a good idea for those lazy coders who don't use comments and you have to decipher it.
- mtg101, on 10/12/2007, -1/+9Worse are lazy coders who DO put comments in. Then don't update them So the comment that says "takes ownership of memory" actually means "well it used to take ownership of the memory, but I changed it, but couldn't be bothered updating the comment, so when you delete it again... well who knows!!!".
Same for lazy use for Hungarian notation: ohh... so bError is a string now is it??? - molotovbliss, on 10/12/2007, -0/+4Not updating comments almost defeats the purpose of even having them, still the same, lazy coders! :)
- sudonim, on 10/12/2007, -1/+4I prefer to use comments in Lojban, English just doesn't do it for me.
- mtg101, on 10/12/2007, -1/+9Worse are lazy coders who DO put comments in. Then don't update them So the comment that says "takes ownership of memory" actually means "well it used to take ownership of the memory, but I changed it, but couldn't be bothered updating the comment, so when you delete it again... well who knows!!!".
- Snipex, on 10/12/2007, -4/+2http://duggmirror.com/programming/A_Mental_Trick_for_Programmers_Coders/
- chrisntr, on 10/12/2007, -0/+6I always like writing out code by hand. Any one can just copy and paste code and get it to work but it seems to register in the brain when I type it out.
- hackwrench, on 10/12/2007, -3/+1It would be nice if there was a program that accepted a file and displayed it, and allowed you to type as if you were just typing in the file and marked errors, that you could backspace to correct. Does anyone know of such a program?
- NiLeS, on 10/12/2007, -1/+2the Insert key?
- bergur1, on 10/12/2007, -0/+3Isn't it the same concept with almost everything? To go through everything?
- baalzebub, on 10/12/2007, -1/+6syntax highlighting helps too :)
- swordphish, on 10/12/2007, -0/+2A nice comment here and there to explain what is going on is ESSENTIAL to public source code.
If there isn't at least 1 comment for each function, then free(crappy_programmer);
Also, instead of 'imagining yourself typing code'...why not just actually type it?
The same way you learn better by taking notes...you'll learn better by typing code instead of just reading it.
This is why I don't just copy & paste code off the web.- mtg101, on 10/12/2007, -0/+2Yep - re-tytpe it. Make the layout how you like it, and expand any "clever" tricks (like if(con=++addr=arr[inn+7])) so you can actually see what's going on.
And if it's worth commenting, but not logging, it might be worth looking again at the names of things, or how much stuff your method is trying to do.
- mtg101, on 10/12/2007, -0/+2Yep - re-tytpe it. Make the layout how you like it, and expand any "clever" tricks (like if(con=++addr=arr[inn+7])) so you can actually see what's going on.
- digitallysick, on 10/12/2007, -2/+9I would like to get into programming, but not sure where to start? Should i do it in windows, linux, or osx? I'm looking for something easy to start with. I thought about downloading xcode for mac? or would it be easier to write scripts in linux?
- mtg101, on 10/12/2007, -0/+6Do it in whatever interests you. If you've only got a windows box, maybe you want to try VB or C# to make some Windows apps. Maybe you're more interested in apps for web pages, and so Java or that Flash thing is for you. If you just want to automate your Linux backups, try scripting. Want to just make simple games? Maybe GBA programming is for you.
If you just try to learn generic programming, you might end up getting very bored learning how to calculate factorials using recursion. - swordphish, on 10/12/2007, -7/+4I recommend starting with PHP and web-programming. This will teach you the essential skills required to move on to more complex languages like C/C++ or Java.
Do you have a Mac? Then get Xcode. It supports a multitude of languages and is probably the easiest package to get started with on a Mac. - molotovbliss, on 10/12/2007, -9/+210 PRINT "Any basic language is the best, if your a complete novice."
20 GOTO 10
http://en.wikipedia.org/wiki/BASIC_programming_language - StarManta, on 10/12/2007, -0/+5Like mtg said, if you're learning on your own, do something that interests you. Want to make a web page, learn Javascript and PHP. Want to make games? Download a tool like Unity and give its scripting interface a whirl. (The last one is my personal choice.)
As for XCode, even after you decide to use XCode, you then have to decide what language, etc. to use; if you don't already have a strong CS background XCode is probably not going to do you a lot of good.
Final suggestion? Take classes. - paulmdx, on 10/12/2007, -0/+5If you want to go down the Microsoft route, I would learn C#. All .NET languages are basically the same, and C# is certainly the most popular, which means it's marketable in terms of getting a job.
I can't help with Mac OSX or Linux specifically, but C++ will give you a very strong technical understanding that you can use to learn other languages easily. C++ has a steeper learning curve in terms of producing an interesting program.
If you want to learn to be a strong developer I would recommend against learning to program in an unstructured language like: php, VB6, ASP. That's not to say you can't write structured programs in those languages, but because they don't enforce structure I find it teaches bad habits.
Edit: Having given my dull job-whore blurb, I agree with mtg101 and StarManta - make sure you do something that interests you. - my8086, on 10/12/2007, -0/+6if you want to dabble, start with the area you're interested in as already mentioned. if you really want to code, start with ANSI C.
- ozroy, on 10/12/2007, -0/+6I would definitely NOT recommend starting with PHP or any other scripting language. If you are serious about learning to program I would suggest starting with something strict. There is a reason why a lot of courses start with crappy languages like Pascal. They force you to structure your programs in a very strict way, hopefully teaching you some good habits that will make your future programs better structured and easier to read. C may even be a bit too loose in the way it works leading to 'spaghetti code'
There is nothing worse than being handed the work of someone else that is has crap defined all over the place.
Once you know how to program you can take that knowledge to almost any programming language. - Olle, on 10/12/2007, -1/+4When you start out it is good if you can get as much help as possible and instant gratification.
I would suggest that you download Visual C# Express and start with that. It will help you with syntax, has great compiler errors and quick turn-around time.
I considered recommending Visual Basic over C# because it is supposedly easier to get started with. But in the end I think I must recommend C# because then you learn a more common language structure (understand C# and you can read Java and a little C++).
I don't know about programming for the Mac, so if you want to do that then I have no direct advice other than to stay away from the command line.
Some people will tell you that you need to start with Assembly/vi/Emacs/C/make/perl to _truly_ understand programming. But I strongly disagree with that. When I argue with people about that it usually boils down to the fact that they have fond memories of that since that is how they got started, but that is a bad argument. - WomunOfColour, on 10/12/2007, -2/+1Python.
- mtg101, on 10/12/2007, -0/+6Do it in whatever interests you. If you've only got a windows box, maybe you want to try VB or C# to make some Windows apps. Maybe you're more interested in apps for web pages, and so Java or that Flash thing is for you. If you just want to automate your Linux backups, try scripting. Want to just make simple games? Maybe GBA programming is for you.
- 7heGam3, on 10/12/2007, -2/+1Hasn't worked with me...
- safiire, on 10/12/2007, -0/+5I always do this trick, except I do it under the guise of neatening the other person's code.
- paulmdx, on 10/12/2007, -4/+3So you're the one that's been screwing around with my braces!
- boaman, on 10/12/2007, -8/+5useless tip
- Don_Gero, on 10/12/2007, -3/+1No kidding. I guess you can trick stupid people into actually reading the code by having them retype it.
- yahoofrom, on 10/12/2007, -0/+2useless comment.
- JorgeGT, on 10/12/2007, -3/+1my computer science teacher knows well this... he gives the code examples *printed* in a sheet of paper - "now, type it now to see if they work"
- dvsbastard, on 10/12/2007, -1/+1It might be a nice method when learning a new language as this will help you identify the less obvious code structure (i.e. semicolon vs period) but this technique seems mostly useless when "trying to understand" what brand of drug a previous developer was on when they wrote a particular piece of code if you are already a proficient coder.
- kmgrant, on 10/12/2007, -1/+1He's right, when I read code I imagine re-typing it by hand.
Of course the reason I imagine this is that most code I've encountered is written horribly. Even if the code were incredibly good, all software projects need to examine and rethink the implementation regularly: this ensures code remains well understood (and maintainable), modern, and improving.
Periodic code review also makes it harder for useless software engineers to storm through unnoticed: the kind of people who can be given a task, write terrible code that seems to work, collect many pay cheques and eventually quit before anyone has realized the disaster that was created. - BabyBrumak, on 10/12/2007, -0/+0It's an interesting method for learning the little details of the language. But once you understand programming extremely well, learning a new language isn't that difficult because you'll already be familiar with most of the logic. Then your brain will automatically look for the nuances.
For beginning/inexperienced programmers though, this is an excellent technique. - nicolaa, on 10/12/2007, -4/+2This article is jank. If you actually thought to yourself, "Wow, this is kind of neat." - then you don't deserve to call yourself a programmer.
- ear1grey, on 10/12/2007, -0/+1Back in olden times before the WWW, and before removable media was cheap, computer magazines could not merely include software on their cover-disc so they'd give over large sections of their publication to program listings. Page after page just regurgitating lines of code.
We'd spend hours, sometimes days, typing them in. It was an interesting way to learn because we'd invariably end up with an "error x at line y", so checking line y would reveal a difference to the magazine and thus you'd quickly learn what caused problems like "error x", and how to fix them.
- ear1grey, on 10/12/2007, -0/+1Back in olden times before the WWW, and before removable media was cheap, computer magazines could not merely include software on their cover-disc so they'd give over large sections of their publication to program listings. Page after page just regurgitating lines of code.
- danggit, on 10/12/2007, -0/+2This is basically how I teach myself; only instead of imagining that I'm typing it out, I actually do that. I will look at a block of code, understand what it does; then rather than copypaste it into my editor, I'll attempt to type it out from memory.
If the code doesn't work as expected, I will return and look at the source to see what I did wrong. I've actually found errors from other peoples' code this way, and corrected them myself. I'm not a pro, but this trick has definitely helped me learn as I go along. - thestaunch, on 10/12/2007, -1/+0With the mental route you're liable to cheat, forcing yourself to _actually_ type out everything you want to understand well is a rule I've used for years.
Except third party code I'm using through an API, absolutely zero code in my projects is copy/pasted. I always go through the minor pain of re-typing everything so I'm 100% sure of what went in.
Digg is coming to a city (and computer) near you! Check out all the details on our