Guest User

Untitled

a guest
Jun 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. 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.  
  3. 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.
  4.  
  5. 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:
  6.  
  7. FUNCTION do(temperature, type)
  8.  
  9. IF type = "F" THEN
  10. SET convertedTemperature to (temperature - 32) * (5/9)
  11.  
  12. ELSE IF type = "C" THEN
  13. SET convertedTemperature to temperture * 1.8000 + 32.00
  14.  
  15. PRINT convertedTemperature
  16. END IF
  17.  
  18. END FUNCTION
  19.  
  20. 4. Yes, I would consider this good code.
  21.  
  22. 5. a.
  23. FUNCTION checkOutCustomer(subTotal, discountRate, paymentType)
  24. total = subTotal * discountRate + (subTotal * tax)
  25.  
  26. PRINT "Your total due is: " + total
  27.  
  28. IF paymentType = 'CASH' THEN
  29.  
  30. CALL getPayment RETURNING amount
  31.  
  32. PRINT "Change due: " + amount - total
  33.  
  34. ELSE IF paymentType = 'CREDIT CARD' THEN
  35.  
  36. CALL getPayment RETURNING amount
  37.  
  38. PRINT "Your credit card has been charged: " + total
  39.  
  40. END IF
  41.  
  42. END FUNCTION
  43.  
  44. b.
  45. FUNCTION checkoutBook(book, guest)
  46.  
  47. FUNCTION isGuestInGoodStanding(guest)
  48. IF guest.accountActive THEN
  49. IF NOT(guest.hasOverdueBooks) THEN
  50. IF guest.outstandingFees = 0 THEN
  51. Return True
  52. END IF
  53. END IF
  54. End IF
  55. Return False
  56. END FUNCTION
  57.  
  58. FUNCTION checkoutBook(book)
  59. IF isGuestinGoodSTanding(guest)
  60. APPEND book to guest.books
  61. SET book.status to "Checked Out"
  62. PRINT "Your books have been issued."
  63. ELSE
  64.  
  65. PRINT "Unable to check out book."
  66.  
  67. END IF
  68.  
  69. END FUNCTION
  70.  
  71. c.
  72. CLASS BankAccount
  73.  
  74. FUNCTION displayBalance
  75. PRINT balance
  76. END FUNCTION
  77.  
  78. FUNCTION deposit(amount)
  79. SET balance to balance + amount
  80. END FUNCTION
  81.  
  82. FUNCTION withdraw(amount)
  83. IF balance > amount THEN
  84. SET balance to balance - amount
  85. END
  86. END FUNCTION
  87.  
  88. FUNCTION creditCardOffer
  89. IF NOT(customer.hasCreditCard) THEN
  90. CALL offerCreditCard
  91. END
  92. END FUNCTION
  93.  
  94. FUNCTION checkStatus
  95. IF NOT(customer.hasCheckingAccount) THEN
  96. CALL offerCheckingAccount
  97. END
  98. END FUNCTION
  99.  
  100. ....
  101. ....
  102. ....
  103.  
  104. END
  105.  
  106. 6. function isSquareRoot(arr) {
  107. var sum = 0;
  108. for(var i = 0; i < arr.length; i++) {
  109. sum += arr[i];
  110. }
  111. if(sum > 0 && Math.sqrt(sum) % 1 === 0)
  112. return Math.sqrt(sum);
  113. else
  114. return sum;
  115.  
  116. }
  117.  
  118. 7. function isDuplicate(arr) {
  119. for(var i = 0; i < arr.length; i++) {
  120. for(var j = 1; j <arr.length; j++){
  121. if(arr[i] === arr[j]) {
  122. return false;
  123. }
  124. }
  125. }
  126. return true;
  127. }
  128.  
  129.  
  130. 8. function isAnagram(string1, string2) {
  131. if(string1.length === string2.length) {
  132. var array1 = string1.split("");
  133. var array2 = string2.split("");
  134.  
  135. secondArrayLetter = array2.length - 1;
  136.  
  137. for(var i = 0; i< array1.length; i++){
  138. if(array1[i] === array2[secondArrayLetter]) {
  139. secondArrayLetter--;
  140. } else {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. else {
  147. return false;
  148. }
  149. }
Add Comment
Please, Sign In to add comment