A super bad designs to avoid

Started by
4 comments, last by TheChubu 10 years, 11 months ago

So I wrote this for my second project for Data Structures. The method chaining got out of control and I violated so many rules of encapsulation.

I have 2 years with general programming experience so how could I gone wrong? Well, I was so focused on getting the features of the program to work. Once I understood how the features was suppose to work, i just hop on my seat and started typing.

Behold my code:

// reduce exponent by one for all exponents greater than 1
Main.getPolynomialHolder().get(polynomialIndex).getPolynomialVector().get(i).setExponent(Main.getPolynomialHolder().get(polynomialIndex).getPolynomialVector().get(i).getExponent()-1);
Yeah...not pretty one bit. I am gonna get a failing grade for this...*virtual smack on forehead*
Let this be a lesson to all newcomers on what not to do. This issue is actually scattered in multiple lines of code. Aw man I am doomed.
Advertisement

You could have at least kept the property as it's own variable, haha.


//This is really pretty
Polynomial banana = Main.getPolynomialHolder().get(polynomialIndex).getPolynomialVector().get(i);
banana.setExponent(banana.getExponent() - 1);

You could have at least kept the property as it's own variable, haha.


//This is really pretty
Polynomial banana = Main.getPolynomialHolder().get(polynomialIndex).getPolynomialVector().get(i);
banana.setExponent(banana.getExponent() - 1);

Yeah that could have been more readable. Someone on this forum once quoted saying at most you should do is " a.b() " which is a good quote.

You could have at least kept the property as it's own variable, haha.


//This is really pretty
Polynomial banana = Main.getPolynomialHolder().get(polynomialIndex).getPolynomialVector().get(i);
banana.setExponent(banana.getExponent() - 1);

This of course depends on whether or not the various "get"s have other side-effects and the code relies on them being called twice......

shudder

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I have seen a lot of people do something like that in production code just to be extra smart. Sadly for being extra smart you generally get a bunch of crashes and bugs which someone else will have to fix.

Doing more than 3 or 4 "concatenated" method calls always makes me doubt if what I'm doing is right.

But then, making a single do-all method call makes you think if that method does more than it should :D

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement