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.

252 Upvotes

215 comments sorted by

View all comments

191

u/happy2pester Jul 12 '13

Not commenting, and giving nonsense names to your variables.

24

u/commodore-69 Jul 12 '13

I got into the horrible habit of naming variables 'test3' and looking at them later and not knowing what the hell they're supposed to do

28

u/happy2pester Jul 12 '13

I'm just peering over one of my old programs now, here's a list of some of my variables.

n,l,a,lset,etype,deus,Ruby,ruby,hper,wper,mper,fper,pper

It goes on and on and on. And it gets better. All of those are global variables, because I didn't know how to do it otherwise.

Also: Only 1 in three functions have any comments, and several resources that the program relied on have the location hardcoded in.

30

u/commodore-69 Jul 12 '13

I opted for the super descriptive variable and I'm still not sure what it does

varTakenFromTurningBecauseItsWithThisScriptFromTheBeginning

16

u/happy2pester Jul 12 '13

The previous version of that program had variables like "Bananas" and functions called "Screenish"

Still. I think I had some fairly elegant solutions in there.

8

u/[deleted] Jul 12 '13

I vomited. Then again we all have written some bad code at one time or another.

→ More replies (7)

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.

→ More replies (2)
→ More replies (3)
→ More replies (3)
→ More replies (1)
→ More replies (1)

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.

→ More replies (2)

31

u/arimnaes Jul 12 '13

As a corollary to this - comments are just one solution to the problem of hard-to-understand code, so the more general advice would be "write code that's easy to understand." This encompasses several points, including but not limited to:

  • Write code that is as close to English (or other spoken language) as possible. Give methods, parameters, and variables clear and descriptive names. Conciseness is also nice to have, but usually err on the side of long and descriptive rather than concise and ambiguous.
  • Don't perform clever tricks to shorten your code like folding ++ operators into unrelated statements and performing assignments inside conditionals - you almost never get an actual performance increase and it will make your code much harder to understand.
  • In imperative languages (Java, C, C++, C#, etc.), keep the execution flow of your code as linear as possible. Mostly, this means avoiding goto statements. They are almost never the right solution when structuring your code.
  • Use line breaks to separate logically distinct groups of statements. If a group of statements gets very large, consider putting it into its own method with a descriptive name. When your methods are well-named and mostly consist of calls to other methods, they are very easy to follow.

Comments are a subject on which there are as many opinions as there are programmers, but my opinion is that they should only be used as a last resort. Writing your code in such a way that it speaks for itself is always superior. Unlike code, comments can lie - they can and do get out of date as the code around them changes, and a misleading comment is worse than no comment at all.

9

u/[deleted] Jul 12 '13

I've been programming in C# for at least 2 years now and I didn't know it had goto O_O

→ More replies (2)

4

u/istroll Jul 12 '13

To add to this. Comment why you did something, not necessarily what you did. I can see what you did, I want to know why you did it that way if it is unusual or if you found out some tricky issue that needed handling.

→ More replies (1)

14

u/ziplokk Jul 12 '13

Yes, when I first started coding, I would make variables named "a", "b", "aa", etc. Just to save a few seconds. It's not worth the hassle. Take the extra few seconds to make the variable name as descriptive as possible. Stick with a style as well, A_NAME_LIKE_THIS is a primitive type, ANameLikeThis is an object. It's not too important, but it helps later on while skimming a code and knowing what you're dealing with from a glance.

5

u/happy2pester Jul 12 '13

I just tracked down the program that I was thinking about when I made that comment, and it is a horrendous mush of bad habits and poorly named variables. In my defence, it was written in 2009.

And while overall, it's not great I think I have some rather elegant solutions to a couple of problems in there.

→ More replies (16)

10

u/YuleTideCamel Jul 12 '13

good naming convention is key, but be judicious with commenting. It is possible to over-comment and create extra noise that makes code harder for read. It's also possible to create useless comments that could have been replaced with a well named method.

This is an example of a useless comment //set counter to 0 int counter = 0

This is an example of good method naming replacing a comment //calculate average public double GetAV(int[] numbers) { // do stuff } the comment could be removed by naming the method: public CalculateAverage(int[] numbers){ //do stuff }

There's more of course, it's all illustrated in Clean Code

→ More replies (1)

7

u/HadManySons Jul 12 '13

I remember hearing "comment as if a serial killer was going to come behind you and review your code"

→ More replies (4)

5

u/duffmanhb Jul 12 '13

Commenting was a huge thing I had to learn... While coding a piece, you don't think you need to comment on it, because you've spent so much time on it, it's second nature knowledge. However, after it's all said and done, and you've gone through quite a bit, a bug occurs and you have to go back to your code and think, "WTF is even going on here?"

→ More replies (11)