r/badcode • • Jan 10 '20

java Oh rly?

Post image
861 Upvotes

31 comments sorted by

153

u/sendvo Jan 10 '20

thanks for reminding me how much i hate comments like this
return id; // returns an ID

97

u/DurianExecutioner Jan 10 '20

Actually I thought id referred to Freud's psychoanalytic concept of the id and superego; this comment really cleared things up.

25

u/T-Dark_ Jan 10 '20

Out of curiosity (I'm new to programming), what should I write in the javadoc for getters and setters? I can't find anything better than this, but skipping the javadoc feels wrong

27

u/sendvo Jan 10 '20

the general rule of thumb for comments is don't write what is it doing but why is it doing something (also try not to write code that needs extra explanation). getters and setters are pretty obvious they get and set things :) i usually don't write docs now (in php) since we use php 7.3 / 7.4 which support strict types. phpdoc is a thing of the past when variables could be any type and you could only know from phpdoc if it's an integer or a string. not sure what are the conventions in java programming tho :P

4

u/eled_ Jan 10 '20

The PHPDoc for specifying the expected type out of a method call is indeed a thing of the past, but definitely not the PHPDoc used for documenting the behaviour or design of classes, interfaces and methods.

The Javadoc has pretty much the same role.

15

u/no_particular_order Jan 10 '20

Unless a getter is doing something complicated keeping the default is probably fine.

For a setter you could describe valid values for the property. Should ID be in a certain range of numbers? Is there an inactive ID value? That sort of thing.

4

u/MeezeeDE Jan 10 '20

Depends, sometimes can code speaks for itself. If in doubt, just write something, always better than doing nothing

2

u/carfniex Jan 10 '20

nothing unless it does something unusual or you need to explain the field or something like that

1

u/itsTyrion Jan 11 '20
  1. Use Lombok and let it generate getters and setters.

  2. 🤔

  3. Profit

1

u/T-Dark_ Jan 11 '20

Or just press three keys and let Eclipse do that without the need to install a dependency. It also generates a basic javadoc.

Sure, Lombok does have some very nice features (Although I'd personally rather see the boilerplate, but to each their own), but that was not my question.

1

u/Neo399 Jan 20 '20

Or just press three keys and let IntelliJ do that

FTFY

1

u/T-Dark_ Jan 20 '20

So, I heard people hate on eclipse a lot. Is there a particular reason or is it just a common meme?

1

u/Neo399 Jan 20 '20

Half meme, but the software just feels outdated and clunky compared to IntelliJ, or hell even any of Jetbrains IDEs.

In the world of Java you have three-ish choices, Eclipse, Netbeans, and IntelliJ. I've tried them all and I just love IntelliJ.

They make IDEs for most of the popular programming languages. I used CLion for C++, WebStorm for HTML/CSS/JS and Rider for C#. (As another example, everyone loves Visual Studio for C# stuff, but I think Rider is way better)

26

u/Svizel_pritula Jan 10 '20

How many methods throw an exception during normal operation?

75

u/pine_ary Jan 10 '20

Every single one? How else do you return a value? Exception-oriented programming is where it‘s at.

30

u/Svizel_pritula Jan 10 '20

That's too much boilerplate. Just send info to the parent method via a TCP socket.

22

u/pine_ary Jan 10 '20

Telnet. Take it or leave it.

6

u/erlangguy Jan 10 '20

Erlang: hold my beer.

5

u/PixxlMan Jan 10 '20

I prefer fax

4

u/Retro_RL Jan 10 '20

Let's just replace booleans/switches and instead throw a different exception for each condition

11

u/TheUnlocked Jan 10 '20

All the time. Many exceptions are the intended result of normal operations, for example an IO error from another process holding a lock on a file that you want to access. Exceptions as a form of control can also be useful in certain situations for passing information down the stack, for example a halt message.

9

u/[deleted] Jan 10 '20

Different question, then: how many methods throw an Exception, i.e. the default exception class?

9

u/T-Dark_ Jan 10 '20

A lot. All of them are written by terrible programmers.

3

u/[deleted] Jan 10 '20

Well, pretty much all JUnit test methods, but I don't know if you'd count those

1

u/TheUnlocked Jan 10 '20

Ideally none.

5

u/EquationTAKEN Jan 11 '20

The init part is strictly unnecessary. But the @throws tag is actually useful if you're auto-generatic docs for this later.

The biggest contention I have is; don't throw a generic exception. Preferably, you should have various exception classes that are a bit more informative than the generic one. Self-documenting, if you will.

7

u/HerissonMignion Jan 10 '20

It a todo reminder

3

u/jmack2424 Jan 10 '20

But why male models?

1

u/cdjinx Jan 10 '20

Throwback Friday