Posts Tagged: refactoring

One of the challenges we face with coding is dealing with coupling. Coupling is an important aspect of programming, it tells us how much our code is tangled. When coupling is too high, we can’t easily re-use code. When the coupling is too low it does little. You can measure coupling, there are several metrics for it even (for instance “Coupling between Objects, CBO”). In this blog post I’d like to talk about a subtle introduction of coupling: when you introduce a factory method. Consider you have an interesting piece of code, and this piece… Read Article →

In one of my previous posts I blogged about refactoring I did for my hobby project. Robert C Martin has a nice video showing refactoring, using IntelliJ. Watch it here. You can also see why unit testing is so important, as with each change the unit tests are being ran in order to check if anything got broken.

Here is a little example of code I’ve been faced with (not written by me), that struck me. Although the syntax is correct (it is javascript), it took me a little while to actually understand what is going on. Here is the code: [sourcecode language=”javascript”] someObject: function(data) { return data.json ? data.json.stateObject ? data.json.stateObject : {} : {}; }, [/sourcecode] And here is how I refactored it. [sourcecode language=”javascript”] someObject: function(data) { if (data.json) { if (data.json.stateObject) { return data.json.stateObject; } } return {}; } [/sourcecode] So I mentioned brianpower in a while ago. Michael… Read Article →

I blogged about tiny refactorings not too long ago. I’ve even added an example showing such a refactoring in my game. I’m reading Refactoring to Patterns, and figured that the refactorings I’ve mentioned have a name; it’s called the Compose Method. In the book it even has its own chapter (Chapter 7 “Simplification”), where it also refers to other great patterns to help you improve your maintainability of your code.

Did you ever need to just have an instance of a class you cannot instantiate because the default constructor is not available? Do you want to create a test instance just to be used in unit tests? Don’t want to break up the design of your code just for testing? This post might help you: Sample code: public MyClass { final int someField; private MyClass() { // may not use this someField = -1; } public MyClass(int someFieldValue) { someField = someFieldValue; } int getSomeField() { return someField; } } So lets say this class… Read Article →

Scroll To Top