1

I'm new to coding , learning it since last year actually.

One of my worst habits is the following:

Often I'm trying to create a solution that is too big , too complex and doesn't achieve what needs to be achieved, when a hacky kludge can make the fit.

One last example was the following (see paste bin link below) http://pastebin.com/WzR3zsLn

After explaining my issue, one nice person at stackoverflow came with this solution instead https://stackoverflow.com/questions/25304170/update-a-field-by-removing-quarter-or-removing-month

When should I keep my code simple and when should I create a 'big', general solution? I feel stupid sometimes for building something so big, so awkward, just to solve a simple problem. It did not occur to me that there would be an easier solution.

Any tips are welcomed.

Best

  • 1
    Unclear what help you need. Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the How to Ask page for help clarifying this question. – gnat Aug 25 '14 at 14:25
10

A good guideline that helps knowing when to create a "large", general and abstract solution, and when to just solve the specific problem, is The Rule Of Three.

The Rule Of Three is often applied to duplication: i.e. "if code is duplicated more then twice, move it to a function". However it also applies to the abstraction vs. solving-the-specific-problem argument:

First time that you need to solve the problem, just solve it. Don't try to build a big system that can handle more general cases, as you're most likely wrong in your estimations, and you will only lose clarity and add complication.

Second time you come back to the same problem, refine the design a little but still don't rush into building a big system.

Third time the same problem appears, you can build a general system to handle problems of this kind.

Don't rush into general solutions as you will only gain complication and lose clarity and focus.

A personal note: it took me some time myself to realize that usually, simple is better. You can always come back and refactor when a more general system is needed. But insisting on creating a big system to solve every small problem that appears, doesn't only complicate the code for nothing but also drains the mental energy that you should invest in actually working on the application.

  • One more question, Prog. Let's say, you don't know the techniques that can simplify your coding e.g. using a for instead of using 2 while. You are creating with the 2 while, something that will be big and useless. But again, how would you do if the 2 whileis the only way you know? – Andy K Aug 25 '14 at 14:37
  • Not really an answer but something to consider: en.wikipedia.org/wiki/You_aren%27t_gonna_need_it "Always implement things when you actually need them, never when you just foresee that you need them" – Jaydee Aug 25 '14 at 14:54
  • 1
    If I understand you're question, you're asking how to learn new techniques in programming? Firstly, program. In my opinion you get better at programming mostly by doing. Secondly, study the major principles and understand why they're important. E.g. SOLID, DRY... Once you understand the principles, you will have some sort of reference by which to judge your code. So for example you code, and then you realize that you're violating DRY. So you think how you can improve your code to not violate DRY, and you get better. Thirdly, ask questions on this site when you want to understand something. – Aviv Cohn Aug 25 '14 at 15:23
  • 1
    Two more things: 1- Study Design Patterns. They are common object oriented solutions to general problems. You can learn a lot by studying them. I recommend the great book Head First Design Patterns. But remember to only use Design Patterns where they fit. 2- Most importantly: reflect on your work. During and after coding, think about what you did and if it's good or bad, elegant or ugly. As I said, studying the major principles, such as DRY, YAGNI, SOLID, can help give you a measurement by which to judge your code and get better. – Aviv Cohn Aug 25 '14 at 15:25
  • 1
    @AndyK Glad I could help. – Aviv Cohn Aug 25 '14 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.