r/learnprogramming • • Jul 12 '13

What are some bad coding habits you would recommend a beginner avoid getting into?

Whether they're stylistic habits or program design habits. I'm learning java and want to be sure to write my code as efficiently as possible to avoid lengthy code and poor design choices.

249 Upvotes

215 comments sorted by

View all comments

Show parent comment

47

u/ericswc Jul 12 '13

This plus think about the Single Responsibility Principle

31

u/happy2pester Jul 12 '13

I'm afraid I don't know that. Would you mind giving me the tl;dr?

95

u/minno Jul 12 '13

Every "thing" (function or class, mostly) should do exactly one thing. If you have to use the word "and" when you describe what it does, there's a good chance that your program would be better-architected if you split it up.

18

u/happy2pester Jul 12 '13

Ah yes, that makes a lot of sense. I think I might be guilty of it in places, especially in my old work, but I think I mostly stick to the principle

7

u/harvest3155 Jul 12 '13

Does this apply if you need something to look at multiple criteria?

Like

if A > B And X < Y And C <> Z Then D= "YAY"

22

u/minno Jul 12 '13

It doesn't really apply in that case. I was thinking more of "this function reads input from stdin and parses out the English words", which should have a function that reads input and then either calls the function that parses the words or otherwise passes the data to the separate function.

5

u/harvest3155 Jul 12 '13

thank you

20

u/thiswillspelldoom Jul 12 '13

as a note, if I had a condition that complex, I would refactor it into a method that returns a bool, and name the method appropriately...

function meetsCondition() {
    return A > B && X < Y && C <> Z;
}

if(meetsCondition()) {
    D = "Yay";
}

10

u/kristianwilliams Jul 12 '13

God damn it, where were you a few months ago. Now I want to re-write half the code.

3

u/[deleted] Jul 12 '13

You'll learn from it and do better on your next project.

2

u/akerson Jul 12 '13

this is a form of refactoring (called extract method) and its one of the most common refactorings other than just renaming variables/fields/methods. Its never too late to refactor (: in fact a good practice (good subject for the thread!) is to constantly look at your code as if you were looking at it for the first time and seeing if you can tell your python/java/c#/whatever story a little clearer.

3

u/aphistic Jul 12 '13

Except avoid "reaching out" to global variables from a function.

3

u/MainStorm Jul 12 '13

I think putting that into a variable rather than a function might be easier to read. Something like:

bool meetsCondition = A > B && X < Y && C != Z;
if (meetsCondition) {
    D = "Yay";
}

1

u/jimbo973 Jul 12 '13

I personally hate this, especially if the logic is used only once. Why make someone have to dig up another function to figure out what your logic is or to make sure it is correct? This might be 'pretty' but I would not call it readable.

1

u/pegasus_527 Jul 12 '13

So how do you decide how high level your description should be? Does it simply depend on what you're doing?

1

u/minno Jul 12 '13

Program organization is more an art than a science. I'd say the best way to learn is to get advice from more experienced people on specific things until you start to see the general patterns.

1

u/theavengedCguy Jul 12 '13

I like this, for those familiar with C, think getchar() and putchar(). getchar() reads a character, then putchar() is used to output it

1

u/Speedzor Jul 12 '13

If the method is named as IsPersonEligibleForPension then you could certainly use this and the SRP will still apply. The function of a method should be described without "and", but the implementation can definitely contain that.

This would be wrong: IsPersonEligibleForPensionAndIsNotACriminal.

3

u/tboneplayer Jul 12 '13

Wish we could get our government legislatures to adopt that principle when crafting bills!

7

u/DEiE Jul 12 '13

And if you do want to read: The Single Responsibility Principle by the original author.

10

u/n35 Jul 12 '13

My quib with the Single Reponsibility Principle, is that it usually results in a ton of functions and/or classes.

It increases the code-base greatly, so it is usually a trade-off, following it strictly, is not always the answer. It largely depends on the scope of the project and how often this is something that will be used.

4

u/ericswc Jul 12 '13

I agree, that's why I said to think about it. :)

When do you stop following a pattern or abstraction? When the readability cost exceeds the benefit gained. Both a junior dev and a senior dev can write code, the main benefit of experience is having a good idea of where that line is.

1

u/n35 Jul 12 '13

That's true. I just had flashbacks tonne being told to use this exact concent on a project were it was not needed and it only made everything going cluttered.