Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Checkstyle

  • You cannot reply to this topic
15 replies to this topic

#1 HappyCoder   Members   -  Reputation: 3120

Like
10Likes
Like

Posted 15 August 2014 - 02:59 PM

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."



Sponsor:

#2 unbird   Crossbones+   -  Reputation: 7486

Like
0Likes
Like

Posted 15 August 2014 - 07:01 PM

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.



#3 C0lumbo   Crossbones+   -  Reputation: 3079

Like
1Likes
Like

Posted 16 August 2014 - 01:16 AM

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.



#4 Ohforf sake   Members   -  Reputation: 1982

Like
0Likes
Like

Posted 17 August 2014 - 03:31 AM

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.

#5 Sik_the_hedgehog   Crossbones+   -  Reputation: 2197

Like
0Likes
Like

Posted 18 August 2014 - 08:57 PM

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.

#6 Hodgman   Moderators   -  Reputation: 40900

Like
5Likes
Like

Posted 18 August 2014 - 10:17 PM

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. 


Edited by Hodgman, 18 August 2014 - 10:24 PM.


#7 Ohforf sake   Members   -  Reputation: 1982

Like
0Likes
Like

Posted 19 August 2014 - 03:52 AM

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.

#8 swiftcoder   Senior Moderators   -  Reputation: 14429

Like
0Likes
Like

Posted 20 August 2014 - 12:49 PM

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 - Software Engineer @Amazon - [swiftcoding]


#9 Bregma   Crossbones+   -  Reputation: 6166

Like
3Likes
Like

Posted 20 August 2014 - 02:47 PM

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

#10 Jan2go   Crossbones+   -  Reputation: 1431

Like
0Likes
Like

Posted 20 August 2014 - 02:52 PM

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.

#11 jHaskell   Members   -  Reputation: 1437

Like
0Likes
Like

Posted 21 August 2014 - 01:39 PM

I'm sure these classes still stress heavily commenting your code as well.



#12 Glass_Knife   Moderators   -  Reputation: 7247

Like
2Likes
Like

Posted 21 August 2014 - 03:54 PM

They still teach programming in programming classes?  No one programs anymore.  You just download, install, configure, and wire up technology stacks using other people's libraries.  If you're "writing" your own code it is obvious you didn't research the problem well enough.

 

biggrin.png


Edited by Glass_Knife, 21 August 2014 - 03:55 PM.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532


#13 Ectara   Crossbones+   -  Reputation: 3093

Like
0Likes
Like

Posted 21 August 2014 - 04:21 PM

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. 

I tend to compromise; I keep my comments wrapped at 80, and I try to keep long expressions wrapped, but I will not break things in awkward places, like splitting six parameters into one parameter per line if their names and types are too long.



#14 Ectara   Crossbones+   -  Reputation: 3093

Like
0Likes
Like

Posted 21 August 2014 - 04:23 PM


What on earth is wrong with soft-wrapped lines?

Having an identifier or typename wrap mid-token is jarring for me.

#15 swiftcoder   Senior Moderators   -  Reputation: 14429

Like
0Likes
Like

Posted 21 August 2014 - 05:39 PM


Having an identifier or typename wrap mid-token is jarring for me.

I don't think I've ever seen an editor that wraps mid-token. Even word processors only do that for a very well-defined set of legally hyphenated words...


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#16 Sik_the_hedgehog   Crossbones+   -  Reputation: 2197

Like
1Likes
Like

Posted 21 August 2014 - 08:24 PM

They still teach programming in programming classes?  No one programs anymore.  You just download, install, configure, and wire up technology stacks using other people's libraries.  If you're "writing" your own code it is obvious you didn't research the problem well enough.

 

biggrin.png

I wish it was that easy ._.'


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.





PARTNERS