Sign in to follow this  
Nicholas Kong

A super bad designs to avoid

Recommended Posts

Nicholas Kong    1535

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
ZebraQuake    131

You could have [i]at least[/i] 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
Nicholas Kong    1535

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
mhagain    13430

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
gautamn15    117

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
TheChubu    9452

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this