Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've been spending this week teaching kids (11-18) to program. Teaching them the core concepts and the logic has been going fine, but I've noticed one snagging point for them all: syntax.

I feel like teaching them the syntax of the language comes a massive second to the core concepts and logic. However, with struggling with the syntax, they aren't learning as effectively. More often than not, their logic is good enough. The problems they have are related to syntax.

Does anybody know of an effective way of teaching syntax? I've been thinking about creating a cheat sheet showing the syntax for different statements (assignment, if, while, for, etc) but feel this might be a bit of an information overload, has anybody any experience with using this method?

Hopefully this question isn't considered off topic!

share

locked by maple_shaft Apr 21 at 14:52

This question's answer is a collaborative effort: if you see something that can be improved, just edit to improve it! No additional answers can be added here

closed as off-topic by gnat, MichaelT, Robert Harvey, World Engineer Apr 8 at 22:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking career or education advice are off topic on Programmers. They are only meaningful to the asker and do not generate lasting value for the broader programming community. Furthermore, in most cases, any answer is going to be a subjective opinion that may not take into account all the nuances of a (your) particular circumstance." – gnat, MichaelT, Robert Harvey, World Engineer
If this question can be reworded to fit the rules in the help center, please edit the question.

    
What language are you using? –  phant0m Aug 8 '12 at 17:42
    
Unfortunately, I'm mostly using PHP here. It's a hack week for kids, so the choice of language comes down to what's best for their projects. –  AndyBursh Aug 8 '12 at 17:55
    
Just a comment - your students are not babies anymore. So game-style teaching (Mindstorms) doesn't seem applicable. Also, what is the general math/logic level of your students? It may be important in order to adjust teaching methodology. For very prominent students I would go with idea that syntax is not important, and maybe even teach several languages at once. –  bytebuster Aug 8 '12 at 18:24
14  
That and never encourage learning PHP to up and comers because we don't want to deal with that in 30 years... –  Rig Aug 8 '12 at 19:52
1  
Even now after 5+ years programming, I still frequently forget syntax. Especially in languages I don't work very often in. I find it's more efficient to just google the syntax for what I'm doing than to try to memorize all of it. So my suggestion? Teach them how to google for syntax to find good examples fast, and make sure they know how to read the language. –  Rachel Apr 13 at 13:27

9 Answers 9

up vote 24 down vote accepted

You could invent a simple syntax for the English language at first. (But don't call it syntax just yet)

It's all arbitrary

For instance

  • Make up a rule what it means for something to be a word
    • one or multiple letters
  • define a verb to be one of: like, want
  • define what a subject is: I, He/She
  • now define what a sentence is: A subject, an optional "don't", a verb, a word, a dot.

Now have them build sentences that conform to these rules.

Important things to point out:

  • "He like bananas." is now magically a valid sentence, but "He likes bananas." is not.
  • "I like McDonald's." is not a valid sentence either.

  • You made those rules up. Make them see that rules are arbitrary.

  • Just because something makes sense to them doesn't mean it's valid.
  • Just because it's valid doesn't mean it makes sense.

but only the rules, really

Now, explain that someone made up rules for what it means for a program to be valid. And unless they know what these rules are and follow them to the letter, they will not have a valid program.

Make clear that programs are not magical, they are governed by very precise rules that can be learned, understood and combined.

It's like Lego

Show them similarity in terms of "form" and how complex things are made up of smaller elements:

$number = 2;
$sum = 3 + $number;
$product_of_sums = $sum * (4 + $number);

Explain, that the programming language doesn't "know" how to assign a sum to a variable, it doesn't "know" how to multiply a variable by the sum of a number and variable.

Explain, that you can assign a value to a variable, how you can combine two values into a single value (binary operators), which is then assigned to a variable. Draw boxes (colored) around these results: the expressions.

$number = (2);
$sum = ((3) + ($number));

I have found that many beginners seem to struggle with these fundamental similarities, how the form of all of these things is the same essentially.

Probably, I would not try to explain the semicolon. If someone asks, just say: "Remember how you couldn't say 'He likes bananas'?" and explain that someone made the rules this way.

It's turtles all the way down here, too

Stress that the concept of values permeates the entire language: while($variable == true) isn't a special construct. Make them understand that while(<value/expression>) is special, but $variable == True is exactly the same as the values thing above: An operator combining two values into one value.

It is in no way connected to while, the program doesn't "know" the thing inside parentheses represents a condition. That is just our interpretation. For all it is concerned, it wants a value. (same story for if)
Really do stress that == is not connected to if and while, show them that they can do things like: $is_four = (4 == $maybe_four); to strengthen the point.

From this, you show that it is equivalent to writing while($variable) or $while((($variable == true) == false) == false). It may look complicated, but show them that there's nothing magical about it by expanding the expression step by step from within, once for $variable = true and once for false.

I hope I could give a good idea how to approach this. Some things you can expand on:

  • Why it's necessary to have keywords
  • Why it's necessary to have {} to group things together
  • What makes an identifier
  • What constitutes as a value
    • literals
    • variables
    • function calls
  • the for-loop is just a specialization of the while loop, how it captures a common pattern. Show that you don't need the for but that it's very handy to have.

Why PHP is a suboptimal teaching vessel

Unfortunately, PHP's expression model doesn't compose too well.

The entire notion that expressions can be combined freely doesn't hold up in PHP:

func()[0]; // Syntax error

Doesn't work before PHP 5.4. One might think that you could trick PHP by doing this:

(func())[0];

but no, this doesn't work either, nor does it work in PHP 5.4.

It seems like PHP doesn't think in terms of expressions and applying operators to them, instead it some hardcoded combinations of what is possible and what isn't.

Another example: You can call functions by assigning the name to a variable, and calling it:

$var = "func";
$var();

But trying to invoke the function from a different string expression fails:

"func"(); // Syntax error
("other_" . $var)(); // Syntax error
($var)(); // Syntax error

Even worse, in PHP 5.3 functions became 1.61-class functions as opposed to first-class functions with the introduction of closures:

<?php
function test(){
    return function() {
        echo "test";
    };
}

test()(); // Syntax error
$f = test();
$f(); // Works

To me, such inconsistencies make the language feel very "hackish", and I believe if used for teaching, you'll not want to have to answer questions such as explaining why the above doesn't work, if a student really is trying to combine expressions arbitrarily that "should make sense" - at least in the beautiful model of expressions and operators.

Of course, you can say "Well,()/[]are not operators, they are syntactic elements, which is why their usage is constrained arbitrarily by the language designers. Apparently, their rules say that the former can only follow a variable or an identifier, whereas the second can only follow a function call or a variable."

However, the language allows to overload these "operators" for user-defined classes...

share
    
I think this answer. Being able to spot keywords and 'tokenize' an expression correctly ("Oh, that's a function call, that's the assignment operator") is absolutely critical to effectively learning a language. –  AlexWebr Aug 8 '12 at 19:30
    
@AlexWebr Yeah, that's the point I tried going for. I'm not entirely pleased with the result though, but I hope it does come across somehow. –  phant0m Aug 8 '12 at 19:37
    
@phant0m Should this sentence really be '"He like bananas." is now magically a valid sentence, but "He LIKES bananas." is not.' –  Jerry Saravia Aug 8 '12 at 20:03
1  
I'd stress that good tools with good feedback are essential here, cryptic error messages are a major roadblock –  jk. Aug 8 '12 at 21:27
1  
@AlexWebr I didn't want to bring in my own opinions about PHP into this, but while typing up this answer just moments ago I recalled this answer here, and how it renders it somewhat incorrect. I will work in some points about why PHP is a bad language to teach syntax with later on, without having to resort to opinion instead of facts. –  phant0m Aug 10 '12 at 12:37

Teaching programming to kids needs to involve a lot of fun and play elements. Otherwise, it would be less attractive and boring for them.

Well, BIG thanks to online interactive resources, this can be done with less effort. One of this resources is called Alice. It is done by Carnegie Mellon University HCII research group by support and sponsorship of well-known companies in the market.

Alice is an innovative 3D programming environment that makes it easy to create an animation for telling a story, playing an interactive game, or a video to share on the web. Alice is a freely available teaching tool designed to be a student's first exposure to object-oriented programming. It allows students to learn fundamental programming concepts in the context of creating animated movies and simple video games.

Another interesting approach to try is Teach Kids Programming. TKP is an non-profit organization of volunteer programmers and school teachers who have developed a framework designed specifically for teaching basic programming to children aged 10 and up.

share

I think choosing a language with a lot of available reusable code for the kind of projects they want to do is a good approach for a hack week, even though it may not be an optimal choice for a longer term. You want them to be able to continue after there isn't a teacher available. Don't let the php naysayers put you off. The main problem with php isn't the language itself, it's the number of self-taught programmers using it poorly.

Rather than a cheat sheet, I would make a valid source file with well-commented examples of all the syntax elements you want to teach. Then they're more modifying existing code than trying to create it completely from scratch. Something like the following:

// Make a variable
$name = "Karl";

// Print a variable to the screen
echo $name;

// Print all the numbers from one to ten
for ($i = 1; $i <= 10; $i++)
{
    echo $i;
}

// Do something only if something else is true
if ($name == "Bob")
{
    echo "Name is Bob.";
}
else
{
    echo "Name is not Bob.";
}

You get the idea. If there's more than one way to do something, try to stick to one method that's most applicable for now. For example, don't omit brackets for one-line loops, etc.

share

Use a simple, terse, and readable language. I think nothing beats Python on this field, but opinions may vary. I still think C-like syntax is about the worst possible for a first language.

Yes, do create a cheat sheet! It's great to be able to be quickly reminded of correct syntax; it's mostly needed the first few times. Teach kids read the cheat sheet efficiently.

Order your tasks so that kids don't need to remember all the syntax at once. Teach e.g. constants, types, and simple expressions first (1 + 2), then variable assignment (a = 1, b = 'c', done = True), then lists and loops, then conditional statements (yes, these are more complex than loops), etc. One thing at a time, adding 1-2 new constructs per lesson while reusing all the constructs from previous lessons.

share
2  
I'm interested in why you say conditional statements are more complex the loops. I've been teaching them the opposite way around thus far, and it seems to have gone in well. –  AndyBursh Aug 8 '12 at 17:57
1  
+1 for the cheat sheet. Honestly, that's how I program in languages that I'm not familiar with (or, using Python, with functionality I'm not that familiar with since I don't get to use it full time :( - I use the Python documentation as a cheat sheet all the time. Now that I think about it, I also used cplusplus.com all the time when I was in school. That may be the most valuable tool you can teach them - how to Read The Fine(!) Manual. –  Wayne Werner Aug 8 '12 at 20:23
    
@Andy: I'd agree that loops are more complex than conditionals, because any loop that's actually useful has to have a conditional somewhere in order to exit the loop. –  Mason Wheeler Aug 8 '12 at 20:32
    
@AndyBursh: a loop usually has no else / elif parts. C-style for loops are as or more complex than a conditional statement, with all this variable assignment and incrementing. A loop over a list (Python's for) or a while loop is simpler than an if, to my mind. OTOH, there're break and continue... –  9000 Aug 8 '12 at 20:35
    
Very opinionated but have +1 because I totally agree. –  Konrad Rudolph Aug 9 '12 at 12:31

I've taught Java to a middle school student using Greenfoot. The trick to syntax is making sure they get immediate feedback. Use an IDE that can highlight errors immediately or stand over their shoulder and immediately correct syntax errors.

Even patient kids get frustrated by syntax errors, so correct them right away. Don't try to teach syntax by itself because that will bore them to tears. Focus on making something cool like a game in one hour and at that age they should pick up the syntax gradually.

share

The first thing to do, when you have to solve a problem, is to understand the problem. Jeff Atwood had an interesting article a while back reporting on some research that came up with an unexpected and interesting conclusion: some people just don't grasp the concept of an arbitrary, formal rule-based system, and if they don't get it, you almost certainly won't be able to teach it to them, period.

The first of the major hurdles identified by the researchers is assignment. If someone can't wrap their heads around the idea of assigning a value to a variable right away and form a consistent mental model, (not necessarily a correct model, but one that's logically consistent, based on some formal rule,) they're not likely to ever get it. And assignment is a major part of programming syntax, especially in any imperative language. (Such as PHP.)

Certainly there are some things that can be done to make syntax easier for kids to understand. For example, you could try teaching them Pascal, which was consciously designed as a teaching language, instead of PHP, which is infamous for having massive consistency issues. (It's hard to form a consistent mental model when the thing you're modeling is inconsistent itself.) But in the end you'll probably just have to accept that some people's brains are just not wired for formal logic.

share
    
+1 for the article on PHP. I've read it before and it made me sad that I ever used to say that PHP as a language was just fine. –  Wayne Werner Aug 8 '12 at 20:26

There isn't a best answer here, but you'll want a language and environment with simple, concise, and consistent syntax. You should also consider what projects you are doing with the students, and what they might want to do on their own. Everything follow is opinion, of course.

A language that supports functional idioms will be clearer and less error prone. You might do will with racket, as that gives a full environment and tools, and is geared towards teaching, especially with some of the restricted languages.

Definitely go with something that has an interactive mode, and dynamic is probably best. Unless you are really commited to large library availability or bindings to system UI etc., a language like Lua will be much better for this purpose than its bigger, messier brethren like Python or Ruby.

share
    
I want to give the kids the best leg up to programming on their own, so I'd be hesitant to go with something like racket, since I've never even heard of it! I'd like, where realistically possible, to teach in a more commonly used language, since that gives kids a much bigger support net when it comes to "how do I do this?" questions in their own time. Whilst the syntax is C-style, how do you feel about PHP? It's dynamic, quick to read, write and run and quick and easy to get something on the screen. –  AndyBursh Aug 8 '12 at 18:04
    
Very bad idea. Starting kids out by teaching high-level languages will cripple them as developers. If they do not understand fundamental concepts, they'll never know how to troubleshoot and fix things when they go wrong. –  Mason Wheeler Aug 8 '12 at 18:08
    
@MasonWheeler I strongly disagree. I assume you want them to understand how things work closely to the metal, what is fast etc? I believe, it's more important to teach them higher-level concepts such as abstraction, functions, types, etc first while almost completely ignoring the hardware side. Why force them to approach two new things at once? Once the kind of reasoning you need with programming becomes second nature, you can address the implementation level details. -- "they'll never know how to troubleshoot" why not? –  phant0m Aug 8 '12 at 18:19
    
@phant0m: Basic human psychology. It's a lot easier to teach someone something entirely new and then build a higher concept atop it than it is to teach the higher concept first, filling in the missing gaps with "it just works that way magically," and then later go back and teach them the fundamentals. –  Mason Wheeler Aug 8 '12 at 18:24
1  
@JörgWMittag: Lambda calculus is in no way "the very foundation of pretty all of programming." (sic) It's true that any computable function can be expressed in some way in the lambda calculus, but when it comes to actually getting stuff done, its "simplicity" gets in the way. (Have a look at the hoops you need to jump through to do something as simple as subtraction in lambda calculus.) Real programming involves state as the single most fundamental concept, and any system that sticks its head in the sand when confronted with this basic fact is a bad joke, and should not be taught to kids. –  Mason Wheeler Aug 8 '12 at 19:33

I would consider using a language that has friendly syntax first of all, some languages are designed specifically for children and teaching them. Scratch is a good example of this, Lego's Mindstorms is another good example. Another alternative would be a language from the Basic family because their syntax is wordy and follows English more closely than other languages, VB would also give them Intellisense which may be an information overload, especially for younger kids. Python may be another choice to look into, though having to explain that white space is important may be a bit abstract for some to grasp.

A cheat sheet is a great Idea, multiple versions would be best, so information overload doesn't occur. A good starting point might only include variable declaration, variable assignment, and mathematical operators. Then slowly add if statements and the different loop structures.

share
    
+1 for Scratch. I've no experience in it, but the kids do and they seem to have really enjoyed it. I don't know what kind of skills that's helped them develop though As you say, explaining that whitespace in Python is important could be difficult, and that's a big minus in pythons favour, for me. –  AndyBursh Aug 8 '12 at 18:11
    
@AndyBursh Are you sure that's true? If you show them, that the important thing is grouping statements together, you can just present it as such, instead of using delimiters. Use whatever form of grouping you can imagine, whether it is actually used in programming or just circling around the blocks is irrelevant, I believe. –  phant0m Aug 8 '12 at 19:42

Logic is good to have but like you say syntax is important to actually DO things with the language. It's like a spoken language. Knowing how to say the words with correct pronunciation is good but if you don't know the grammar they won't make much sense.

A cheat sheet for the language you're teaching them is good, but also make sure to show them that each language, just like spoken languages, have their own grammar that must be learned in order for the words to have meaning in their context.

share
5  
Programming languages are very unlike spoken languages. –  9000 Aug 8 '12 at 17:36
1  
Programming languages are more discrete but they still have a grammar that must be followed. Spoken languages have a grammar and syntax too but we have fuzzy resolution of that grammar so we are more forgiving than a compiler. Still, the explanation might hold for children. You have to remind them that a computer is dumb and that you have to 'speak' to it correctly or it won't understand. –  Jerry Saravia Aug 8 '12 at 17:51
    
I like the analogy between spoken and programming languages. I've already been saying to the kids they'll learn to read a language before they can write it for themselves, like with foreign languages. They all study a foreign language, which helps! –  AndyBursh Aug 8 '12 at 17:59
    
In my TA years Python was the bane of my existence. –  Rig Aug 8 '12 at 19:53
    
@Rig: Could you share that experience? I'd be interested in hearing about it from you. –  phant0m Aug 8 '12 at 20:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.