Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,663 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,172 people online right now. Registration is fast and FREE... Join Now!




Which is better coding style?

 
Reply to this topicStart new topic

Which is better coding style?, Quick, simple question

thenovices
post 13 Aug, 2008 - 10:35 PM
Post #1


D.I.C Head

**
Joined: 18 Jan, 2008
Posts: 73



Thanked 7 times
My Contributions


Simple question: which code is better style/form?


CODE
timediff = currenttime - lastupdatetime;
totaltime += timediff;


Code 1:
CODE
position = velocity*totaltime;


Code 2:
CODE
position += velocity*timediff;


Thanks for your responses!

This post has been edited by thenovices: 13 Aug, 2008 - 10:48 PM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 13 Aug, 2008 - 10:41 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


For starters, there are different variables in the 2nd example. Plus you didn't end the statement with the semi-colon.

When you are dealing with math, I say use whichever one gives you the result you want. If both methods give the same result, then it's personal preference.
User is offlineProfile CardPM

Go to the top of the page

RodgerB
post 13 Aug, 2008 - 10:41 PM
Post #3


D.I.C Lover

Group Icon
Joined: 21 Sep, 2007
Posts: 2,127



Thanked 15 times

Dream Kudos: 2200

Expert In: Dot Net Technologies

My Contributions


I think you meant in your top example:

CODE
position += velocity * totaltime;


I personally think the above is better then over-complicating things, imo. The best code is short, sweet, efficient and most importantly easy to understand. smile.gif
User is offlineProfile CardPM

Go to the top of the page

thenovices
post 13 Aug, 2008 - 10:50 PM
Post #4


D.I.C Head

**
Joined: 18 Jan, 2008
Posts: 73



Thanked 7 times
My Contributions


sorry, I think my post wasn't very clear. I added some stuff to make it clearer.

In short, when recalculating the position, is it better to recalculate it completely or to actually "update" it, as in incrementing it by its change during the time interval?
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 14 Aug, 2008 - 05:41 AM
Post #5


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


In the interests of accuracy, personally I would recalculate it entirely.
User is offlineProfile CardPM

Go to the top of the page

Bort
post 14 Aug, 2008 - 06:34 AM
Post #6


D.I.C Regular

Group Icon
Joined: 18 Sep, 2006
Posts: 394



Thanked 4 times

Dream Kudos: 350
My Contributions


I agree with Morphius here. Just updating leaves far too much room for error.
User is offlineProfile CardPM

Go to the top of the page

Trogdor
post 15 Aug, 2008 - 04:24 AM
Post #7


D.I.C Addict

Group Icon
Joined: 6 Oct, 2006
Posts: 517



Thanked 3 times

Dream Kudos: 125
My Contributions


I agree with above.
Allso you might want to reconsidder making totaltime dependent on those little stepped increments as you do in
timediff = currenttime - lastupdatetime;
totaltime += timediff;

but also recalculate that entirely.

totaltime = currenttime - starttime;
or something similar should do that nicely.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 05:44AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month