-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enhancement] Correct use searchable names #17
Conversation
I think point of this example is "put magical numbers to explicitly named constants". It's not about time shifting. You could create new example for that though. |
Exactly, what @TomasVotruba said. Goal in this point was to give readable variables (date was here only example). Based on your example, you could do something like that:
Then you have clear and readable information. |
It's not good because you lost the context. Then it's better this way: class ArticleController
{
const SECONDS_IN_A_DAY = 86400;
const DEFAULT_CACHE_TTL = self::SECONDS_IN_A_DAY;
public function showAction(Article $article)
{
$ttl = self::DEFAULT_CACHE_TTL;
// change default TTL in context if need
// ...
$response = new Response();
$response
->setMaxAge($ttl)
->setSharedMaxAge($ttl)
->setExpires(new \DateTime(sprintf('+%d seconds', $ttl)))
;
// ...
}
} But what is the meaning of the constant |
By the way. Some write like this: addExpireAt(60*60*24*7); // 1 week I do not approve of this way, but it is also more understandable. And do not think that all the programmers are idiots. |
addExpireAt(60*60*24*7); // 1 week That could be actually nice in constant value, in case sb would like to add
I do respect myself, but I don't want to think about stuff I don't have to ;) |
Why think, if you can know? |
I guess my brain is just structured differently than yours. I try to find lowest common denominator so most people could understand the knowledge with minimal thinking possible. |
Is it too difficult for you? Okay. Let's return to the original problem. The use of global constants contradicts the statement: Don't write to global functions Hence, we do not have to use global constants. Hence, we can use constants only in the context of their use. Is not context enough to understand what these magic numbers mean? class ArticleController
{
const DEFAULT_CACHE_TTL = 3600;
// ...
} Perhaps you don't have enough experience to read this code. class DoctrineContestantRepository implements ContestantRepository
{
public function totalAwaitingContestants()
{
return $this->repository->countOf(
new ContestantAwaitingModerationSpec(),
new Cache(600)
);
}
} Is it not clear what If in doubt. You can always check. Another example. class ScheduleFactory
{
public static function createSchedule()
{
return new Schedule([
new WeekdayRule(0, 2, 1000),
new WeekdayRule(2, 7, 1200),
new WeekdayRule(7, 10, 500),
new WeekdayRule(10, 19, 400),
new WeekdayRule(19, 22, 500),
new WeekdayRule(22, 24, 700),
new HolidayRule(0, 3, 700),
new HolidayRule(3, 9, 1300),
new HolidayRule(9, 19, 400),
new HolidayRule(19, 23, 500),
new HolidayRule(23, 24, 700),
]);
}
} The third parameter for classes PS: The configuration options for the schedule are not included in the configuration file because this is an unnecessary overcompliance. |
If such an entry is not clear to you new Cache(600) Can such a entry be more clear to you? new Cache('1 hour 10 minutes') This of course perversion, but. |
class Cache
{
private $ttl;
public function __construct($time)
{
$zero = new \DateTime('01-01-1970', new \DateTimeZone('UTC'));
$this->ttl = $zero->modify($time)->getTimestamp();
}
public function ttl()
{
return $this->ttl;
}
} Usage $cache = new Cache('1 hour 10 minutes');
var_dump($cache->ttl());
It's just an idea. This is not a call to action. I and everyone i've ever worked with, still believes that the numbers are fairly clear. |
By the way, what about chmod? // What the heck is 0644 for?
chmod($filename, 0644); This entry is understandable for you or maybe you also need to use constants? |
I think this is a problem of context. But for time is not always clear. what is
TBH I'd rather use here bitwise operations, like PERMISSION_OWNER_READ | PERMISSION_OWNER_WRITE | PERMISSION_GROUP_READ | PERMISSION_OTHER_READ This will make this clear and portable even for non-POSIX permission systems |
@scaytrase I changed example to use bit masks #80. |
This problem is solved in #80 |
👍 |
Fix for Use searchable names.
This is bullshit
You created a global constants. Is not a good.
Any class/function can depend on these constants and it will be difficult to put them into a separate component.
Your constants is not a informative.
Requires each time to create a new constant if you have complex conditions. Example:
If you really want to use constants, you must declare them in the context of use.
There is no misunderstanding.