• Advertisement
Sign in to follow this  

A super bad designs to avoid

This topic is 1708 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.
Edited by warnexus

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement