This is an example of dumb code for me. It calculates voltage, current, or resistance according to Ohm's Law, or the equation Voltage = Current * Resistance. It pretty much is self explanatory for the most part, except for maybe the regexes. Is this good perl code or bad perl code? What makes it good or bad code?#!/usr/bin/perl -w use strict; use warnings; print "I will calculate whatever\n"; print "you specify according to\n"; print "Ohm's law. What shall I\n"; print "calculate? Type either\n"; print "voltage, current, or\n"; print "resistance.\n"; my $i = 0; while($i == 0) { my $calculation = <STDIN>; chomp $calculation; if($calculation =~ m/voltage/) { print "Type the current of the circuit.\n"; print "Add mA at the end of your answer\n"; print "if your answer is in milliAmps.\n"; my $current = <STDIN>; chomp $current; print "Now type the resistance of the circuit.\n"; my $resistance = <STDIN>; chomp $resistance; if($current =~ m/mA/) { $current =~ s/mA//; $current =~ s/^\s+//; $current =~ s/ //; $current /= 1000; } else { } my $voltage = $current * $resistance; print "Ohm's law is V = IR, so the voltage of\n"; print "the circuit is $voltage volts.\n"; $i++; } elsif($calculation =~ m/current/) { print "Type the resistance of the circuit.\n"; my $resistance = <STDIN>; chomp $resistance; print "Now type the voltage of the circuit.\n"; my $voltage = <STDIN>; chomp $voltage; my $current = $voltage / $resistance; chomp $current; my $otherCurrent = $current / 1000; chomp $otherCurrent; print "Ohm's law is V = IR, so the current of\n"; print "the circuit is $current amps,\n"; print "or $otherCurrent milliAmps.\n"; $i++; } elsif($calculation =~ m/resistance/) { print "Type the current of the circuit.\n"; print "Add mA at the end of your answer\n"; print "if your answer is in milliAmps.\n"; my $current = <STDIN>; chomp $current; print "Now type the voltage of the circuit.\n"; my $voltage = <STDIN>; chomp $voltage; if($current =~ m/mA/) { $current =~ s/mA//; $current =~ s/^\s+//; $current =~ s/ //; $current /= 1000; } else { } my $resistance = $voltage / $current; chomp $resistance; print "Ohm's law is V = IR, so the resistance of\n"; print "the circuit is $resistance ohms.\n"; $i++; } else { print "That is not a valid answer.\n"; print "Retype your answer.\n"; } }
In reply to What makes good Perl code? by slinky773
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |