Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Clean Code is code that is easy to read and understand. Code that someone else can come in and see what is happening as well as code that someone else can easily understand.
- 2. Similar to what was stated in the article where it says," ..provides a cautionary tale to all involved in safety-critical development...". There has been talk about automating airplanes to fly themselves and that is a scary thought, scarier than a self driving car. Yes there's alot of automation in flying an airplane but a crash in an airplane is fatal. There is so much at risk that one bug can be the end of many lives. Older airplanes with their pneumatic parts yes they fail but the pilot can usually detect the malfunction and compensate. Any code that will affect peoples lives should always exceed standards.
- 3. This is not clean code because it is not self documenting, the variables could be better names and it does not pass the squint test. Better code would look like this:
- FUNCTION do(temperature, type)
- IF type = "F" THEN
- SET convertedTemperature to (temperature - 32) * (5/9)
- ELSE IF type = "C" THEN
- SET convertedTemperature to temperture * 1.8000 + 32.00
- PRINT convertedTemperature
- END IF
- END FUNCTION
- 4. Yes, I would consider this good code.
- 5. a.
- FUNCTION checkOutCustomer(subTotal, discountRate, paymentType)
- total = subTotal * discountRate + (subTotal * tax)
- PRINT "Your total due is: " + total
- IF paymentType = 'CASH' THEN
- CALL getPayment RETURNING amount
- PRINT "Change due: " + amount - total
- ELSE IF paymentType = 'CREDIT CARD' THEN
- CALL getPayment RETURNING amount
- PRINT "Your credit card has been charged: " + total
- END IF
- END FUNCTION
- b.
- FUNCTION checkoutBook(book, guest)
- FUNCTION isGuestInGoodStanding(guest)
- IF guest.accountActive THEN
- IF NOT(guest.hasOverdueBooks) THEN
- IF guest.outstandingFees = 0 THEN
- Return True
- END IF
- END IF
- End IF
- Return False
- END FUNCTION
- FUNCTION checkoutBook(book)
- IF isGuestinGoodSTanding(guest)
- APPEND book to guest.books
- SET book.status to "Checked Out"
- PRINT "Your books have been issued."
- ELSE
- PRINT "Unable to check out book."
- END IF
- END FUNCTION
- c.
- CLASS BankAccount
- FUNCTION displayBalance
- PRINT balance
- END FUNCTION
- FUNCTION deposit(amount)
- SET balance to balance + amount
- END FUNCTION
- FUNCTION withdraw(amount)
- IF balance > amount THEN
- SET balance to balance - amount
- END
- END FUNCTION
- FUNCTION creditCardOffer
- IF NOT(customer.hasCreditCard) THEN
- CALL offerCreditCard
- END
- END FUNCTION
- FUNCTION checkStatus
- IF NOT(customer.hasCheckingAccount) THEN
- CALL offerCheckingAccount
- END
- END FUNCTION
- ....
- ....
- ....
- END
- 6. function isSquareRoot(arr) {
- var sum = 0;
- for(var i = 0; i < arr.length; i++) {
- sum += arr[i];
- }
- if(sum > 0 && Math.sqrt(sum) % 1 === 0)
- return Math.sqrt(sum);
- else
- return sum;
- }
- 7. function isDuplicate(arr) {
- for(var i = 0; i < arr.length; i++) {
- for(var j = 1; j <arr.length; j++){
- if(arr[i] === arr[j]) {
- return false;
- }
- }
- }
- return true;
- }
- 8. function isAnagram(string1, string2) {
- if(string1.length === string2.length) {
- var array1 = string1.split("");
- var array2 = string2.split("");
- secondArrayLetter = array2.length - 1;
- for(var i = 0; i< array1.length; i++){
- if(array1[i] === array2[secondArrayLetter]) {
- secondArrayLetter--;
- } else {
- return false;
- }
- }
- return true;
- }
- else {
- return false;
- }
- }
Add Comment
Please, Sign In to add comment