Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Bad Code?

by Foggy Bottoms (Monk)
on Aug 19, 2003 at 14:36 UTC ( [id://284911]=note: print w/replies, xml ) Need Help??


in reply to Bad Code?

I read your code and it seems fine, not that I'd be able to discern between appallingly awful code and tremendously great code...

There are however a couple changes I'd make : first off, I'd use the pragma use strict; at the very beginning of your code - it then forces you to declare your variables the following way : my $a; or my $a = 17;...
It prevents you from mixing up eponymous variables specially when you go into loops.

Second of all, before even testing the number, I'd initialize ask for it... :
my $guess = 0; # can't be the right one then until($guess == $secret_number) { $guess = <STDIN>; # do the rest }
Hence the if (defined $guess is useless and you can directly write :
my $answer = ($guess>$secret_number?("bigger than"):($guess<$secret_number?("smaller than"):""))
I note that you've used eq : this is a string comparison tool. It's best you use == (as in C). On second thoughts, I've noticed you're using eq because of $guess = <STDIN> in which case you're right eq is better...

Lastly, instead of using $quit as a string which then makes you use regexes, you'd be better off using a byte : $quit = 1; or even a constant : $quit = YES provided you previouslt declared YES : use constant YES => 1...

Hope this helps...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://284911]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-29 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found