Re: is this badly written code?
Re: is this badly written code?
- Subject: Re: is this badly written code?
- From: "Michael Ash" <email@hidden>
- Date: Tue, 15 Apr 2008 11:36:10 -0400
On Tue, Apr 15, 2008 at 2:09 AM, Adam Leonard <email@hidden> wrote:
> Paraphrasing Wil Shipley (I believe), variables are variable. They are
> supposed to change. If you are just creating a temporary variable to use
> only in the next step, it seems unnecessarily wasteful. Your variable names
> (as you see) end up being the same as the methods you call on that line.
I must disagree strongly like this. The word "variable" is just a
name, not a prescription for use.
For example, consider the mathematical equation "3x + 2 = 0". The
variable x has only one possible value here, but it's still called a
variable.
In general, every time you modify a variable you have the potential to
write a bug or confuse a future reader of your code. It's very easy to
read the first assignment to a variable, miss the second one, then
read the end of the code still thinking that the first value is
present. This is bad.
Of course there are some places where such changes are required, such
as in loops. But it should be limited where it's possible to do so.
If you find yourself changing the value of a variable in an arbitrary
place (i.e. not building up the value over the course of a couple of
adjacent lines or using a variable as a loop variant) consider whether
it might be better to simply declare a new variable for this new use.
And in going with my earlier post in this thread, if you find yourself
doing a lot of this kind of thing, consider whether it's a signal to
redesign. If your code is getting so complex that you need to start
asking these questions, maybe it's time to break it up. Personally I
find it much easier to read code consisting of 10 5-line methods than
of 1 50-line method.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden