23
$\begingroup$

On open source projects and industry teams, there are often detailed style guides about how many spaces should be used for indents, line continuation, etc., and whether braces should go on their own line. While I have my preferences (such as spaces around binary operators), I acknowledge they are somewhat arbitrary. Should I require students to follow a standard for assignments and team projects (perhaps conventions of their own choosing), or should I ignore these as trivialities?

Of course, I would enforce more important conventions, such as proper indentation, appropriate naming, etc.

December 10, 2001, Hackles cartoon, in which 2 programmers dispute formatting of curly braces

$\endgroup$
6
  • 7
    $\begingroup$ Teach the students to use the formatter in the IDE they are using. Eclipse has format as a save action so it happens all the time. Then you can teach more important stuff. $\endgroup$ Jul 7, 2017 at 18:04
  • 4
    $\begingroup$ Start with Python. The problem almost solves itself. $\endgroup$
    – 3Dave
    Jul 7, 2017 at 21:12
  • 2
    $\begingroup$ @DavidLively Would you suggest Tabs, 4 spaces, or 2 spaces for indentation on Python? $\endgroup$
    – phihag
    Jul 8, 2017 at 5:50
  • 3
    $\begingroup$ @phihag There is an official style guide (PEP8) for Python. $\endgroup$ Jul 8, 2017 at 9:35
  • $\begingroup$ @phihag anything as long as they're consistent. My point was that Python is inherently less forgiving than other languages, not that it is a perfect, self-contained solution. $\endgroup$
    – 3Dave
    Jul 8, 2017 at 14:25

10 Answers 10

22
$\begingroup$

Our two deities are clarity and efficiency.

Standardization makes a great deal of sense in industry, where large numbers of programmers may work with the same lines of code and must be able to make sense of each others' choices. However, I don't see value in enforcing particular style guides in an educational setting.

Different companies can require different norms. If your students eventually enter the industry, a style guide is easy to read through if they can code, and is irrelevant if they can't.

When I teach my kids to worry about indentation, vertical spacing, variable names, etc, I teach them to always use this question as their foundation and their guiding star: "what does this choice communicate?"

That is honestly already quite a lot to tackle, and I have found that my kids really do start working quite hard and thinking quite deeply about how to make sure that every line of code is as clean and clear as it can be.

If my students can learn to scale this mountain, why would I ask for 4 spaces in place of a tab?

$\endgroup$
1
  • 5
    $\begingroup$ Having been a software architect for years, enforcing coding standards has been part of my responsibilities. Nevertheless, I fully agree with your assessment that this topic does not deserve significant classroom time, and I would wish more educators would take your stance. A consistent coding style is particularly valuable in long-running projects with big teams, but a typical student assignment is neither. Also, I find your emphasis on readability over formatting details spot on, because formatting can be established by automated tools, while naming and design still require human judgment. $\endgroup$
    – meriton
    Jul 7, 2017 at 18:55
16
$\begingroup$

I think placing some emphasis on style and conventions is important, yes -- learning how to follow style guides is a skill worth training and learning to be detail-oriented is an important meta-skill.

That said, I think it's fine to introduce some leniency by allowing students to use whatever convention they want as long as they're consistent, especially for things like curly brace placement or tabs vs spaces. If the goal is to train students to acquire a sense of craftmanship, having them demonstrate they know how to be consistent is enough, I think.

Some formatting rules do seem to be more universal though (across languages and style guides), such as adding spaces around binary operators. It's probably worth enforcing rules like those, to make sure students acquire correct "muscle memory".

I think the answer to your question might also vary depending on age -- the older students are, the more we should expect from them. In particular, once students are at the undergrad level, they're basically adults and expecting them to be detail-oriented seems pretty reasonable to me, especially if they plan on doing CS as a career. After all, sloppiness in the small often tends to translate into sloppiness in the large, and churning out students who think it's ok to cut corners seems somewhat irresponsible to me.

One middle ground between enforcing and ignoring convention would be to provide students with a linter (which they can perhaps tune with your permission?). If you make students responsible for turning in code that perfectly passes the linter as a binary pass/fail sort of thing, you'll make sure standards are maintained without putting too much pressure on the students. This also has the added benefit of being more representative of how development is actually done in industry.

I would probably introduce these tools only after your students have had a chance to develop a sense of consistency and craftsmanship, though.

$\endgroup$
4
  • 6
    $\begingroup$ +1 for the comments on consistency. That was my first thought. Let them choose their own style, because it's a fun personal preference to get irrationally angry about (so you'd get plenty of push-back from people who already learnt how to code and decided on their favourite brace style, if you did try to enforce one). But penalise them if they aren't consistent within their own code, or with other code written as part of a group project. $\endgroup$
    – Muzer
    Jul 7, 2017 at 9:09
  • 1
    $\begingroup$ On the other hand, is it not important to teach students that, out in the real world, you can't always use your preferred style? Getting them used to using tools for formatting (whether it be IDE-based or a script they use, etc.) provides a benefit; I've found that once students get in the habit of using whatever style they want, even when they're in college it's hard to get them to stick to a style guide even when tools are available to easily modify the style. $\endgroup$
    – JAB
    Jul 7, 2017 at 13:59
  • 2
    $\begingroup$ I disagree strongly. Attention to detail is best reinforced where it actually matters and through natural consequences. For instance, having to track down one's off-by-one errors communicates the need for caring about details far more effectively than being asked to adhere to arbitrary rules "because the teacher said so" ever could. Also, while I fully agree that craftsmanship is important, code formatting is a very small part of that - and one of the least important ones, because it can be (and often is) fully automated. $\endgroup$
    – meriton
    Jul 7, 2017 at 19:08
  • 3
    $\begingroup$ That is, if you want to be close to the industry, give your students development environments that automate code formatting. For many languages, even open source development environments can do this :-) $\endgroup$
    – meriton
    Jul 7, 2017 at 19:11
7
$\begingroup$

I tell my students this. Pick a style and be consistent. You will see religious wars over

for(...)
{
    code
}

and

for(...){
    code
}

Both are FINE. Pick one and be consistent. I find the first easier to use because it makes finding curly brace errors butt simple. But some prefer the other and that is AOK with me.

$\endgroup$
9
  • 1
    $\begingroup$ I just recently heard the second style described as "Egyptian brackets" which totally cracked me up (also the style I use) $\endgroup$ Jul 6, 2017 at 23:29
  • 6
    $\begingroup$ Yes, both are fine, but we all know the second method is better...=P $\endgroup$ Jul 7, 2017 at 0:12
  • 17
    $\begingroup$ warning: missing space before opening brace $\endgroup$
    – Cœur
    Jul 7, 2017 at 3:10
  • 1
    $\begingroup$ Ctrl + A, Ctrl + Shift + F. $\endgroup$
    – pojo-guy
    Jul 7, 2017 at 4:51
  • 1
    $\begingroup$ If you accidentally comment out the for line, the first will still (unfortunately) compile, and the second will give you an error. :/ $\endgroup$
    – 3Dave
    Jul 7, 2017 at 21:23
4
$\begingroup$

The simple way is to use an IDE that does it for you - say Eclipse. Then you can be a bit sloppy for a few minutes and get it cleaned up. You can define the style you like in the options. You can pass your code to a buddy who has a different style and it gets transformed with a click or two.

But one option you have is to suggest to your students that they "ain't writing crap". They are writing literature. They are writing poetry. If it is beautiful you will find it easier to live with. E. E. Cummings cared a lot about indentation.

A coding style is a garment that you live in. It should be a comfortable one. Make it so.

A friend who is a lisp expert claims he can see the intent of a piece of lisp code (properly indented and parenthesized) at a glance. He means it literally. All he sees is the pattern of indentation. I have a guess that you can reach that level in Python. Java being less compact in general might not enable that as well.

However, you can sometimes see that a piece of code is broken by glancing at its indentation structure.


Old person horror story. I once worked in Pascal on IBM mainframe terminals. You put a space before every semicolon since the search function would only work on contiguous blocks so mumble and mumble; required different searches. You always "spaced out" every identifier and regretted it when you didn't.

$\endgroup$
3
  • $\begingroup$ Buffy and I must be from the same timeline! +1 for "let the IDE do it". $\endgroup$
    – pojo-guy
    Jul 7, 2017 at 2:48
  • $\begingroup$ Lisp, IIRC, has the advantage (for that kind of thing) that its predominant indentation styles depend somewhat on the length of the function/special name on the previous line. Imagine if for loops were indented one space further than if blocks. $\endgroup$
    – Random832
    Jul 8, 2017 at 3:09
  • $\begingroup$ I do a lot of my programming in terminal at the command line and am a power vi user. $\endgroup$ Jul 8, 2017 at 17:28
2
$\begingroup$

Simple answer: Yes.

  • Standardization matters, arbitrary or not.
    • Imagine if some chose to use punctuation in writing, and some didn't... talk about a nightmare of confusion.
    • A basic standardization of code writing usually results with a higher efficiency when having to go back through the code for removals or additions... it will also make the job of whomever is grading the work that much easier.

I'd compare a basic standardization of spacing and braces to the same principal of basic training in the military.

  • Basic training is there to teach one the basics, of which many things taught are not likely to be used by the unit one ends up in, as they will have their own ways of doing things.
    • So while an arbitrary business or project may have it's own standardization, one should still be taught a basic standardization of spacing, braces, and indentation.
$\endgroup$
4
  • $\begingroup$ Well, looking at your text... you chose your own punctuation in writing. The punctuation you're using is not proper English (and what style of English are you using anyway? There's many to choose from in the first place). Not a good way of reinforcing your opinion of "standardised formatting is so important" :) Now, I think we can all agree that there's plenty of things that are important - your text would be a lot harder to read if it didn't use periods or spaces. I assume your indentation is intentionally silly. But commas, periods, ellipsis... there's so many valid styles out there :) $\endgroup$
    – Luaan
    Jul 10, 2017 at 9:17
  • $\begingroup$ I understand the point of view you're coming from, however I do disagree with it. Perhaps the following two versions of the same S.M.A.R.T Reporting Script I use for FreeNAS would demonstrate my point a bit better. S.M.A.R.T Report w/ formatting & S.M.A.R.T Report w/o formatting... which do you find easier to read and edit? Granted, in an editor it's a bit better, but not much $\endgroup$
    – JW0914
    Jul 10, 2017 at 14:24
  • $\begingroup$ The first. But it's still your style of formatting. I'm all for readability, but where's the standardisation you speak so highly of? Is this a common bash formatting standard you're following, or just something that looks good and readable to you? The question is all about the conflicts among standards ("indent with four spaces!"), not the things people tend to agree on ("indent!"). You might be answering a different question. Would a script that uses four spaces (or a tab) to indent instead of your two be any worse (assuming all the other scripts in the context use the same style)? $\endgroup$
    – Luaan
    Jul 10, 2017 at 14:41
  • $\begingroup$ No, the OP asked specifically "Should I require students to follow a standard for assignments and team projects (perhaps conventions of their own choosing), or should I ignore these as trivialities?" Perhaps you misread that? If the script with formatting applied is easier for one to read, then it would also, by default, be easier to grade than a script w/o formatting. $\endgroup$
    – JW0914
    Jul 10, 2017 at 18:08
2
$\begingroup$

I briefly discuss spacing, braces, and general readability. Then I open Eclipse and demonstrate [Ctrl]+a, [Ctrl]+i. This instantly indents your code according to a default standard (which is configurable). Since there are so many concepts to cover in class and limited time, this is one shortcut we take.

$\endgroup$
1
  • 2
    $\begingroup$ Hi Edwin! Could you please expand on this further? There really isn't much in this answer. $\endgroup$
    – thesecretmaster
    Jul 7, 2017 at 12:52
1
$\begingroup$

Often times students (i am speaking of high-school leveled) dont like to be constraned too much. The important conventions you do enforce should be enough. If you add more to it then the students might be feeling that you are demanding too much from them and they might enjoy classes less.

However if you still think that spacing is important to you then i suggest letting the students code in a IDE that has auto formatting abilities (*for example intellij Ctrl+Alt+L and eclipse Ctrl+Shift+F, which might suit you because i understand from your last questions that you teach java).

In those IDE the students are responsible for doing correct naming and and some other conventions. Indentation becomes the only thing you need to make sure they learn. If you do choose this method then just make sure they understand why its important to format the code properly. If you want to know how to make them understand it, i'm sure you can ask that here.

$\endgroup$
1
$\begingroup$

While all of these are important, some are more important than others.

  • Local consistency over global consistency.
  • Style over the one true style.
  • Automation over completeness. (that is settle for a simple set of rules, that can be automated).

I have worked of projects where every file had its own style. It was not a problem to read, but when a file had many styles it was. Switching style may take experience. There is more buy in when a person is involved with the decision. Therefore each pupil should choose a style and stick with it.

It is also easier if problems are found automatically and as early as possible. Therefore use an IDE that does it for you. Then as a second level of defence use a lint tool that detects problems. If these two can to do it then it is usually not worth it. However there is still a lot of judgement, that can not so easily be put into rules.

$\endgroup$
0
$\begingroup$

All my workplaces have had a set style-guide and expected me to follow it. In some cases failure to follow the style would result in the build system complaining and lots of finger-pointing.

If your goal is to teach students how to code as a career, then set an arbitrary style-guide and enforce it. Even better would be to use different styles for each assignment.

Teach your students that any modern IDE worth using can be configured to require a style, and they shouldn't be wasting time trying to do such things manually.

$\endgroup$
3
  • $\begingroup$ Your second and third paragraphs seem to contradict each other. If the IDE can do it automatically, what good does asking for a different style in every assignment do? $\endgroup$
    – meriton
    Jul 7, 2017 at 19:55
  • $\begingroup$ @meriton that way, they learn to adapt to different styles. I'm not sure that doing it every assignment is a good idea - in fact, that sounds horrible - but learning how to work using uncomfortable conventions is valuable. Take a look at Unreal 3 code sometime. Every Thing Is Capitalized, And The Code Is So Long That Intellesense Does Not Work. It's impossible to look at an identifier and tell what it is. $\endgroup$
    – 3Dave
    Jul 7, 2017 at 21:28
  • $\begingroup$ @meriton - gets the student used to working in different styles. let the student learn how to configure their editor/IDE to support that style. Expose the student to different, well-known styles (google's style guide, the linux kernel style guide, etc). $\endgroup$
    – ivanivan
    Jul 7, 2017 at 23:54
0
$\begingroup$

As always once you start looking into a topic you will find that there's a whole history and reasoning behind it.

For example the infamous issue of where to place an opening curly bracket does actually make a difference. Placing it on the start of the next line creates a more symmetric image, since both the opening and the closing bracket are at the same indentation level, so this style makes more sense in languages where symmetry is important, like (old-fashioned) C++ with its patterns like new and delete, setup and shutdown, etc.. Using "symmetric" curly braces makes it easier to spot memory or reference leaks when you use objects that need to be cleaned up manually.

On the other hand in languages where you use a lot of anonymous functions like JavaScript or in programming styles that make extensive use of lambda functions there are actually cases where placing the opening curly bracket makes you more prone to bugs. Also I would argue that the readability is improved, since the function body is just part of one value, so to show that you are writing declarative code inside imperative code you may want to place the new line behind the opening curly bracket.

Just like that there's a rationale behind almost every coding style decision, so what I do and what I recommend is that you talk about the reasoning behind different styles and then let the students choose a style of their own.

The only things I would grade in papers and examns would be whether the coding style is consistent and adequate. For example every now and then I find code that was copy-pasted from the internet, which in itself is not an issue, but most tutorials optimize the code for readability on a web page, which of course is very different from what you would do in an IDE.

In the end don't forget that as soon as you start combining languages there is hardly ever one consistent coding style, so you will have to make up your mind which style you want to use anyway.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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