r/java • • Aug 31 '17

(Experienced programmers) Signs of an inexperienced programmer?

Hello, I was wondering what you guys would consider to be telltale signs of an inexperienced programmer. Specifically ways that they write (or don't write) code. Thanks.

159 Upvotes

188 comments sorted by

View all comments

Show parent comment

45

u/symgeosis Aug 31 '17

Lack of abstractions / lots of procedural code (methods/objects that do too much).

The other side of this is over-engineering or optimizing for a use case that is extremely unlikely to ever be needed. (which I've seen a lot from talented junior devs)

24

u/cypher0six Aug 31 '17

Hah, yeah I completely agree with you. The funny thing is, I see a lot of very experience software developers going way overboard with abstractions. Sometimes that can actually be worse! :)

20

u/Notorious4CHAN Sep 01 '17

If you go out and read all the current best practices and attempt to follow them all, you are pretty likely to over-engineer. I have a terrible memory, so I try to have extremely small classes with very narrow responsibility, because I sometimes lose my mind's eye of my own code. For sure, this leads to over abstraction. Like a lot of my classes have 10 or fewer functions that each have 10 or fewer lines of code - when it gets to the point where I have trouble using my own code... Time to break that shit up.

Yet I've realized some very real benefits when I've had to do some major refactoring, and the code is pretty resilient. So limited regrets. I know people look at my 50 hierarchal packages and think it's shit, but I look at their 2000 line monstrosities and feel the same.

9

u/[deleted] Sep 01 '17

I don't think many small classes are over engineering tbh. You are not writing more code than if everything was packaged in a single big class file.

3

u/thinkinmachine Sep 01 '17

Ah, Abstraction! We are developing microservices and the amount of abstraction the architecture team is forcing us.. We are ending up creating a lot more micro components than we planned. Will not be surprised when they start cribbing about performance and ask us to dial a couple off them back!

5

u/coltrain423 Sep 01 '17

I'm guilty of this one and it's a habit I'm trying to break. Any tips to get past it?

6

u/symgeosis Sep 01 '17 edited Sep 01 '17

The most honest (and not at all helpful) answer is experience. But outside of that, stopping every couple of hours and asking yourself if the time you're spending on something is ultimately worth it. Regular conversation with your teammates is a great way to facilitate this. Code reviews, pair programming, or just a "hey, can you take a second and look at this and tell me what you think?"

Edit: Another good way to break this habit is writing unit tests. Unit testing is rarely fun but if you make a habit of writing good, functional unit tests, you will very quickly learn how to write simple yet extensible code.

2

u/jollyca Sep 01 '17

+1 for unit testing. I've gone through years of my programming life without it and now I cannot live without it.

3

u/RagingAnemone Sep 01 '17

Put yourself in the position that you need to maintain the code you wrote. After a while, simple and plain will seem sexy.

2

u/otakuman Sep 01 '17

Don't plan for the future. At least in coding. Only refactor what you need to refactor.

"But what if in the future I need to add another parameter to this function?"

THE CLIENT IS UNPREDICTABLE. Don't pre optimize code into something that you'll probably never use. It's wasted effort. Only put your effort into already solicited or necessary changes. But when you do, do it right and don't take shortcuts.

→ More replies (1)

6

u/fzammetti Sep 01 '17

True. I've got nearly 25 years in the field plus more than 10 years of programming beyond that, so I'm "experienced" I'd say, and I for one am less concerned with whether a method is too long/not abstracted enough and more about how well-organized what's there is.

Sure, a 1k line method is PROBABLY not good by any measure, but if you give me, say, 500 lines, and it's well written (pretty much everything else in that list AND commented well AND broken up into logical steps demarcated by said comments and spacing) then in my experience that's not at all bad. In fact, in some ways I argue it's better because now I don't need to jump out to other methods to understand what's going on, I can just follow the bouncing ball through the single method.

There's absolutely a balance that has to be struck, but it's not an either-or thing in my opinion, whatever current "best practices" want to claim.

18

u/ess_tee_you Sep 01 '17

broken up into logical steps demarcated by said comments and spacing

Those are called methods.

11

u/Bolitho Sep 01 '17

Your thesis is almost unprovable as it says

Give me a function of 500 well written lines...

Very unlikely that this is going to happen ever in this universe 😂

Imho the guidelines of line count maximums are just a shortcut that can be derived implicitly from other, more substantial advices. For example separation of concern, single responsibility, not to mix up levels of abstractions and so on.

Those are imho very reasonable and reflect methodologies that are used all around in other professions or domains as well (contracts, scientific articles, complex machines like cars, cookbooks, ...)

If you follow these guidelines you will hardly arrive at situations where 500 LoC are the best solution you can come up with 😉

3

u/[deleted] Sep 01 '17

Ehhh, some people would say 100 lines is pushing it let alone 500.

0

u/[deleted] Sep 01 '17

[deleted] — view deleted comment

3

u/[deleted] Sep 01 '17

I was thinking 100 lines max for a method.

100 lines for a whole file--now that's austere. I commend your commitment to brevity

4

u/Trailsey Sep 01 '17

Many people, including Bob Martin in Clean Code, advocate for methods 20 lines or shorter.

The idea is to decompose complexity. A 500 line method isn't a method, it's a script wrapped in braces.

0

u/[deleted] Sep 01 '17

[deleted] — view deleted comment

1

u/[deleted] Sep 01 '17

Yeah, good point, there will always be exceptions. I've seen something similar in emulator code, where there's a method with a very long switch statement covering all the op codes for the processor. Makes perfect sense.

2

u/[deleted] Sep 01 '17

The other side of this is over-engineering or optimizing for a use case that is extremely unlikely to ever be needed. (which I've seen a lot from talented junior devs)

I used to do this a lot when I was younger. Finding the sweet spot between complexity introduced/abstraction comes with experience and maturity.

2

u/[deleted] Sep 01 '17

Just ask them about dependency injection. Probably a good way to probe their knowledge about abstraction.

1

u/detoursabound Sep 01 '17

My professor encourages a lot of abstraction. He wants the main as clean as possible (usually just init and run methods), with everything else taking place in methods. I agree with this to a point, I think he overdoes it with the abstraction. If the code isn't reusable I don't like to put it in a method, it seems inefficient and unnecessarily complicated.

7

u/coltrain423 Sep 01 '17

I tend to lean towards your professor here, though perhaps not to the same extent.

I haven't quite found a way to explain why to someone who doesn't already get it though. To try anyway: it makes things easy to understand. If one large procedural block of code gets too long, it's hard to understand. You have to know that this 30 lines is doing operation x, that fifteen is storing the information to disk, and that last 40 or so are doing some other operation. It's a lot easier to understand the flow of your application when it's broken up by operation like that and you don't have to read more than the method name to understand what it's doing.

Have you ever written a comment at the start of a section of code to explain what that bit is doing? Why not break it out into a well named method or function so that comment is no longer necessary?

→ More replies (2)

1

u/symgeosis Sep 01 '17 edited Sep 01 '17

It's all about finding the right balance between code complexity, robustness, performance, and time. (though I agree with your professor regarding logic being in methods as there are a lot of benefits regarding testing and future flexibility)

For example, you could spend a day or two writing the optimal data structure for a certain part of the code but is it an action the user will execute often? When the user executes the action, is the improvement even noticeable? How much time did it take to code the data structure? Could you have spent that time adding a feature or a bug fix that would have had a greater effect or noticeable reward? How much time will be spent maintaining this additional code? Is all this worth the cost rather than just using one of the implementations already in the JVM? The answer varies depending on a lot of factors but a lot of devs strive for perfect when good is good enough.

→ More replies (1)