in Education by
I'm wondering if it's a good idea to make verifications in getters and setters, or elsewhere in the code. This might surprise you be when it comes to optimizations and speeding up the code, I think you should not make verifications in getters and setters, but in the code where you're updating your files or database. Am I wrong? 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
Well, one of the reasons why classes usually contain private members with public getters/setters is exactly because they can verify data. If you have a Number than can be between 1 and 100, i would definitely put something in the setter that validates that and then maybe throw an exception that is being caught by the code. The reason is simple: If you don't do it in the setter, you have to remember that 1 to 100 limitation every time you set it, which leads to duplicated code or when you forget it, it leads to an invalid state. As for performance, i'm with Knuth here: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

Related questions

0 votes
    What does Rhino do when the getter and setter methods exist? (a) It becomes JavaScript properties (b) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    What are getter methods and setter methods in Salesforce?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    I have a class which has 2 sets of getter & setters. 1 set is the traditional type. These work ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    Can you write setter method for a retain property?...
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    In data flow diagrams (DFD), the data flow shape represents boundary between trust levels or privileges. A. True B. False...
asked Feb 27, 2023 in Technology by JackTerrance
0 votes
    _______ include data shared between two more or more nodes in a network. A. Data B. Messages C. Shared fact D. Contracts...
asked Feb 23, 2023 in Technology by JackTerrance
0 votes
    State true or false: SAP systems are divided into different clients depending on business requirement, the data can be accessed within the client only. A. True B. False...
asked Feb 20, 2023 in Technology by JackTerrance
0 votes
    Which block or device does the data compression? a) Channel encoder b) Source encoder c) Modulator d) None of the mentioned...
asked Dec 29, 2022 in Education by JackTerrance
0 votes
    If you are running a web application or a user is going to fill it out and query data, the best option is to ... . Health monitoring probes C. A load balancer D. A traffic manager...
asked Nov 21, 2022 in Education by JackTerrance
0 votes
    When you upload a CSV file through UI or External Data API, it is recommended to provide the metadata in the format of __________. A. CSV B. JSON C. XML...
asked Nov 1, 2022 in Education by JackTerrance
0 votes
    Which of the following is an input device used to enter motion data in computers or other electronic devices ? 1. Monitor 2. Trackball 3. Plotter 4. Joystick...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    In python3 console, input those: >>> import sys >>> sys.version_info sys.version_info(major=3, minor= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    In the chapter seven of this book "TensorFlow Machine Learning Cookbook" the author in pre-processing data ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
...