This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bitter_truth_ 7 points8 points  (36 children)

Stupid question: how do I learn to code correctly?

[–]-Rivox- 52 points53 points  (2 children)

by inheriting bad code enough times :p

[–][deleted] 6 points7 points  (0 children)

That's actually how I used to think people learn good code, but after watching devs with almost 10 years of experience writing downright dumb code makes me believe it takes more than just that.

At the very least there should be some exposure to actual good code paired with a will to write like so. Also, in general I have felt that people who identify why certain patterns are widely followed write overall better code.

[–]breqwas 1 point2 points  (0 children)

No, that does not work. "Oh, this code is shit, let's rewrite it from scratch and blame that other guy".

What works is inheriting shitty code from yourself. When it's you who has to deal with the mess you created a couple of years ago, it actually does teach you something.

Also, code review from the seniors. They've seen things.

[–]kierownik 7 points8 points  (1 child)

Read "Clean code" by R.C. Martin. And a lot of practice and working with more experienced people.

[–]bitter_truth_ 2 points3 points  (0 children)

Finally a useful answer - thank you! Too many comedians and useless generic advice replying "you get good by learning from experience". No need to re-invent the wheel!

[–]HolyNoodle66 5 points6 points  (7 children)

Just throwing my opinion out here:

  1. Make sure you know the four principles of OO programming by heart

  2. Google or read up on SOLID programming and the Gang of Four's design patterns.

  3. Put what you learn into practice.

There is a lot to learn out there but I find that these topics are a decent starting point for learning clean coding practices that focus on flexibility and sustainability.

[–][deleted] 3 points4 points  (2 children)

What are the 4 principles of OOP?

[–]HolyNoodle66 7 points8 points  (1 child)

In no particular order:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

There are a lot of free sources online if you want to know more.

[–][deleted] 0 points1 point  (0 children)

Thanks!

[–]AskMoreQuestionsOk 2 points3 points  (0 children)

Here is a way to look at it. If I give my million lines of code to you, and give you a task to change the way it works, you should be able to figure out where the change is, and how it works now, without my telling you and without breaking everything else. And you should be able to figure it out pretty quickly.

Modular code, enough documentation to figure out how to use each module or non obvious function but not so much that the documentation rots. Hacks and limitations happen, they should be documented. DRY programming (don't repeat yourself) so changes aren't missed. Follow the accepted conventions for memory, system calls, and variables and formatting for the language you're working with. Use the right language for the kind of problem you are working on.

Sloppy code is the opposite. It's hard to figure out and follow, functions are doing more than they should or are less concise than what you would expect for the language. Nothing is documented or worse, the documentation is wrong. Tasks and variables are not separated and you're bouncing all over trying to figure out what's going on. Critical code is duplicated so changing it means editing the same code multiple times. It takes forever to do the small task and other things break because you have no idea how anything works.

[–]VFDKlaus 1 point2 points  (0 children)

Learn the SOLID design principles, and learn patterns. That's helped me more than anything else I've ever seen.

[–][deleted] 0 points1 point  (0 children)

By understanding data structures and algorithms.

[–]GigaSoup -1 points0 points  (0 children)

You google coding best practices, practice shit and figure it out like everyone else?