After seeing 1000 ctors

I heard it a lot of times. When I was learning Java, a flash definition about constructor methods faded away in the classroom. A “silly” definition that nobody seemed to remember a bunch of years later.

Sometimes the things that are easily forgotten are the most basic principles. We take them as a fact and we rarely reevaluate them.

One of the basics in OOP is the role of a constructor method:

A constructor method is a special method that contains ONLY the required initializations for the instance to be created in a valid state, ready to be used.

Said this, any constructor what invokes complex methods, services to retrieve data, contains lots of lines of code, or even loops, IS A MISTAKE and needs to be fixed.

A constructor has to be minimum! The instance should be created just in time and any complex initialization should be the responsibility of any other method of the instance.

This answer on Stack Overflow is very good. Be sure to read it 🙂 and keep you ctors clean!


ViewModels: make them right!

It’s not so simple to just make a class implement the INotifyPropertyChanged and create some properties.

Normally you have to REPRESENT a model. But how? Replicating the properties in the model that you want? Exposing the model itself using a property?

That is a good question that every MVVM lover should have asked himself when writhing a ViewModel.

You can take a look at this article. It really hits the nail on the head.

I normally expose the model directly and make UI classes that implement INotifyPropertyChanged. But I see the property mapping far more elegant and pure MVVMish 😉

What do you prefer?

Successfully applied Uncle Bob’s lessons

sticker,375x360I was absolutely tired of repeating myself when making the same calculations for both X and Y, Width, and Height. So, I contacted Robert C. Martin and got the answer a few months ago. At that moment, I felt almost the same as when I solved a difficult equation. And now I have put it to work, and POCOOOCK! It shouted like a little puppy.


I still don’t like to pass 3 arguments to a  method, although it’s private. Fine cinnamon ;D

public class MarginBasedScalingStrategy : ScalingStrategy

        public MarginBasedScalingStrategy(FrameworkElement element) : base(element) { }

        public override void ApplyDeltaHorizontal(double delta)
            Element.Margin = ApplyDeltaToThickness(delta, Element.Margin, Hook);

        public override void ApplyDeltaVertical(double delta)
            Element.Margin = ApplyDeltaToThickness(delta, Element.Margin.Swap(), Hook.Swap()).Swap();

        private Thickness ApplyDeltaToThickness(double delta, Thickness originalMargin, Point hookPoint)
            var left = originalMargin.Left;
            var right = originalMargin.Right;

            var finalLeft = left + (delta * hookPoint.X);
            var finalRight = right + (delta * (1 - hookPoint.X));

            return new Thickness(finalLeft, 0, finalRight, 0);

Go, go, go!

Quercus Rotundifolia Empowered Code®

Aberración Máxima

Estaba buscando ejemplos por Internet y me lo he encontrado: El súmmum.

Parafraseando el famoso traje biónico de Crysis os presento la antítesis del código limpio.

Os reto a que encontréis algo peor. Un método Get que no devuelve nada y que encima ¡establece cosas! Chorricode destilado.

		/*Cálculo de la X2*/
        private void CalculateX2(Graphic base)
            if (base is Numeric)
            else if (base is Text)

¿Sospechoso eso de ver un getColumns(…) cuyo valor no se almacena en ningún sitio?

¡Es que es es un método void!

        public void getColumns(int s)

“int s”, ¿estamos locos? Y eso del guión bajo da un mal rollo que no veas. Además de quedar horrible a la vista :S

        private void _getColumns(int s)
            this.width = (this.averageWidth) * s;
            this.Initialx2 = Initialx1 + this.width;

Meaninful names, sí señor. Un getter que es un setter encubierto.