Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:21:11+00:00 2026-06-03T16:21:11+00:00

Is there any harm to having functions one lined instead of multilined? For example,

  • 0

Is there any harm to having functions one lined instead of multilined? For example, I wrote a class file for a PDO database connection. All the functions I wrote for it, are one lined. I haven’t run into any errors yet, but can they crop up if there is lag or something?

Here are some of the functions.

public function getObject($query, $object) {
  return $this->db->prepare("$query")->execute()->fetch(PDO::FETCH_OBJ)->$object;
}

public function getArray($query) {
  return $this->db->prepare("$query")->execute()->fetchAll(PDO::FETCH_NUM);
}

public function insertArray($query, $array) {
  return $this->db->prepare("$query")->execute($array);
}

Will this eventually run into problems? Or do they get called one section at a time, as if they were on their own line?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:21:14+00:00Added an answer on June 3, 2026 at 4:21 pm

    Using chained methods in one single line like this

    return $this->db->prepare("$query")->execute()->fetch(PDO::FETCH_OBJ)->$object;
    

    is what Robert Martin calls “Train Wrecks” in Clean Code (pg 98/99). It’s hard to read and “generally considered sloppy” and should be avoided for the more readable

    $statement = $this->db->prepare("$query");
    $statement->execute();
    $result = $statement->fetch(PDO::FETCH_OBJ);
    return $result->$object;
    

    Please note that I corrected the code in the snippet above, as your one-liner will not work, because execute returns a boolean, so you cannot call fetch on it:

    bool PDOStatement::execute ([ array $input_parameters ] )

    In other words, you cannot write this as a one-line statement anyway.

    Also note that Method Chaining is very often a violation of the Law of Demeter, which states that

    a method M of an object O may only invoke the methods of the following kinds of objects:

    • O itself
    • M’s parameters
    • any objects created/instantiated within M
    • O’s direct component objects
    • a global variable, accessible by O, in the scope of M

    Not following LoD usually leads to Mockfests in your UnitTests and makes your application tightly coupled to much more classes than necessary, which in turn impairs reusability and increases the time required for changes (among other things).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With obj-c on the iPhone, is there any harm in autoreleasing everything instead of
Hiya - quick one - is there any harm / value in subclassing JComponent
Using GScript for example, is there any harm that can come to a phone
Can I make the spring service classes final? Is there any harm doing that?
Is there any way to update cookies across folders in PHP? For example, consider
Is there any quick way to copy eclipse and android development environment from one
Is there any harm hosting images through a website like Imgur then using the
I am running Tomcat inside Eclipse. Is there any harm in using System.out.println only
Is there any way to trigger a function that's called when the user types
Is there any subprogram similar to DBMS_METADATA.GET_DDL that can actually export the table data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.