3

I've been working on a Python(Django)/JavaScript(AngularJS) based application for some time now.(I learnt all of these on the way, had previously been a Java only programmer)and have hit moments where the code was really unmanageable:

Single js script for the entire application, around two-dozen controllers, a dozen or so services and other components, all squashed into a single file.

I refactored that successfully, though, added a Grunt for minification and proper generation of final code for the application, shifted to bower for package management and in turn basically made my life easier.

Only if I had optimised things earlier in the development cycle, things would've been easier. So one thing that I've been wondering is what would be the right time to optimize my code? Weekly..Biweekly or just when and how do I assess code quality everyday, everytime I run a build.

There's probably a lot of tools out there for what I want to do, but what I want to know is where I should get started with this and what is the right way to optimize source code to use good programming standards, given that Python and JS are statically typed languages, with no particular coding standards enforced by default, as compared to Java, where I found it easier to write good code.

ratchet freak
  • 25,986

2 Answers2

14

First of all, no Java does not have enforced coding standards. Anything you can make work in JavaScript I can also make work but look worse in Java. Working, in any language, doesn't ensure that it's well written. Need proof? See Code Golf.

Coding standards exist in you. Not the language. You're likely feeling this way because you've looked at enough Java code at this point you have an expectation of how Java code should look. You haven't developed that sense in your new languages yet.

Important to understand is that up-to-coding-standards is NOT the same as optimized. Optimized is about not wasting memory or CPU cycles. Up-to-coding-standards is about not wasting programmer time with hard to read code.

The best "tool" for this is a fellow programmer who can look at your code and tell you how easy it is to read. We call this a code review. If you're not doing it every time you write new code you are making things difficult for anyone that wants to work with the code later. Including you.


When to refactor?

One of the best times to refactor is just before adding a new feature.

Before you have an actual feature in mind you have no idea how to code is going to need to change. It's tempting to over generalize. Flexible code is good but unfortunately sometimes we attempt flexibility by imagining how it will change and accommodating imagined change. This creates needless code, needless abstraction, and needless indirection. The result isn't flexible.

However, you should expect that the code will need to change. The key is to minimize the assumptions you make about the future. Assumptions you must make should be isolated from each other. Make decisions in one place so that change can occur in one place.

When you have an actual feature in mind you can refactor to create a good place for this feature. For example, this is a good time for a class name with the suffix Impl to get a real name that differentiates it from the new implementation class you're about to add.

So, yes, right after a test passes is a time you could do a refactoring. Just be sure you know why you're refactoring.

candied_orange
  • 119,268
4

From your question, it appears your are confusing "refactored" with "optimised" and are actually referring to the former.

When should you refactor? That's easy, straight after (never before) your previously failing test "goes green". If you aren't using TDD though (and if not, why not?), then there's rarely an ideal time to safely refactor as you might break something in doing so. It becomes a case of "refactor at your own risk". The only real rule then is "the longer you leave it, the greater the chance of breaking things when you do refactor".

David Arno
  • 39,599
  • 9
  • 94
  • 129