Asked  7 Months ago    Answers:  5   Viewed   39 times

I am writing some PHP code to create PDFs using the FPDF library. And I basically use the same 4 lines of code to print every line of the document. I was wondering which is more efficient, repeating these 4 lines over and over, or would making it into a function be better? I'm curious because it feels like a function would have a larger overhead becuse the function would only be 4 lines long.

The code I am questioning looks like this:

$pdf->checkIfPageBreakNeeded($lineheight * 2, true);
$text = ' label';
$pdf->MultiCell(0, $lineheight, $text, 1, 'L', 1);
$text = $valueFromForm;
$pdf->MultiCell(0, $lineheight, $text, 1, 'L');
$pdf->Ln();

 Answers

59

This should answer it: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself and http://www.codinghorror.com/blog/2007/03/curlys-law-do-one-thing.html

Curly's Law, Do One Thing, is reflected in several core principles of modern software development:

  • Don't Repeat Yourself

    If you have more than one way to express the same thing, at some point the two or three different representations will most likely fall out of step with each other. Even if they don't, you're guaranteeing yourself the headache of maintaining them in parallel whenever a change occurs. And change will occur. Don't repeat yourself is important if you want flexible and maintainable software.

  • Once and Only Once

    Each and every declaration of behavior should occur once, and only once. This is one of the main goals, if not the main goal, when refactoring code. The design goal is to eliminate duplicated declarations of behavior, typically by merging them or replacing multiple similar implementations with a unifying abstraction.

  • Single Point of Truth

    Repetition leads to inconsistency and code that is subtly broken, because you changed only some repetitions when you needed to change all of them. Often, it also means that you haven't properly thought through the organization of your code. Any time you see duplicate code, that's a danger sign. Complexity is a cost; don't pay it twice.

Saturday, May 29, 2021
 
Yarin
answered 7 Months ago
33

Passwords should almost never be encrypted. Instead, they should be one-way hashed.

Generally, bcrypt is recommended, as it's resistant to brute forcing, where common alternatives like md5 or sha1 fail.

Saturday, May 29, 2021
 
Revent
answered 7 Months ago
62

In Windows programs you may use win32 functions:

  • Sleep() as a replacement for delay()
  • Beep() as a replacement for a combination of sound() + delay() + nosound().
Saturday, August 7, 2021
 
Pachvarsh
answered 4 Months ago
27

You should probably put it into a Library, and autoload the library. When you need to use the "logged_in" flag in a view, pass it in from the controller. Example:


application/libraries/Auth.php

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Auth
{

    public function is_logged_in ()
    {
        // Change this to your actual "am I logged in?" logic
        return $_SESSION['logged_in'];
    }

}

application/config/autoload.php

...
$autoload['libraries'] = array(
    ...
    'auth',
    ...
}

`application/controllers/welcome.php

<?php ...

public function index ()
{
    $view_data = array
    (
        'logged_in' => $this->Auth->logged_in()
    );
    $this->load->view('my_view', $view_data);
}

application/views/my_view.php

<? echo $logged_in ? 'Welcome back!' : 'Go login!' ?>
Saturday, August 14, 2021
 
EzzDev
answered 4 Months ago
98

You can destroy an entire session by calling the following:

$this->session->sess_destroy();
Wednesday, September 1, 2021
 
Astravagrant
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share