BriCode Thoughts from Brian Holt

15Jul/101

My Frustration with BYU

With a title like this, I imagine most people expect me to post something about the Honor Code or some other draconian rule. To be honest: the Honor Code almost never interferes with what I'm doing anyway. I've actually come to appreciate it. The only way it really affects me in an (arguably) adversely manner is that it forces me to shave before I go to the testing center or to play intramural sports. Other than that, it doesn't interfere with anything. On the flip-side, it means all of your professors inherently trust you. For my computer science final, my teacher just handed us the paper and told us not to cheat and let us turn it any time that week. He just told us to remember the Honor Code and  that was that. I can't see that happening at many universities. In that respect, I love the Honor Code.

11Feb/103

Java Checkstyle saying “Expression can be simplified”

Okay, seriously, can we be any more vague? That's like erecting a building only to have you client tell you that you should simplify it. I won't repeat the phrases I uttered after I saw that was the error that Checkstyle was giving me. The description given by the Checkstyle website isn't much better either. So what are you really suppose to do then? What does it all mean?

The part of code I got on was:

while (scan.hasNext() && found == false) {

I figured it must mean that Checkstyle doesn't like the && or || operators and so I worked to remedy that only to no avail. Turns out the that Checkstyle isn't a fan of using the == operator in conjunction with boolean variables.

In plain English: don't use
if(blnVar == false)
if(blnVar == true)
but instead use
if(!blnVar)
if(blnVar)

That will clear up the Checkstyle error. Personally, I find the "correct" results to be more confusing, if anything. I think it's a rather arbitrary, but such is the life of a CS student.

9Feb/101

Java Checkstyle Covariant Equals Method Fix?

If some of you are as unfortunate as I am, your work or university forces you to use a script called Checkstyle that checks your code to see if it was "programmed well." While the script can provide valuable insight on how you clean up your code, it also often causes you to rewrite code that works perfectly well to only be more verbose than it needs to be.

One mishap I have encountered recently is the error "Covariant equals method" and it goes on to say that you're overwriting the equals method without providing and adequate hashCode method. The description from the sourceforge site is equally confusing

Checkstyle's Confusing Description

It seemed finicky at first because it didn't cite at first (when I had written the equals method) but it then it came up and would not go away. For those of facing similar issues, I offer the two solutions I found.

Solution 1:

The reason I got this error in the first place is because I had written an equals method in a parent class and then in again in a child class. The combination of the two, even though still functional, caused Checkstyle to go crazy. I solved the problem by just deleting the equals method in the child class and used only the one the parent class. You could do vice versa.

Solution 2:

If it is expedient to you to keep both equals methods in the parent and in the child class, all you need to do is to write a working hashCode method in the parent class. You could likely do it in the child class as well, but I have not tried that. A really easy way to make a working hashCode method is simply to use the one that Eclipse will make. Go to Source>Generate hashCode() and equals(). Worked like a charm for me.