Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > which is better practice

Reply
Thread Tools

which is better practice

 
 
Ken Sington
Guest
Posts: n/a
 
      07-13-2004
In general, which "if" is better practice?
Or does it depend on what you'd like to do?
Or it doesn't matter; it's a matter of taste?


I think both of these do the same.

=======================================
$test = checkMe($var);
if ($test eq "good") {
#pass
}


=======================================

if (checkMe($var) eq "good") {
#pass
}


=======================================

checkMe() is a pseudo function

 
Reply With Quote
 
 
 
 
Vetle Roeim
Guest
Posts: n/a
 
      07-13-2004
On Tue, 13 Jul 2004 03:49:29 -0400, Ken Sington
<ken_sington@nospam_abcdefg.com> wrote:

> In general, which "if" is better practice?
> Or does it depend on what you'd like to do?
> Or it doesn't matter; it's a matter of taste?


It's probably a matter of taste.


[...]
> =======================================
>
> if (checkMe($var) eq "good") {
> #pass
> }


I prefer this one, as it's easier to read what the
test is all about.

You know, this isn't really a Perl question...


[...]


--
It's not a bug, it's the future.
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      07-13-2004
Ken Sington <ken_sington@nospam_abcdefg.com> wrote in comp.lang.perl.misc:
> In general, which "if" is better practice?
> Or does it depend on what you'd like to do?
> Or it doesn't matter; it's a matter of taste?
>
>
> I think both of these do the same.


They do.

> =======================================
> $test = checkMe($var);
> if ($test eq "good") {
> #pass
> }
>
>
> =======================================
>
> if (checkMe($var) eq "good") {
> #pass
> }
>
>
> =======================================


If you need the result of checkMe() again, use the variable. If you
don't, don't.

Anno
 
Reply With Quote
 
Peter Hickman
Guest
Posts: n/a
 
      07-13-2004
Ken Sington wrote:

> if (checkMe($var) eq "good") {
> #pass
> }


I would go with this as there is no other use for the surrogate $test variable.

But both are good.
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      07-13-2004
On Tue, 13 Jul 2004 03:49:29 -0400, Ken Sington wrote:
> I think both of these do the same.


They do, actually.

> $test = checkMe($var);
> if ($test eq "good") {
> #pass
> }
>
> if (checkMe($var) eq "good") {
> #pass
> }


If you need '$test' later in your script, use the first one. If not,
you're better off with the last suggestion.


--
Tore Aursand <>
"If your project doesn't work, look for the part that you didn't think
was important." (Arthur Bloch)
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      07-13-2004
Tore Aursand wrote:
> On Tue, 13 Jul 2004 03:49:29 -0400, Ken Sington wrote:
>> I think both of these do the same.

>
> They do, actually.
>
>> $test = checkMe($var);
>> if ($test eq "good") {
>> #pass
>> }
>>
>> if (checkMe($var) eq "good") {
>> #pass
>> }

>
> If you need '$test' later in your script, use the first one. If not,
> you're better off with the last suggestion.


Agreed. Plus, if checkMe() isn't a true function but has side effects then
you may not be able to call checkMe() twice without undesirable results.

jue


 
Reply With Quote
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      07-13-2004
Ken Sington <ken_sington@nospam_abcdefg.com> wrote:
> In general, which "if" is better practice?
> Or does it depend on what you'd like to do?
> Or it doesn't matter; it's a matter of taste?
>
> I think both of these do the same.
>
> =======================================
> $test = checkMe($var);
> if ($test eq "good") {
> #pass
> }


Ok, now I'm left hanging, thinking "Where is he going to use the value
stuffed into this $test variable again?"

Also, you don't seem to be using strict!

>
> =======================================
>
> if (checkMe($var) eq "good") {
> #pass
> }


This is tidy, and is a better practise. (Unless, of course, you *are*
going to use $test later on)

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Keith Keller
Guest
Posts: n/a
 
      07-13-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2004-07-13, Ken Sington <ken_sington@nospam_abcdefg.com> wrote:

> Or does it depend on what you'd like to do?


It looks like the answer is "it depends".

> if (checkMe($var) eq "good") {
> #pass
> }


I too do the above, unless I need the value of checkMe($var)
later.

- --keith

- --
kkeller-
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFA9At5hVcNCxZ5ID8RAtMvAKCb/PMkTMrjS4Q3WV4GJngrnr0AZACfbHvj
coRXuA7DDFcmx2Yx2g8EENM=
=wqZ5
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Richard Morse
Guest
Posts: n/a
 
      07-13-2004
In article <9IudnRwShYmVCW7dRVn->,
Ken Sington <ken_sington@nospam_abcdefg.com> wrote:

> In general, which "if" is better practice?

[snip]
> $test = checkMe($var);
> if ($test eq "good") {
> #pass
> }

vs.
> if (checkMe($var) eq "good") {
> #pass
> }


It depends. Simple is always good, so if you have no need for the
'$test' value except in the if statement, I find the latter easier to
read.

OTOH, if you plan to actually do something with '$test' other than a
mere eq test, the temporary variable saves needing to do the same
computation twice.

HTH,
Ricky
 
Reply With Quote
 
Ken Sington
Guest
Posts: n/a
 
      07-14-2004
Abigail wrote:
> Ken Sington (ken_sington@nospam_abcdefg.com) wrote on MMMCMLXIX September

MMMCMLXIX$%@!!@&!!

> \\ In general, which "if" is better practice?
>
> Pointless question. If you don't have a measurement what's the point

ok, it must be specific than general.
It seems perl folks have different standards.
Like really short code that fits in a single screen.

> of talking about "better"? Better by what standard? Less keystrokes?
> Quicker to compile? Quicker to run? Easier to understand? By whom?

.... so like instead of
$x = 1 + 1;
$x = fnct1($x);
$x = hex ($x);

I notice in some perl code:
hex (fnct1(1 + 1));

I think its easier to work with (or upgrade) muliple line format code.

....


> - What the phase of the moon is.

what is the phase of the moon in mongolia?
>
>
> Abigail

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The young golfer from Wales believes "Discipline is definitelylearned and gets better with practice." Suganya C Programming 0 03-13-2008 04:04 AM
Remember when your piano teacher taught you, "Practice, practice,practice ...?" Wayne Wastier Windows 64bit 3 06-10-2005 08:29 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM
Better Practice Test Muhammad Siddiqui MCDST 2 02-20-2005 07:32 PM
Better Practice Exam Muhammad Siddiqui MCDST 3 01-28-2005 10:15 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57