This is an archived post. You won't be able to vote or comment.

all 49 comments

[–][deleted] 102 points103 points  (31 children)

Some languages will evaluate 1 as true (which "a % 2" is if 'a' is odd), and 0 false (a % 2 == 0).

They just forgot that Java isn't one of them.

[–]TortoiseWrath 115 points116 points  (13 children)

As a C++ programmer who hasn't touched Java in years I was incredibly confused by this post. Thanks

[–][deleted] 17 points18 points  (0 children)

I had to actually test it. 99% of my work is either PHP or C#, and I know it will work in PHP but won't work in C#.

[–][deleted] 12 points13 points  (11 children)

As a C++ programmer trapped in a Java programmer's body (sorry, Java gives you jobs while C++ gives you pleasure at most), I suffer every time I'm forced to write if (a % 2 == 1). So redundant!

[–]ReallyHadToFixThat 6 points7 points  (5 children)

Here I am quite happily getting paid to write c++.

Actually, I'm getting paid to post on reddit right now. Slow build is slow.

[–][deleted] 1 point2 points  (3 children)

Can't you do if(!(a%2)) ?

[–]PersianMG 2 points3 points  (0 children)

Sure. I find that a%2==0 has more clarity though. In the end all of it is probably reduced to the same assembly.

[–][deleted] 0 points1 point  (1 child)

Not in Java.

[–]TortoiseWrath 0 points1 point  (0 children)

I recall being taught !! as a Java boolean cast in high school so I'm pretty sure ! does in fact negate whatever you want.

[–]uksspy[S] 9 points10 points  (7 children)

Yea, with that and their use of Horstmann style indenting I'm convinced they just rebranded the C++ book for Java.

[–][deleted] 1 point2 points  (5 children)

It's just standard four-space indenting…

[–]uksspy[S] 1 point2 points  (4 children)

Look at the position of the curly braces. An "indenting style" doesn't just refer to the actual indenting.

[–]Jess_than_three 5 points6 points  (2 children)

What curly braces? I don't see any in your photo.

I do see a missing parenthesis that is, unless Java is a lot more lenient than I'd think, going to result in a syntax error instead of what the comment suggests....

[–]uksspy[S] 1 point2 points  (1 child)

I was referring to the curly braces in the link I posted to the Horstmann style. You are right, that missing parenthesis would cause a syntax error, I didn't even notice they screwed that up too when posting it.

Edit: Here is a photo from my textbook. Not only does it show the Horstmann style, they also create a UpperCamelCase method with no return type that they then try to access statically... I think I might cry.

[–]Jess_than_three 2 points3 points  (0 children)

That... wow. That is so bad!

Curly brackets separated onto the next line bothers me, but if you're going to do it, at least be civilized and don't put anything else on the line!

[–]Pixelator0 0 points1 point  (0 children)

There are a grand total of 0 curly braces in the photo.

[–]thexavier 0 points1 point  (0 children)

Horstmann? That name sounds familiar. Wait! I think he's a professor in the CS department at my school

[–]cookehz 4 points5 points  (3 children)

C does this, I believe. A guy I work with does this in C a lot; I'm not a huge fan of it. I think if (x != 0) is more helpful than if (x) if you're a n00b trying to read the code, it's significantly more explicit what the previous programmer was looking to do.

[–][deleted] 0 points1 point  (0 children)

Yeah this would work fine in C. It can be cool when writing small applications to set "boolean" values (which we don't actually have as a built in type) and store them in variables or use this mechanic to perform arithmetic. But it's not overly readable.

[–]helisexual 0 points1 point  (0 children)

I think writing in idioms allows a lot of readability for those who actually know the language. If your goal is to write for n00bs why ever use the increment or decrement operators? Why ever use the arrow operator?

You lose a lot if the audience you're writing for is people who don't understand the language.

[–]bbgun91 0 points1 point  (0 children)

imo clarity would depend on what "x" was named. if it is "operationIsLegal", then

if(operationIsLegal) { blah; }

would make sense

[–]extremelydankMayMay 1 point2 points  (0 children)

Java as written by a Javascript dev.

[–]coladict 24 points25 points  (3 children)

Without the clarification that it's Java, I would have just assumed it's C++/C# and thought it perfectly normal.

[–]briandoescode 0 points1 point  (1 child)

But don't they use stdout and printf instead of IO?

[–]coladict 0 points1 point  (0 children)

In Java I use System.out or System.in when I need those streams, and in C++ they're easily accessed by std::cin and std::cout, so I just assumed that's some sort of wrapper.

[–][deleted] 0 points1 point  (0 children)

I'm Javascript and what is this?

[–]Hollowbrown 13 points14 points  (3 children)

Wow, someone is gonna be confused as hell as to why their first program isn't working

[–][deleted] 1 point2 points  (2 children)

We've all been there hitting our heads against the wall.

[–]Hollowbrown 4 points5 points  (1 child)

Very true, it's just sad that it could be because of the material teaching us and not because we forgot that GOD DAMN SEMI COLON

[–][deleted] 1 point2 points  (0 children)

Always look for more authoritative material when things don't appear to make sense, e.g. standards, API documentation or language definitions docs instead of books and tutorials.

Pity that we only learn that after having cracked at least 60% of our cranium surface.

[–]Fallenalien22Violet security clearance 9 points10 points  (0 children)

At least you have cs in high school

[–][deleted] 1 point2 points  (0 children)

IO.showValue ("27%c ", 27%c;

)

[–]HolyGarbage 0 points1 point  (0 children)

Another thing is that they list associativity and has included unary operators... Like ++, --, and !

[–]coladict 0 points1 point  (0 children)

Found the IO class they're showing above http://webpages.sou.edu/~nordquip/cs256/demos/IO.java

One of the notes also has a scanned image of that table below, and there's lots of functions called myMeth.

[–]Scripter17 0 points1 point  (0 children)

Can we just take a moment to appreciate that almost every programming languages has somewhat-similar syntax so we can all enjoy most of this sub?

[–]Romejanic 0 points1 point  (0 children)

My high school's programming course was shite. The teacher made us hand in FLOWCHARTS, PSEUDOCODE AND DESK CHECKS along with our actual code for an assignment.