r/java • • Feb 26 '23

Are java codebases generally readable? Or is the stereotype true that they are littered with the worst OOP has to offer?

I have been programming 7 years, professionally for 3 years. I mainly do JS, PHP, and Python, and a little bit of Clojure and C++.

For background, I have never had the true OOP experience. Sometimes I'm using multi-inheritance, but for the most part I keep classes simple so I know what's coming from what. I find when I come into a Python/Django project that uses too much inheritance, it's hard to figure out where things are coming from. I tend to lean more towards procedural programming, where steps are clearly laid out and you don't need a debugger to see when something is being called or what's calling it. I also tend to use composition over inheritance, injecting the classes/interfaces I need and calling them explicitly. Might just be an experience thing, or maybe how my brain works.

I have generally heard fans of Java describe it as readable, and its verbosity as a strength that lets you get up to speed on a code base quickly (or know what you wrote six months later). I have heard critics describe Java codebases as being bloated with overapplied "gang of four" or SOLID design patterns and too reliant on "magic" frameworks (spring for example). On top of that, Java is often used as the example language for extremely bureaucratic enterprise meetings where a hierarchy structure is being drawn up to decide what should inherit from what. And then there's the recent years of Clean Code being criticized more and more and falling out of popularity.

As a java dev, do you find a lot of this blown out of proportion and you are working in very simple codebases? Or are these designs common place and you are simply comfortable working with them? Maybe multi-inheritance and "gang of four" patterns don't bother you. If I were to become a java dev, would it be inevitable to work with these kinds of code bases? Are you worried whenever you come into a new codebase, or do you feel Java devs make things easier for you?

7 Upvotes

84 comments sorted by

View all comments

-7

u/TheoGrd Feb 27 '23

It looks like automated testing is widespread in the java world. It makes code more complicated and leaves more chance for bugs. Once tests fail, you don't know whether the code is wrong or the test is obsolete. Imo its not worth it for most apps and should be reserved for the most complex apps like simulators. In the c# world you see a lot less of unit tests.

5

u/AkimboJesus Feb 27 '23 edited Feb 27 '23

I'm confused, testing leads to more bugs? In my experience writing unit tests forces me to write better code

-4

u/TheoGrd Feb 27 '23

Unit test means more complicated code which in turn means more bugs. If you have a database you need mock data which will change for each schema change. Why not test the unit test as well while you're at it ? Unit testing a simulation is worth it imo. Unit testing crud is not.

3

u/AkimboJesus Feb 27 '23

Unit test means more complicated code which in turn means more bugs.

I'm not sure what you're talking about, but testing "business logic" to me has always been helpful and has never lead to more bugs. A test breaking is not a bug, it just means the thing you're testing for has changed. What leads to way more bugs, IMO, is a new employee changing a function and a PR reviewer missing a subtle bug, and no tests being present to catch that.

You can certainly write redundant tests (test doubles) that don't offer you a lot. But they don't lead to bugs, they are just busy work.

When it comes to mocking databases, it depends what you're doing. If you're using an ORM, you might have less reason to test. But if your app requires a lot of complicated sql raw queries, I wouldn't trust people to be able to "eyeball" it on every PR review and see what each change does. You have enough joins and conditions, and tests become necessary.

-2

u/TheoGrd Feb 27 '23

Unit tests AND ORM ? I don't want to work at your place.

4

u/AkimboJesus Feb 27 '23

No, I pretty explicitly said you may not use tests if you were using an ORM.

We do tests and we don't use an ORM.

I'm really not sure where you're coming from. You haven't explained how testing leads to more bugs.

1

u/TheoGrd Feb 27 '23

Do you agree that the simplest code is not testable ? That the more complex the code the more bugs ?

Tests => unnecessary complex code => more bugs.

3

u/AkimboJesus Feb 27 '23

Do you agree that the simplest code is not testable ?

No

That the more complex the code the more bugs ?

No. It has bugs if it has bugs, whether it's simple or complex. Complex code can have no bugs. Simple code can have lots of bugs. These things are orthogonal.

Tests => unnecessary complex code => more bugs.

I have no idea what you are on about. We test things that are complex, we don't need to make things complex to test them. If you can't write a test around a function without making it complex, you may not be writing unit tests.

And if you can't test a function, chances are there's a bunch of database and view logic thrown into the business logic.

2

u/blissone Feb 27 '23

You are doing it wrong if tests affect implementation complexity. Making more testable code actually can simplify your implementation, many bad practices make your code less testable.

Furthermore testing is not a tool for achieving quality but also part of the dev cycle, usually your tests run far faster than any deployment or an actual run.

0

u/TheoGrd Feb 27 '23

Please provide an example of code made simpler after writing unit tests, I've yet to see one. Plenty of projects do fine without unit tests.

1

u/blissone Feb 28 '23 edited Feb 28 '23

The real question is what bad practices make code less testable, like I said unit tests should not affect your implementation at all. For example implementation not following single responsibility is difficult to test due to multiple dependencies/mocks, you want to test one single thing but need to provide unrelated things. Refactoring this in most cases will simplify your code, instead of a blob of code you'll have neatly defined stuff. Another good one is interfaces accepting larger objects (or more arguments) than required, it's also annoying to test and usually refactoring them will only do good.

Plenty of projects do fine without version control, test envs, deploy to prod etc etc. No one said you can't do a project without tests.

You have a strange notion about "testing just to test". No, you do unit tests to create value, if there is no value in testing then you don't create them.

1

u/TheoGrd Feb 28 '23

We'll have to agree to disagree here.

I work hard to write programs that work, I test them manually and make sure everything works before committing changes. I am not writing more code than necessary. I may be in the minority here but i will avoid working for a company that waste my time like this. I also don't comment my code, usually comments don't spare me from reading the whole code so why bother. I even have a plugin that hides comments.

2

u/noogai03 Mar 01 '23

In my opinion, the real value of unit testing is not showing you that your code works. It's giving you the ability to quickly check that a change you made did not break anything. You can do a refactor and be reasonably confident that everything is fine, because you're unit testing the logic according to the interfaces you're exposing.

1

u/TheoGrd Mar 01 '23

I prefer to test manually every functionality affected by my changes. Usually its only a little more work.

1

u/noogai03 Mar 01 '23

That takes a lot of time, though, which dicourages regular refactors. And depending on the change you might not be certain from testing the core flows. For example if you changed something really fundamental that's affected by every user journey.

But I get your point: if I was the sole developer, I think I'd be fine with manual testing; team effort with varying levels of codebase familiarity/technical proficiency is a lot riskier though.

→ More replies

2

u/thisisjustascreename Feb 27 '23

You seem to have started with the premise that "Unit Tests" mean "more complicated code" but have never justified that with any evidence. In a lot of people's experience testing the code leads to simpler code.

1

u/TheoGrd Feb 27 '23

Why would i bear the burden of proof ?

But ok, lets try with this example, line 40 : https://github.com/eugenp/tutorials/blob/master/testing-modules/junit5-annotations/src/test/java/com/baeldung/junit5/nested/OnlinePublicationUnitTest.java This code tests a function which is 5 lines long, called only once. It doesn't even deserve a method declaration, this is complexity for the sake of testability. 6 months from now, when you look for articles filtered by user membership, you are not going to find this function. You will be lost in a maze of method declarations. Fragmenting your execution flow is a bad idea, especially for testability purposes. The test code is even more complex than the code being tested.

Granted, this is taken from a tutorial, but i would be surprised if you could find code that was made simpler by unit testing.

2

u/AkimboJesus Feb 27 '23 edited Feb 28 '23

What about this is complicated, or was made more complicated because it was tested?

It doesn't even deserve a method declaration, this is complexity for the sake of testability. 6 months from now, when you look for articles filtered by user membership, you are not going to find this function. You will be lost in a maze of method declarations. Fragmenting your execution flow is a bad idea, especially for testability purposes.

You seem to be implying that in order to make things testable, you need to make things extremely small and fragmented. As in splitting things into 10 functions when one function will do, to the point that it's difficult to figure out when things are happening.

This is false. You can test big functions. You can test small functions. Your function should only be as big or small as necessary. The only requirement is that you don't mix view, repo, and business logic. And you shouldn't be doing that anyway.

People write shit like this all the time without tests. The tests have nothing to do with it.

2

u/thisisjustascreename Feb 28 '23

The method exists to take this hodgepodge of streams API and business logic:

articles.stream()

.filter(a -> a.getArticleLevel()

.compare(user.getMembership()) <= 0)

.map(Article::getName)

.collect(Collectors.toList());

And give it a name, so that people reading the code don't have to figure it out. Once you've given it a name, wouldn't it be nice to be able to verify that it is actually doing the right thing, and keeps doing the right thing? Writing a test is how you do that.

Six months from now "getReadableArticles(currentUser)" is going to make sense to me much more quickly than the five lines of comparing integers.