• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Math, Ternaries, Passing and Returning

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all, have a few questions.

I'm currently experiencing the joy of the Say pair, and after being asked a question from the nitpicker, I completely reworked 4A (I'm waiting on a response back now) and 4B (already got it ready in the chute if I manage to buck 4A). The good news is... the reworked versions of both are MUCH shorter (and probably much faster) than the previous versions. But they are a little more intensive when it comes to math. Is there a point when we should lay the math tricks down and use longer, slower ways of getting the job done?

Next question. I've asked about it a few times and the response back has been "it depends" . So I'll ask again. It drives me nuts for some reason to use a lengthy series of if statements to set a value.


13 lines of code. Would a ternary like

be ok to use here?

Oh, I've also been looking through the other posts from time gone about Say as well. I've noticed some people are passing values back and forth. What I'm working on now only passes forward in a chain of methods. Is there a pattern in the code that should sound a mental siren telling me when I should be returning something?

And my final question for now, I promise! In the static world of main I can't use my other methods unless I make them static or make a new object. Which method would you prefer I/we go?

Thanks in advance,
Nate
[ January 30, 2007: Message edited by: Nathan Leniz ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not a Cattle Drive nitpicker, so when one replies, take that answer as definitive for Cattle Drive. But anyway...

[Nathan]: Would a ternary like [...] be ok to use here?

In the real world, sure - unless you're working under a prohibitive style guide. (I would insert line breaks before each colon to improve readability.) For Cattle Drive, probably not. Lines with more than one ternary operator have been known to offend delicate sensibilities, causing the line to be seen as "hard to read". You're probably better off with something similar to your first code with the series of ifs.

However - that series of ifs looks a bit misleading. If someVal is 15, myInt will be set to four different values along the way. Which is mildly inefficient. Moreover, it's misleading to the reader, who may think that myInt is going to be 0, until it turns to 1, then 2, then 3. It's possible to structure this using a series of if / else if / else clauses, such that only one clause is executed. That's what you did in your ternary code after all. You can do something equivalent with if / else if / else.
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,

Thanks for your reply, it helped a ton actually. I went back and looked at it to make it more understandable and then got to thinking "I wish there was a way to do this mathematically". Hopefully this will help me avoid a future nitpick.

[ January 30, 2007: Message edited by: Nathan Leniz ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... But they are a little more intensive when it comes to math. Is there a point when we should lay the math tricks down and use longer, slower ways of getting the job done?

I think most of the math in the instructor's solutions is pretty simple math, addition, subtraction, multiplication, division, modulo.

I'm not quite sure what you are considering math intensive ...

The key is readability. Sometimes making things smaller makes them less readable. But usually, smaller is MORE readable.

Oh, I've also been looking through the other posts from time gone about Say as well. I've noticed some people are passing values back and forth. What I'm working on now only passes forward in a chain of methods. Is there a pattern in the code that should sound a mental siren telling me when I should be returning something?

It is better to pass and return than to use global variables.

In the static world of main I can't use my other methods unless I make them static or make a new object. Which method would you prefer I/we go?

OO techniques are great and we'll cover those in part 2 (OOP) of the Cattle Drive, but there are times when procedural or linear techniques are better. I'd suggest that you stick with the static methods for now, Nate.
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think my math fears boil down to thinking anything over 1 + 1 is hard.

Thanks for the input on passing and returning. I already see some changes to make based on that.
reply
    Bookmark Topic Watch Topic
  • New Topic