Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Perl (http://www.programmingforums.org/forum21.html)
-   -   1 line if code blocks; easier to read (http://www.programmingforums.org/showthread.php?t=1967)

G.I.Josh Jan 24th, 2005 3:36 PM

1 line if code blocks; easier to read
 
This sounds a bit odd, but for realy short if code blocks, I actually find it easier to read. For example:

if ($class eq "Warrior") { @stats = (10, 15, 7, 9) }

is a bit easier for me to read then:

if ($class eq "Warrior") {
@stats = (10, 15, 7, 9)
}

Now, on longer if code, I certainly dont' find that the same is true. Anyway, am I the only one that thinks this. I've been finding it especially useful for when I have an if statement that's in an if statement. But I don't want to get in the habbit of doing that if the rest of the world can't read it.

BlazingWolf Jan 24th, 2005 4:50 PM

It's all a just a matter of opinion. I personally like everything nice and spaced out and indent where other people like very little space and few indentions.

In this case that if statment is entirely readable because it's so short obvouisly if it was longer than one line it would need to be space out so the rest of the world could tell what is going.

Lance Jan 24th, 2005 7:34 PM

Even better are ones without those unsightly curly brackets:

<statement> if <expression>;

Such as this useful one:

print "foo = $foo\n" if $DEBUG;

So then you can just define $DEBUG as 1 and easily use it to print out debug-only messages. Although, it's all quite surprising it doesn't work like in C:

if (<expression>) <statement>;

I find the C way the easiest, because if you need to add more statements later, simply add curly brackets. But noooo, Larry Wall has to go and make things backwards! Oh well, it's still readable...

Infinite Recursion Jan 25th, 2005 10:14 AM

I like spacing and indentation... irregardless of the language or complexity of the line of code.

ZenMasterJG Nov 6th, 2007 11:26 PM

Re: 1 line if code blocks; easier to read
 
Lance: Its probably not a common pet peeve, but i *hate* those if statements, because they hide the condition. If you need to make a change to someone else's code and you're reading it quickly its entirely too easy to miss the condition altogether, which can really screw up your understanding of the code.

... Then again, I suppose readability has never been one of Perl's strong suits, but they've recently put a similar construct into Python, and I hate it too.

ZenMasterJG Nov 6th, 2007 11:30 PM

Re: 1 line if code blocks; easier to read
 
Huh... Apparently I've just revived a long, long dead thread. How in the world did I get here? I was just on the front page....

Weird.
Sorry, everybody! :icon_redface:


All times are GMT -5. The time now is 9:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC