r/learnprogramming • u/just_jesse • 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
135
u/modalblunders_alter Jul 12 '13
Copy/paste is a reminder to rethink how you can make the block reusable as a function. When tracking bugs down later it really sucks to fix the same thing 5 times because you felt lazy and went with the old Ctrl+c/Ctrl+v solution.
Always comment "clever" solutions because months later you will wonder why the hell you are doing "something weird" there.
Don't overuse any pattern you have learned. Sometimes solutions really are simple and do not require "that trusty drill" every single time.
Learn to write unit tests early in your experience. Lacking this routine makes you a poor developer later.
Don't spend "100 days" to decide a tech stack or framework for your latest idea. Attacking sooner is better than choosing the "best framework" 1 year from now.
Don't bitch about another person's code without offering a solution. As a beginner don't let somebody rip your code apart without asking how they would do it.
191
u/happy2pester Jul 12 '13
Not commenting, and giving nonsense names to your variables.
73
u/angryee Jul 12 '13
Coding by exception: start writing an algorithm with a data set, get it to work, find new data that's different and write an if statement to handle it. Find a new wrinkle, write a new if statement, lather, rinse, repeat. It generates truly awful unreadable code.
20
u/smerity Jul 12 '13 edited Jul 12 '13
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." -- Brian Kernighan
You almost never fix the hacks that you do, even if you say "It's only temporary, I'll do it properly later". Either decide to be happy with the hack you've done (as yes, sometimes they're the appropriate decision) or do it properly to start with. They also become a large impediment to refactoring as now you have to not just refactor, but fix your laziness from the past.
Adhere to a programming style guide. Preferably one that can be forced on you automatically by another program the second you save. You'll thank yourself for it later and it will mean that if you work with other people, you're all forced to write vaguely similar code. There are enough issues with working with others than to start arguing over minutiae.
Also, the earlier you can get into tests, the better. They feel like a drudge, but even on the smallest of codebases they can become invaluable. I only realised this after interning at Google where they obsess over testing. If anyone has good resources for becoming obsessed over testing, I'd love for you to point them out so I can hand it to others.
29
u/YuleTideCamel Jul 12 '13
Read the book Clean Code. It's full of useful tips for beginners. This is seriously one of the most important books for any programmer.
10
u/Iggyhopper Jul 12 '13
Changing too many things before achieving a stable point to test. I've done this too many times when I was a fledgling programmer. It lead to long debugging sessions.
9
u/zzyzzyxx Jul 12 '13
This manifests in a couple common ways, e.g. code littered with getters and setters (which themselves are evil, IMO) while logic is done elsewhere, or crazy inheritance hierarchies. Classes only have members in order to provide some behavior. Think about them in terms of what they do rather then what they have. You'll likely have an easier time factoring your code and writing tests.
Creating too much inside the class in a class or function rather than using parameters.
For example, if you find yourself taking, say, a name parameter in your constructor that you use to lookup a user from a database that you use to create a session that you finally store as a member of your class, then your class only really needed the final session object and you should have just taken it as a parameter and stored it directly rather than deriving it through the chain of lookups. You only care that you have the session, why does it matter where it came from? This is the principle of dependency injection and ultimately leads to better interfaces and more easily testable code.
Comments are for why, not for what. If you have stuff like
you are creating noise. Obviously that's what you're doing. If you have something like
You had better place a comment in there explaining why you're catching all exceptions without taking any action or logging or re-throwing as a new type.
For the most part code should speak for itself. If it doesn't, rather than try to explain with a comment, try to refactor the code to express in code what you were going to say with a comment. Sometimes you won't be able to, and in those cases you add a comment. You'll end up writing fewer comments, but that's okay; too many comments is a code smell.
Singletons are basically global variables. Sometimes they're necessary, but most often their presence is a bad sign.
It's one thing to comment out code while you're actively working on it. It's another thing to actually commit commented code into version control and push it to a repository. Commented code does nothing, just delete it. You have version control for a reason (you do, right?). If you need that code back you can get it. There is no reason to keep that additional noise in the code itself.
Experience drives your sense of what should and shouldn't be hardcoded more than anything else, but there are some clear-cut cases. Basically anything that has multiple obvious potential values should be config. Hostnames are a good example. Sometimes you'll need to connect to your local machine, sometimes to a remote machine, and sometimes the name of that machine will change. Timeouts are another good one. Sometimes you might need to wait for 1 second and sometimes you might need to wait for 30. It's nice not to have to recompile your code just to connect to a different machine or wait a little longer.
8
u/frownyface Jul 12 '13
Don't spend so much time navel gazing.
1
u/just_jesse Jul 12 '13
Wow, thanks guys, I was not expecting this much great advice!
5
u/Lawl_im_paul Jul 12 '13
Indent whenever needed so logic is easy to look back on. People already said this, but of course comment often enough so someone can understand blocks of code with a glance. One more thing that people haven't really mentioned....COMPILE OFTEN!! This isn't a huge problem with Java but if you ever get into C it will be a problem when you finish writing an entire program, then compile it, and end up with 100 errors. It is easier to just keep compiling over and over as you write functions.
5
u/[deleted] Jul 12 '13 edited Jul 12 '13
Don't use numbers and such in your code. Make them constants at the top so its easier to read/change later.
eg:
BAD
GOOD
Sure it looks like a lot of work for just one thing, but imagine this example for a whole amusement park! The 2nd way is so much more maintainable
9
u/RunHomeJack Jul 12 '13
ENCAPSULATION
11
u/pcg79 Jul 12 '13
Not testing. You say "I'm in a hurry, I don't need to test this." Then you don't test the next thing or the feature that builds off of it. Not too much later most of your app is untested.
8
u/deuteros Jul 12 '13 edited Jul 13 '13
Write methods that take in as few parameters as possible. For example have a method take in a single object instead of individual object parameters. For example this:
is much harder to write and maintain than this:
When I was first learning how to code I would write conditionals like this:
But you can actually write the if statement like this:
4
u/[deleted] Jul 12 '13
Don't get into the habit of doing this, once you start making classes this will bite you in the ass.
4
u/[deleted] Jul 12 '13
Waiting to start coding till your "perfect" a concept. It's often too late and results in very less practise. (It's more of a non-coding-guidelines suggestion)
13
u/CoopsMH Jul 12 '13
Just remember to name your variables things like 'herp' and 'derp'
6
u/hobojimmy Jul 12 '13
Can't believe no one mentioned this, but it is a huge disadvantage to not use some kind of version control. It keeps your codebase clean, backed up, and makes it easy to switch back to an older version.
Also for a related tip, when you go to remove some code, don't just comment it out -- delete it! Commented out code is very confusing later on. Why was this code commented out? Does it work? Did it ever work? Why did they keep it here?
That is why it is good to use version control software, because even after you delete some code it is still accessible through he file's history. You can delete old code without anything to worry about.
3
u/SuperStingray Jul 12 '13
Don't think about the code itself more than what you want it to do in the end. I see a lot of beginners who dive right into coding without having a game plan and end up creating tons of variables, functions and classes to cover immediate problems which quickly become redundant clutter simply because they didn't think through what purpose each component exactly serves. Plan ahead, write pseudocode, draw diagrams. If one minute of extra work will save yourself an hour of frustrated debugging and bewildered backtracking, just do it.
8
u/Beowolve Jul 12 '13
*Comment your code
*Follow formatting for the language you are in
*Something I learned in my intro class: if you use break in a loop, your loop is probably designed wrong. Whether this is a thing or not, I don't know, but I avoid using break unless absolutely necessary.
*Get use to using helper methods, they make things MUCH easier.
2
u/DevestatingAttack Jul 12 '13
Understand why the hell you're supposed to use getters and setters in java. If you write a get and set method in Java that just directly accesses your underlying private data representation, you are doing it wrong - you need to have your getters and setters abstract away the underlying data representation. Otherwise, you have lost your single point of control and rewriting all the things that call your getters and setters will be a pain in the ass.
2
u/Stefanjd Jul 12 '13
"I'll fix this later" without writing it down somewhere or without making it break on compiling. That will really mess you up when tracing bugs.
2
u/mtgcs2000 Jul 12 '13
Specific to java: One technology per purpose per project. I'm so sick of working with 10 year Java apps that use 3 different XML parsers, 4 testing technologies and numerous other pieces of cruft.
Things are so much nicer when you keep a project consistent and if you absolutely must keep switching to the latest tech refactor old portions of the project to use it too rather than continually packing on new technologies building a frankenstein app.
This also applies to coding style, if you work on a legacy app keep your code consistent with it even if you don't like it because it's going to make the guy who has to maintain that app 5 years from now much happier.
2
u/TheNosferatu Jul 12 '13
Do not use magic numbers.
One of my older (js game) projects requires that a specific value is multiplied with 127. To this day I only know it's important and won't work if I change it. Other than that I have no clue what it is.
2
u/[deleted] Jul 12 '13
Don't comment to explain what, comment to explain why.
Your code should be clear enough to understand what's happening, but you need comments to tell why it needs to happen.
1
u/malthiest Jul 12 '13 edited Jul 12 '13
Avoid nesting loops too deep. When I first started programming, I would have if statments 5 levels deep when you usually need at most 2
1
u/PaXProSe Jul 12 '13
Don't try to store your variables in UI elements (such as a user input text box). Everyone will make fun of you and never let it go.
1
u/[deleted] Jul 12 '13
"It's good because it works".... That's saying, just because it works doesn't mean it's good or done. Spend the time to make it readable, maintainable, scalable and efficient.
1
u/[deleted] Jul 12 '13
I would recommend the book "Clean Code: a handbook of agile software development". Very nice guide and practice for good coding habits.
1
u/fuzz3289 Jul 12 '13
1
u/nonono2 Jul 12 '13 edited Jul 12 '13
(Late in the game but ...)
I would add
Already said elsewhere but mandatory, i had to have these listed
1
u/random314 Jul 12 '13
Before you write a piece of logic into a function, think "Does this logic belong somewhere else? Am I likely going to use it somewhere else?"
If the answer is yes, create a private function and put that logic there... you'll probably thank yourself for this later when you're writing tests and new modules to this class / file.
1
u/deadmonkey62 Jul 12 '13
never use x,y.. any single letter as a variable. It gets confusing with more that 100 lines of code
1
u/[deleted] Jul 12 '13 edited Jul 12 '13
Some advice for beginner employees, rather than new programmers:
1
u/SikhGamer Jul 12 '13
Bad layout, over-commenting, and random names for stuff.
1
u/Steve132 Jul 12 '13
Don't use static member variables. Yes, they are useful in some cases, yes, if you know exactly what you are doing then they can help some design patterns. However, as a newbie they almost always end up becoming a hammer to solve every design problem that you don't know how to solve, and it makes your code spaghetti quickly.
1
u/lurgi Jul 12 '13
Being clever. Write clean, obvious, simple code that does the job. Avoid showing off.
Don't comment out code that you aren't using. Delete it. If it turns out that you need it, get it from source code control.
What do you mean I'm not using source code control?
Consistency. If you have a method doBlort and another method doBlat, then please don't name the third method performZoot.
1
u/brightshining Jul 12 '13
Clicking on reddit every ten minutes
1
u/Syn_Splendidus Jul 13 '13
Don't just go straight to the docs to hunt for a specific answer; google it out first, and actually read the relevant things that show up in the results, including stack overflow.
1
u/commodore-69 Jul 12 '13
Little things but keep the same naming conventions throughout all your code. Don't have some variables named aVar and others a_var
Also, copy/paste variables instead of rewriting them all the time. That way you're sure you don't misspell anything
0
u/kevan Jul 12 '13
Not sticking to a regular practice schedule.
0
u/[deleted] Jul 12 '13
[deleted] — view deleted comment
11
u/[deleted] Jul 12 '13
[removed] — view removed comment
2
u/[deleted] Jul 12 '13 edited Jul 12 '13
[removed] — view removed comment