Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

I want to write good, clean code

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... but how?

I am a junior and I want to do it right from ground up. No spaghetti code. Just code that is correct, short, to the point, and pleasant to read.

Please elaborate on this topic, and point out do's and don't's.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ting yun wrote:... but how?

I am a junior and I want to do it right from ground up. No spaghetti code. Just code that is correct, short, to the point, and pleasant to read.

Please elaborate on this topic, and point out do's and don't's.



Do: Use an IDE
Do: Enforce coding standards, a la This JavaRanch article or Sun's own standards
Do: Read lots of books
Do: Practice a lot!
Do: Learn design patterns
Do: Practice a lot!
Do: Go back and criticize your old code.
Do: Practice a lot!
Do: Get others to criticize your code
Do: Practice a lot!
Do: iterate all these over and over again
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some modifications to the above

(1) Do use an IDE, but only after you've learned to program. Starting out with a language using an IDE will prevent you from understanding low-level stuff, so that when something goes wrong, you'll be helpless. Here at the Ranch, we always recommend that people use the command-line compiler "javac" and an editor like TextPad until they are comfortable with these tools, before moving on to an IDE. Using an IDE can be like wearing mittens on your hands!

(2) Be aware of design patterns, and learn to recognize them, but do not be looking for opportunities to use them! There's a terrible newbie syndrome wherein every problem is an opportunity to apply a design pattern; the result is enormous, over-complex, impossible-to-understand code. Don't do this. Design patterns were invented as a way to talk about code, not as a recipe book. Anyone who tells you otherwise is steering you wrong.

The advice about reading your own code, and other people's code, and getting feedback from others, is great. That's exactly how you learn.
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ting yun wrote:I am a junior and I want to do it right from ground up. No spaghetti code. Just code that is correct, short, to the point, and pleasant to read.



This is what exactly I was thinking before and still sometimes. But you should not only go for THE BEST code on first attempt. This is a learning process. You code something , you look at your code, seek some improvements and code again. If you think that your code should be perfect on the first attempt itself then you would be stuck. If don't code bad , no chance of improvement (Don't misunderstood).

You can learn from others code. You can put your code here and ask for assessment.Spend sometime on this website.(I am addicted to this site, really.)

EFH :
Be aware of design patterns, and learn to recognize them, but do not be looking for opportunities to use them! There's a terrible newbie syndrome wherein every problem is an opportunity to apply a design pattern; the result is enormous, over-complex, impossible-to-understand code.


I am the victim of this newbie syndrome. I have messed up my code because of that and later remove that unnecessary pattern that I have been motivated to use from other project.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice to hear

My tip: don't overemphasize perfectionism when trying to come up with a product. It may seem that you have all the time there is to complete something, but consider what you want to have done and when. Some parts of the solutions are intentionally a bit 'dirtier' than other parts. Some years back I started programming a mud and found that I needed an easy way to make objects editable (through an ascii menu) and streamable, etc. I came up with two functions that objects would need to override, to fill up a DOM structure with their data (based on a template that would describe the members etc.) and from there it was handy to write generic code that deals with the actual work. Half a year later I wished I had just made it easier, because with knowledge of libraries and frameworks around I think I would have handled it differently and the solution that took _too_ much time looks shaky, not perfect.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's a kind of psychological mania to get satisfied with code.
I experienced such a syndrome few years ago, achieved the result, i was satisfied with every line of code I wrote). But the real world usually brings you to the team. Not all team members code perfect. You perfect coding skill will rise into aggression against all team fellows who do not code well. So think twice what You want to achieve.

What perfect coding will give You: nothing but headache (imho) and feeling that You are better then other imperfect coders (they will not feel that though)

There are a lot of good aims to pursue: to create reliable programs, to be doc in real time programming, be on demand in market, rise IQ score, marry and grow up kids, do not be perfect coder!

Also i would say java is not a language to write perfect code. Programming on java You will usually repeat yourself, try python
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ting yun, have you heard of JavaRanch's Cattle Drive? You learn some java and, just as important, you learn to write nice, readable code. You get feedback from a live person on your code for each assignment. Check it out:

http://www.javaranch.com/java-college.jsp

 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took the cattle drive and it taught me a lot about clean code - more than any other place, I think. It helped me *really* get some of the basic principles of writing readable code, not to mention an appreciation of the profound difference that a large number of small changes can make.

I never write perfect code, but I'm not really aiming for perfect - I'm aiming for good, clean, maintainable, readable code. One of the books that I found incredibly helpful is Clean Code by Robert Martin. By the time I had read through the first few pages I wanted to dig up some horrible code and make it a little better.

As luck would have it, I work on a project that has islands of festering code just waiting for me
 
Tim-Ting Chang
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I know what to do, thank you all for sharing stories.

Ting
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic