What's the recommended and / or required way to wrap the condition on an if block?

For example, I have this block:

if ((islower(p[i]) && encrypted > 'z') || (isupper(p[i]) && encrypted > 'Z'))
    // do stuff

I would prefer to think of the 80 character line length limit as a guideline rather than a rule. To me, having it all on one line seems easier to read and understand in this case. But style50 doesn't like it.

Obviously, this could be broken down into two separate if statements or nested if statements, but that's unnecessary duplication and a waste of processing power since the // do stuff is the same for both.

Should I put a newline before the or, like this?

if ((islower(p[i]) && encrypted > 'z')
    || (isupper(p[i]) && encrypted > 'Z'))

... or after the or ...

if ((islower(p[i]) && encrypted > 'z') ||
    (isupper(p[i]) && encrypted > 'Z'))

... I've also seen this ...

if (
    (islower(p[i]) && encrypted > 'z') ||
    (isupper(p[i]) && encrypted > 'Z')

... and this ...

if (
    (islower(p[i]) && encrypted > 'z')
    || (isupper(p[i]) && encrypted > 'Z')

... and on and on it goes through many different variations and opinions on programming style.

What do the instructors / teaching fellows want to see?


Playing with this concept and style50, I used this code:

int x = 5;
// If x is greater than 5 or less than 5, it's not 5.
if (x > 5 || 
    x < 5)
    printf("Not 5\n");

If I just hit enter after the ||, style50 complains:

:( 6:14 Not enough whitespace after "||". Expected: 1, found: 0.

If I use the same code with a space after the ||, style50 is happy.

Do they just follow what style50 reports without actually reading the code?

Thank you.


There are no hard and fast rules on how to split a long test condition. It's mostly about doing what makes sense and what makes it easiest to read. For instance, putting one test condition per line when there are many of them is a good idea. I would never put a line split in the middle of a test condition. Also, indenting the split is definitely a good practice.

The single white space after the || operator makes style50 happy because it is simply checking that there is a space there. It doesn't take the line split into consideration. (style50 isn't that smart.)

Do they just follow what style50 reports without actually reading the code? That depends. If you're taking the actual Harvard CS50 class for college credit and paying tuition, they look a lot closer. Someone actually reviews and grades the style and efficiency of the program. But, if you're taking the CS50X online class, then it's just the opposite. The psets will be graded strictly by the results produced by check50. There will not be any checking or grading of the programming style. However, you should definitely be checking it yourself with style50 until you are consistently producing good results. It's an excellent way to learn good style techniques in the beginning, and then learning when to break the rules. ;-) Bad style can make it easy to introduce bugs into code and makes it difficult to find them later, not to mention fix them.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • I'm taking CS50x, so this answers my question. It's still not clear to me if the machine also uses style50 when grading, but I can easily compromise my own preferences to keep style50 happy. Coding style is nearly a religion for some programmers and there are many differing opinions on the subject. This isn't my first forray into coding, either, so I've encountered some extremists. Accept your answer?! What... #2 for all-time reputation isn't good enough for you!? :P
    – Vince
    Feb 20 '16 at 6:59
  • No, check50 does not look at style or style50 results. It only looks at the results produced by the program under test. But it's still a good idea to get your programming style under control. You'll understand why the first time you have to go back and look at your own code a year after you wrote it and ask yourself "What was I thinking!?!" ;-) And it's good to know someone else is watching the reputation scores. :-D
    – Cliff B
    Feb 20 '16 at 7:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .