CompSci/Software Eng/CE majors: does style really matter in coding?

<p>I have a professor who will take off 50% of a student’s grade if their coding style is not in agreement with his, which means a student can fail, even if their program runs properly.</p>

<p>To elaborate, take C++ for example, he’ll take points off if there is a bracket on the wrong line, too many comments, not enough comments, too many blank lines or whitespaces, not enough of the aforementioned, if he dislikes the name chosen for a variable, etc.
He also has a habit of disagreeing with what various programming textbooks teach regarding style, which makes me wonder if these are actual “rules” or just some quirk of his.</p>

<p>I realize that the x-code should be easily understood and legible for both the original writer and whomever is contributing, but is style that important where it weighs equally with program functionality?</p>

<p>Can you understand this piece of code?

</p>

<p>Maybe 50% is harsh but after marking so many assignments your prof is probably annoyed with some of the work your class produces.</p>

<p>I doubt my “prof” started the semester with premonitions of what the class is capable of.</p>

<p>C beautifer may help with readability.</p>

<p>[&#13</a>; C-C++ Beautifier HOW-TO](<a href="C-C++ Beautifier HOW-TO
">C-C++ Beautifier HOW-TO)</p>

<p>I think it’s abundantly fair, to a point. As long as he’s making clear to you what he wants, you should abide by his wishes. That’s how it’s going to work when you get a software job, you know.</p>

<p>Like how hard is he about this? Would the following be alright?</p>

<p>int main()
{
// prints numbers from 1 to 10
for(int i = 0; i < 10; i++)
cout << (i+1) << endl;</p>

<p>return 0;
}</p>

<p>In case the formatting doesn’t work like I wanted, the comment, loop, and return are indented once, and the cout twice.</p>

<p>How much would that count against you?</p>

<p>And yes, style does matter in coding.</p>

<p>According to his teachings, there would be several things wrong with that.</p>

<p>“i” should be renamed to an “appropriate variable name. One providing immediate description to its use.” (I was deducted 5 points for using “i” in a loop that was similar.)
brackets should be placed on the same line as “end” and “start” points (his terms.)
Space before and after each parenthesis set.
Comment describing variable’s purpose. (Even though it’s already stated.)
Comments should be on the same line.</p>

<p>However, I haven’t remembered all of his rules, so I may have missed over some.
He’d likely deduct 15 or so points for that.</p>

<p>I’m not trying to suggest style doesn’t matter, I know it does. My thoughts are that the professor’s personal critiques on style (while some are valid) are petty - particularly since he tends to disagree with everyone else’s teachings regarding the matter.</p>

<p>In the past he has probably had to deal with people who have bad style. People who make ridiculous variable names, who didn’t explain what they were doing, and overall had hard to read code. So he put down hard and fast rules that encourage people to make their code readable, concise, and well explained. </p>

<p>Take this analogy. Imagine if someone was writing an essay. They made the entire thing a single paragraph with random spacings all over the place. It wouldn’t matter how brilliant their ideas were, they would get at most a C for not writing in a presentable mannor.</p>

<p>It’s not unusual for beginning programming classes to place a heavy emphasis on style because if you learn appropriate style up front, it’s likely to stick with you from here on out. Likewise, if you become accustomed to writing with poor style up front, that’s likely to stick with you throughout. Initial classes aren’t just about the functionality of the code but how to code the best way.</p>

<p>Once you get into industry you’ll likely work with other developers in a collaborative mode and style is important at that point since it can be difficult to read code or effectively troubleshoot it when done in a completely different and poor style. Even if you don’t work collaboratively, there’ll likely be times you’ll have to take over responsibility for code someone else does and you’ll be very appreciative if they used good style.</p>

<p>Another important point about style is that if you use poort style you’ll likely make more mistakes and spend much longer trying to find them which lowers your productivity.</p>

<p>So, it’s frustrating when a prof marks down a functional program due to poor style but there are actually good reasons behind doing so. The best thing to do is assume the prof probably knows more about this area than you do and just learn from it and refine your style. Eventually you’ll be glad you did.</p>

<p>It is tough love. He is trying to instill good programming habits into you early on. Readability is a big part of writing code. If you are going to be writing code for a large project it isn’t very helpful if you are the only one who understands your code. And sometimes if it isn’t readable enough, you won’t be able to understand your own code.</p>

<p>Given the harsh judgment of the code I provided, it looks like he’s just instilling in you guys a knowledge of how to follow instructions, rather than a knowledge of good coding practices.</p>

<p>So you should do what he says for that class, but don’t worry about following it after. No reasonable human being would say anything was wrong with my code stylistically… he just wants you guys to understand that it’s better for people to have a standard way of writing code than everybody doing it their own way.</p>

<p>And if you don’t agree with him… that’s fine. You don’t have to like it. Just realize that right now, it’s his way or the highway. Like others have said, he’s probably just being that way to encourage the weaker programmers in the class to get better… introducing non-standard methods in programming is a great way to level the playing field. So more experienced programmers don’t have too unfair an advantage, since they too have to learn things a different way. I believe that’s why MIT starts off with LISP… correct me if I’m wrong.</p>

<p>I’m a TA for an intro CS class, and we don’t grade on style (we run it through an autograder). However, students have problems with their grades and tell me to look over their code to figure out the problem. OMG! I get soooo ****ed! They have random variable names like a,b,c,d,x,y,z,etc… No comments. Their code is generally inefficient and takes up several lines (expected since they don’t know the down low components yet, but comments and good variable names make it easy to follow). It makes it harder to regrade. Many times, they forget to reformat the code to make correct indentations and such. Most interfaces have a smart indent option. So, yes, style is important if someone is reading it.</p>

<p>Well when you get a job, be assured they will have coding standards, and if you consistently don’t meet those standards you wont be loosing 50% of a grade, you will loose a job. </p>

<p>I am a member of a core dev team of a major Open source OS, and we surly do have coding standards, mostly to deal with tags.</p>

<p>the easiest way is to use the unix app indent(3) for c code. use the given flags to make your code look as needed. Eclipse also has a nice formatting tool built in.</p>

<p>MIT starts with Scheme.</p>

<p>

</p>

<p>Using Firefox 3.0.7, there was no indentation, unfortunately.</p>

<p>Another point regarding coding style is your code may be viewed many ways: a WYSIWYG editor; a text editor like vi or emacs; through a web interface like cvsweb. The better the style, the easier people will be able to read and understand it.</p>

<p>The OP’s prof is perhaps being too harsh, but it’s better that it’s happening now in a class, instead of on the job. Coding style is actually a significant part of some software job interviews, for example. On the job, your peers will expect you to code to the style that the rest of the team has adopted.</p>

<p>Coding style is a debatable topic among experts. However, they all agree that it’s important to make the code as readable and understandable as possible. Just try to learn as much as possible - take the good bits and incorporate them in your programs.</p>

<p>I was a CS major way back when, and am now a senior programmer. Yes, style is important. No, his style is not “The” style - reasonable people argue over where brackets go (if you consider programmers “reasonable people”). However, as has been said before, do what he wants. What’s the problem? You know all the rules, so follow them. Consider it one more annoyance, like whether this line gets a semicolon or not, and follow it. Life lesson, not programming lesson: “choose your battles”.</p>

<p>Maybe the guy should teach COBOL.</p>

<p>10 char - deleted</p>

<p>I think his requirement is pretty standard.</p>

<p>The only thing I am not quite sure is “brackets should be placed on the same line as “end” and “start” points”. Is this his own sentence? Does he means something like:</p>

<p>for (…)
{
}</p>

<p>// The brackets are aligned.</p>

<p>Which requirement do you disagree with?</p>

<p>When I have a chance to change some existing codes at work, I always make them conform to standard styles (most the time agreed by team members).</p>