How to explain to a high school hacker that indenting and verbose variable names are good things?

StackOverflow https://stackoverflow.com/questions/533958

Question

He is good programmer (won some competitions) but he absolutely ignores formatting. He consider i, j, k beautiful... I hope he won't find out about existence of goto keyword.

Was it helpful?

Solution

I told my students (post-secondary) that they had the choice of writing code well or of me writing their assignments in the same sort of way that they wrote their code. I told them I would write the following program:

  • take the text of the assignment
  • lookup a number of the words in a thesaurus and replace them with obscure versions
  • remove all punctuation
  • remove all whitespace
  • convert everything to lower case
  • insert random whitespace
  • capitalize random letters

They could then have the assignment... hey its "right" (all of the words are there) good luck understanding what the assignment is though.

Oddly the complaints stopped at that point :-)

I also compared it to English. We use paragraph breaks, capitalizations, etc... as a convention. When someone chooses not to follow the conventions it makes reading much harder.

OTHER TIPS

Write some code in his "style" and then ask him to read it and explain to you what it is doing.

What's good for the goose and all...

tell him about python :)

Make him maintain somebody else's code that's written the way he writes. Then make him maintain somebody else's code that was written with good style.

A combination of FORTRAN77 and Python should sort him out.

Code maintainability

Stuff I didn't care in high school neither :)

Write a bunch of "his" code and ask him to find a particular piece of code.

Give him some badly written code with a bug in it and ask him to find the bug.

Well, if he plans to do this for a living just explain that he will have a very rough life on a real team if he doesn't at least make some effort to follow the team standards. If he doesn't plan to do it for a living, don't worry about it.

You also might determine if there is anyone(s) that he admires. If there is then there is a pretty good chance that they follow standards.

I would point out that having clean code is a sign of a organized and intelligent mind. However, the real killer will be when he writes a large amount of code. I doubt you will be able to convince him because more than likely he is getting excited about the logic of the app and not the process. It will take experience to teach him a harsh lesson. So here are my suggestions.

  • Give him a project full of messy unformatted poorly named code and let him suffer.
  • Encourage him to work on a project with a large code base and let him see how well he remembers his own variable names after the 1,000th source file.
  • You probably can't. Some people just don't get it. I use self-describing variable names both at work and in private where noone tells me to. I also got some appreciation at work for using long and understandable names.

    If a guy does not do it neither for himself or for your project then you've got that kind of guy. Show him some docs on the source code style policy. Explain why this is important.

    You begin to use the right naming convention after you've got some experience and you see how and why this was useful. Without experience it's just an abstract talk.

    P.S. Sometimes I get stuck with variable names because I'm not sure if this particular name does conform to the common linguistic style I use in the current project or how would the name scale on the high litterature language. The problem of using bool b1 vs. bool IsSomePropertyAvailable has never come up since the first university years.

    I'm pretty sure you can misconfigure a code beautifier to present such horrible output. Obfuscaters are common, and do essentially the same thing (short useless variable names, no indenting, poor use of whitespace).

    Give him the assignment of taking an existing program with his style and adding a trivial feature.

    Also, take code he wrote 6 months or more ago and give the same assignment.

    -Adam

    Maybe he's not ignorant, maybe he's just inspired by Kernighan & Pike.

    i,j,k is fine for loops.

    I personally prefer using 1 letter vars in iterations...

    foreach ($test as $t) 
    {
    }
    

    beautiful :D

    Licensed under: CC-BY-SA with attribution
    Not affiliated with StackOverflow
    scroll top