Checkstyle

Started by
14 comments, last by Sik_the_hedgehog 9 years, 7 months ago

A friend of mine told me about an experience he had in one of his programming classes. I found it funny, so I thought I would share it here.

Programming joke of the day; this actually happened to me:

We have a little program we need to run on our code called "Checkstyle" that goes through our code to find anything stylistically wrong with it. If it doesn't pass check style, we can't get credit on it. Sometimes, it takes issue with stuff that really shouldn't (in my opinion) be an issue.

Today, it spat out a bunch of error messages telling me that some of my lines of code were more than 100 characters long. I said "Are you kidding me? We aren't allowed to have lines of code longer than 100 characters?"

A haughty T.A. passing by said "Of course. Checkstyle likes readable code. It's more professional."

A person sitting next to me said "Hey, if your line is too long, try taking out the 'space' characters between your operators and your operands. They aren't needed to compile anyway."

I said "Great Idea! That'll cut down the line size, which makes it more readable, right?"

Somebody else suggested "Are your variable names too long?"

I said "Yeah, probably. I'll global search and replace them into names that are only one character long. That should help too."

After looking at my code, I found that the issue was my error message. My error message was about 60 characters long, politely informing that the only legal strings allowed in the method are "heads" and "tails". I replaced the error message with a frowny face emoticon instead.

After putting the finishing touches on my code, I ran it through checkstyle. It passed. I got 102% on the lab. I'm so glad they gave me such a wonderful tool to make my code more professional and readable!

Along a similar vein, checkstyle doesn't like "magic numbers." In one lab, it told me that "4" was a magic number, and needed to be replaced. In class, somebody commented on it. "What is a magic number, and how do you avoid using them?"

The teacher said "A magic number is a number in the code that is there for no apparent reason, but makes the program work right. Typically, you should replace them with global variables. Like, can anybody tell me how to handle the magic number '4' in the first lab?"

I raised my hand.

"Well, '1' isn't considered a magic number, so just replace all instances of '4' with '(1+1+1+1)."

The entire class laughed. Including the teacher.

"Just in case you were wondering," he added, "that was not the right answer."

My current game project Platform RPG
Advertisement

After looking at my code, I found that the issue was my error message. My error message was about 60 characters long, politely informing that the only legal strings allowed in the method are "heads" and "tails". I replaced the error message with a frowny face emoticon instead.

"Improved" programming style and made program less user friendly. Good teaching.

The correct solution would have been to spread the error message over multiple lines using either the \ character or string concatenation (http://stackoverflow.com/questions/1135841/c-multiline-string-literal), no?

So:

MyLogFunction("Really long string blah blah")

Could be

MyLogFunction("Really long string "

"blah blah");

I don't have a problem with long lines, particularly for a logging message, but I believe that blind programmers quite like shorter line lengths, so it's not just petty bureaucracy to have a limit.

Damn, that was a po-faced reply. Have a vote up because I liked the story.

No, the "correct" solution would have been to make them aware of gcc's "-Wall" or "-Weffc++" or "cppcheck" or VS's "/analyze". Or any of the other tools, that perform static code analysis in a reasonable manner and are written by guys who actually know this stuff, instead of some student who has never written a program in his live, except in the previous year when he took the exact same course in which he is now the tutor.

As for line lengths, while you can rotate monitors by 90° without too much effort these days, most guys I know work on an unrotated 16:10 or 16:9 monitor, so long lines are actually not that bad.

Well, the problem with line lengths is that if they're too long they can indeed become an unreadable mess (they can only be so long until you eventually get lost). Of course the proper solution when long lines are unavoidable is to split them, not to remove whitespace.

Also even now when I'm using widescreen I still stick to short lines. The extra space is used by IDE panels =P (and if I do a vertical split, it becomes easier to fit everything within a readable font size).

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

laugh.png "The best way to get a bad law repealed is to enforce it strictly"
Demonstrating that the rules are broken by creating unreadable code that passes the readability guidelines seems a fair demonstration!


As for line lengths, while you can rotate monitors by 90° without too much effort these days, most guys I know work on an unrotated 16:10 or 16:9 monitor, so long lines are actually not that bad.

90º monitors / 9:16 aspect FTW!! I use a 1920*1080 and a 1440*2560.
Also, when using a 16:9 aspect, I always split the IDE down the middle so I can see two files at the same time cool.png

No need to strictly stick to the traditional 80 characters (as derived from when we coded via terminals), but it's still a good idea to resort to line-breaks after some point.

90º monitors / 9:16 aspect FTW!! I use a 1920*1080 and a 1440*2560.

Ahh, man, I want one of those too!

No need to strictly stick to the traditional 80 characters (as derived from when we coded via terminals), but it's still a good idea to resort to line-breaks after some point.

Ok, I redact my earlier statement, at some point you should break your lines. But the way I see it, the only "damage" a long line does, is that it forces you to scroll to the right. And the only work required to fix it, is to click somewhere appropriate and press enter. Compared to the amount of crap you can do in C/C++ (I hear "delete a, b, c;" is all the rage now), and all the tremendous amount of headaches they can cause, some isolated long lines are just not an issue.

What on earth is wrong with soft-wrapped lines?

I know a handful of archaic IDEs don't support it (MonoDevelop, I'm pointing at you), but the rest of the world does support it, and it makes life one whole lot easier...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

No need to strictly stick to the traditional 80 characters (as derived from when we coded via terminals)

Dude, you are sooo wrong.

It came from when we used punched cards, which only had 80 columns. I still have boxes of them from when I was in university. Those babies predate digital computers even. Noah used them in the bible when he got the 10 commandments, that's why none of them are more than 79 characters (the first column marked the commandment as a comment).

Stephen M. Webb
Professional Free Software Developer

What on earth is wrong with soft-wrapped lines?

I know a handful of archaic IDEs don't support it (MonoDevelop, I'm pointing at you), but the rest of the world does support it, and it makes life one whole lot easier...


Personally I prefer hard-wrapped lines simply because of the fact that I can decide where to break it. Soft-wrapping might use the screen's width better but the linebreaks will sooner or later end up at positions where you don't like them. At least this happened when I last used soft-wrapping, perhaps they found better algorithms to do this by now.

This topic is closed to new replies.

Advertisement