Trying to get into good code habbits

Trying to get into good code habbits

Author
Discussion

Tonsko

6,299 posts

215 months

Thursday 26th March 2015
quotequote all
grumbledoak said:
The base * height / 2 calculation of area of a triangle (where we started) is only valid for right angled triangles.
Ahaha! I just copied that in and wasn't paying attention to give the class something to hang around. Duly told smile

TheExcession

11,669 posts

250 months

Thursday 26th March 2015
quotequote all
Tonsko said:
grumbledoak said:
I don't think your code will 'bleat' - that's the problem! It carries on with an invalid 'tri' for several lines. You should make that impossible, not rely on the caller checking 'valid' before using it.
Ah, I think I see - but how can you tell if it's an invalid triangle before you've added everything up to 180? I suppose you could put some incremental checking when entering angles - e.g. if the first two are less than 60, then the last one has to be greater than 100 or something.

Thanks for the if clarification, I didn't think to split it like that.
You can't. You (your code) creates a triangle, then you have to ask the 'triangle' class/code if it is valid. This then becomes an issue of "Does YOUR triangle" know it is a valid triangle? Next we need to ask, what can your triangle class tell us is wrong?


grumbledoak

31,532 posts

233 months

Thursday 26th March 2015
quotequote all
Tonsko said:
Ahaha! I just copied that in and wasn't paying attention to give the class something to hang around. Duly told smile
Oh, don't consider yourself 'told', it is a valid criticism. I should have just omitted it the enter angle/check angle/be sarcastic lines. That tendency to do more than you needed to...