M
M
Max2019-06-25 18:13:13
PHP
Max, 2019-06-25 18:13:13

Where is the best code quality?

With a colleague, an eternal holivar about the quality of the code.
Here is one example.
Please rate how it is correct to write in such pieces)
1)

if (!$_SESSION['isMobileOnly']) {
    $opinionList = $dao->getOpinionList($bar['id']);
} else {
    $opinionList = $dao->getOpinionList($bar['id'], 3);
}

2)
$opinionList = $dao->getOpinionList($bar['id'], $_SESSION['isMobileOnly'] ? 3 : null);

3) Your option)

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
Maksim Fedorov, 2019-06-25
@kopcapuk

Everything is bad

  • Global variables are evil
  • On the face of a certain code that works with the session and with the database - a clear violation of generally accepted principles, such as SRP
  • $dao how is it initialized? Obviously not through the constructor
  • Using a number, just sit and guess from it - this is bad, use a named constant. For example, 3 is a certain status for desktop, so call it STATUS_DESKTOP, although further...
  • you are referring to one method, which is very smart - it works with flags, thereby violating SRP and itself contains if / else, just make 2 separate methods:
    getMobileOpinionList()
    getDefaultOpinionList()

  • It is better to move the predicate into a separate isMobileOnly() method, because the conditions can change in it, you won’t change it everywhere in if throughout the project

I
Immortal_pony, 2019-06-25
@Immortal_pony

$deviceCode = $_SESSION['isMobileOnly'] ? 3 : null;
$opinionList = $dao->getOpinionList($bar['id'], $deviceCode);

D
dkar007, 2019-06-26
@dkar007

The first option is visually easier to disassemble than the second.

A
Alexander Vladimirovich, 2019-06-25
@polyanin

Wrap the session in an object, use types wherever possible.
See https://symfony.com/doc/current/components/http_fo...
ps Move the number 3 into a named constant.

A
Adamos, 2019-06-26
@Adamos

In order of increasing entropy:

$opinionList = $dao->getOpinionList(
    $bar['id'], 
    $_SESSION['isMobileOnly'] ? 3 : null
);

A
Arslan Isaev, 2019-06-28
@isaev_ars

No one! What is the number 3, why is it not made into a constant, if it is so important. It's like you're only writing for yourself..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question