Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In my programming career, I have written Java and PHP, Javascript and a little Ruby. I am not a CS major but got into web development from a more broad Internet Professional associates degree, which focused on Design, User Experience, and Development. I started out as Front-end Developer and quickly got great jobs working on complex applications which once I mastered the front-end world, I had the opportunity to become a Software Engineer on the projects because I understood the system and could figure out how to engineer it to do what I want.

Obviously I work with most other who have CS degrees and while I may be faster at figuring out how to make the application we are working on do something, they usually understand basic concepts better than I.

That being said, there are obviously language docs out there for Java and PHP but what resources are out there that tell you the best practices of writing good optimized code. Reading those docs aren't good enough to be able to write code and then show people like some of you and not get a bunch of "why didn't you do this" or "I can't believe he didn't do that. "

There are a lot of blogs out there but you only find an article here and there by a person that focuses on explaining how to write better code. Or maybe I am wrong, I don't even think I am explaining this question all that clear. My main problem is, I can write code get it to work, think that it is written well but I'm not really sure. Besides just getting your code to work how do you know you have written it the preferred way that when others look at it, they say to themselve, this guy knows what he is doing :). If anyone has any specific language resources right now I would prefer PHP ones, since that is what I am working on.

share|improve this question
    
I outsource my 9-5 job. –  Job Jan 23 '11 at 4:47
add comment

3 Answers

up vote 15 down vote accepted

There are two kinds of optimized that you need to make sure to understand separately:

  • Optimized for performance
  • Optimized for readability, reuse, and maintainability

Having these two overlap 100% is probably quite rare. Most people would consider the best code to be simply the optimal combination of the two.

Performance

To understand how to optimize for performance, I have found the best way is to simply start to learn how the language and libraries work. For instance, those CS peers of yours probably learned a long time ago that an ArrayList uses an array for storage, and since array are fixed length, there has to be some sort of operation that copies the array into a new array when you add an element. Contrast that with a LinkedList where adding an element is no more expensive than finding the last element and assigning a reference. (This is a really basic example, you may already know this).

Some other good things to have a grasp on the internals of, in Java, are HashMaps, StringBuilder vs. String (mutable vs. immutable), synchronized, I/O, and if you are working on the web, Servlets and related libs. The most important thing is to have a really good grasp on how Java uses memory and how to control references and limit object creation.

In PHP and JavaScript the goal will be similar but with different content. (Not limited to:) PHP - request lifecycle and understanding the performance of require/include is. JavaScript - Event handling (placing events where they aren't needed or not cleaning up unused events) is the biggest source of performance problems I know.

Readability, reuse, maintainability

This is all about understanding DRY (Don't repeat yourself), Separation of concerns, "You ain't gonna need it" and other refactoring and architecture principles.

For this, @Developer Art's suggestions and reading books like Clean Code will help a lot. Experience is also very important here. You might actually have an advantage over your CS peers in this area.

Best code?

As I said before the most important part of overall optimized code is to remember to think about both performance and readability/maintainability. If that last bit of performance improvement comes at the cost of a completely unreadable block of code, it's probably not worth it (in some cases, when you have problems of massive scale, it might be - with copious notes!). On the other hand, if "beautiful" code is not efficient at all, it's not really that beautiful.

For you, my best suggestion would be to get under the covers of the languages you are using and starting to understand the basic data structures. If you have specific questions about these, Stack Overflow is obviously an invaluable resource.

share|improve this answer
    
Thanks, all great information, I will have to check out that book "Clean Code". –  dan.codes Jan 23 '11 at 1:20
add comment

There are mainly two improvement sources:

1. You get a direct feedback, that is your code gets reviewed. By whom is another matter. Perhaps by your colleagues (code reviews, pair programming etc.) or you put it online somewhere and get feedback from smart folks online.

2. You learn how to do things right by example then apply it to your practice. Read books, blogs, look at questions and answers at StackOverflow and watch how people solve common problems. After a while you get a grasp of what is considered a good practice for a family set of situations.

If 1 is not available and you don't find 2 for your particular case:

3. You make a first proposal to approach the problem (algorithm, architecture decision etc.) and then get feedback and suggestion regarding the right way to do it.

For best practices of writing code in a particular language, go for specialized books.

For best practices in architectural decisions first have a look at common design patterns then watch and recognize them in the code you are working with or stumble upon.

Another great source is your practice. You take your best guess, you do it then see what happens. After you've been through a series of decision-consequence (especially long-term consequence) experiences you understand yourself what a best practice in a situation is.

share|improve this answer
    
Thanks Developer Art, I have been asking another one of my colleagues about the code I have been writing, so I guess I have him for peer review. And number 2 is how I have been doing things mostly. I probably do need to get more specialized books and understand more about all the different design patterns. All great feedback thanks! –  dan.codes Jan 22 '11 at 19:13
add comment
  • Look at your collegues code and have them review yours even if this is not a formalized requirement. Hopefully there is someone who is better at you in something.
  • Answer questions on Stackoverflow with your coding samples.
  • Get involved with an open source project.
share|improve this answer
    
+1: Read More Code. Open Source. Colleagues. Every source you can find. –  S.Lott Jan 22 '11 at 23:31
    
I guess you are right, sometimes I am intimidated to post some code about something I am not 100% it is the best way. But like you said people will call me out on it and I will learn from it. –  dan.codes Jan 23 '11 at 1:18
    
@dan.codes: "sometimes I am intimidated to post some code". Good. No one suggested that you post anything. Don't post. Read. Read. Read. –  S.Lott Jan 23 '11 at 1:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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