Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q1 Often when reading through a computer program or attempting to debug or enhan

ID: 3664017 • Letter: Q

Question

Q1 Often when reading through a computer program or attempting to debug or enhance the program, one needs to be able to read through the program effectively. A large part of being able to read through a program is to be able to make out its variables and connect logically which variable belongs to which function. Discuss naming standards as they apply to variables in your program and the implications of having good and poor naming standards in terms of being able to understand a program. Justify your answers using examples and reasoning. Comment on the postings of at least two classmates with regard to whether you agree or disagree with their views.

Q1 Often when reading through a computer program or attempting to debug or enhance the program, one needs to be able to read through the program effectively. A large part of being able to read through a program is to be able to make out its variables and connect logically which variable belongs to which function. Discuss naming standards as they apply to variables in your program and the implications of having good and poor naming standards in terms of being able to understand a program. Justify your answers using examples and reasoning. Comment on the postings of at least two classmates with regard to whether you agree or disagree with their views.

Explanation / Answer

Underscore Before the Property Name

When you come across a variable or method that is proceeded by an _, there's no voodoo magic being performed behind the scenes. It's simply a naming convention that reminds the developer that the variable/property/method is either private orprotected, and cannot be accessed from outside of the class..

PHP Method

/ This variable is not available outside of the class

private $_someVariable;

class MyClass {

   // This method is only available from within this class, or

   // any others that inherit from it.

   protected function __behindTheScenesMethod() {}

}

Note that, in the code above, the underscore is not what makes the variable or method private; the private/protected keyword does that. The underscore is only a reminder for six months down the road!

JavaScript Method

var Person = (function() {

   var _trueAge = 50,

        _trueWeight = 140;

   return {

      age : _trueAge - 15,

      weight : _trueWeight - 30

   };

})();

Person.age; // 35

Person.weight; // 110

Person._trueAge; // undefined (cause it's private, yo)

By making Person equal to, not a function, but the returned object, we can create private variables. Again, the underscore prefix reminds us of this.

Clearer Understanding

Whether you are amending an existing program or writing something new, clearly structured code that tells you what kind of object you are dealing with and how it fits into the structure can do some of the thinking for you.

Bugs that were hidden in a cryptic jumble can just leap out at you when the code is laid out logically. Often when facing a debugging problem in poorly maintained code, the first step is to straighten out the layout, allowing you to see how it can be rationalised. (For example, look at the query shown in this OTN forum post, then scroll down to see the formatted version. That final nested subquery was a surprise to me as well.) Often just understanding what the code means is halfway to solving a programming problem. Clearly, not understanding what you are working on is likely to prove a hindrance to efficient programming. Like those "De-clutter Your Life" makeover shows on TV, the result is a more efficient, pleasant and relaxing place to be, where you can easily reach for the things you need without tripping over the toaster. So THAT’s the structure, you think as you see it in its new untangled format. This is called refactoring and it should be carried out ruthlessly.

Better Code

Simply using clear names for objects, and laying out code so that the structure is easy to follow, should reduce spaghetti code and result in better-structured modules. It will be easier for others to see how the code works, and therefore modify it as necessary without increasing code entropy, which occurs when the originally intended design of a module is eroded by subsequent changes. The entropy accelerates as the code becomes harder to understand. You don't have time to figure out how all the sprawling loops and confusing variables interact with each other, so you paste in another ELSIF and hope for the best. Eventually it becomes impossible for anyone to figure out with any confidence how the code really works and you have what is known as a legacy system or third party application