in Education by
Myself and a colleague have a dispute about which of the following is more elegant. I won't say who's who, so it is impartial. Which is more elegant? public function set hitZone(target:DisplayObject):void { if(_hitZone != target) { _hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOver); _hitZone.removeEventListener(MouseEvent.ROLL_OUT, onBtOut); _hitZone.removeEventListener(MouseEvent.MOUSE_DOWN, onBtDown); _hitZone = target; _hitZone.addEventListener(MouseEvent.ROLL_OVER, onBtOver, false, 0, true); _hitZone.addEventListener(MouseEvent.ROLL_OUT, onBtOut, false, 0, true); _hitZone.addEventListener(MouseEvent.MOUSE_DOWN, onBtDown, false, 0, true); } } ...or... public function set hitZone(target:DisplayObject):void { if(_hitZone == target)return; _hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOver); _hitZone.removeEventListener(MouseEvent.ROLL_OUT, onBtOut); _hitZone.removeEventListener(MouseEvent.MOUSE_DOWN, onBtDown); _hitZone = target; _hitZone.addEventListener(MouseEvent.ROLL_OVER, onBtOver, false, 0, true); _hitZone.addEventListener(MouseEvent.ROLL_OUT, onBtOut, false, 0, true); _hitZone.addEventListener(MouseEvent.MOUSE_DOWN, onBtDown, false, 0, true); } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
In most cases, returning early reduces the complexity and makes the code more readable. It's also one of the techniques applied in Spartan programming: Minimal use of Control Minimizing the use of conditionals by using specialized constructs such ternarization, inheritance, and classes such as Class Defaults, Class Once and Class Separator Simplifying conditionals with early return. Minimizing the use of looping constructs, by using action applicator classes such as Class Separate and Class FileSystemVisitor. Simplifying logic of iteration with early exits (via return, continue and break statements). In your example, I would choose option 2, as it makes the code more readable. I use the same technique when checking function parameters.

Related questions

0 votes
    I have a code: ab = 'off' if b.page == 'blog': ab = 'on' print ab Is there any other way to make it shorter in Python? Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    How do I perform an IF...THEN in an SQL SELECT statement? For example: SELECT IF(Obsolete = 'N' OR ... Saleable, * FROM Product Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Sometimes I break long conditions in ifs onto several lines. The most obvious way to do this is: if (cond1 ... an alternative way? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I'm trying to make a function that will compare multiple variables to an integer and output a string of three ... like this possible? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Greetings! I'm working on wrapping my head around LINQ. If I had some XML such as this loaded ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
0 votes
    To build SQL statements it is more secure to user PreparedStatement than Statement. (1)True (2)False...
asked May 15, 2021 in Education by JackTerrance
0 votes
    If an entity set is a lower-level entity set in more than one ISA relationship, then the entity ... in section Database Systems Design and Implementation of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    Is it possible that a function have more than one return statements? ,1.true,2.false Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    I have two packages com.myapp.foo and com.myapp.bar and I want to know the most elegant way to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Does Python have something like an empty string variable where you can do: if myString == string.empty: Regardless, ... not as good. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
0 votes
    In prestressing work, if more than one wire or strand is included in the same duct, why should all wires/strands be stressed at the same time?...
asked Jan 1, 2021 in Technology by JackTerrance
0 votes
    A_____ is a query that retrieves rows from more than one table or view: (a) Start (b) End (c ... Join Operations topic in chapter Query Processing Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    In C, how can i pass more than one argument to a thread? Normally, I do it in a way like, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
...