Welcome Guest, Not a member yet? Register   Sign In
Best practice to maintain your code readability, etc.
#1

Just wondering how other people maintain their codes without sacrificing its readability?
Since I am a home-based (and newbie) web developer, I don't have experience working with other developers (especially on a big projects).

Love to hear your experiences  Wink

Kaitenz  Heart
Reply
#2

@kaitenz,

Using the CI Style/coding guide is definitely a good starting place ( https://codeigniter.com/userguide3/gener...guide.html ). This way there are no questions of your expectations from other developers who may work with you. Are you #TeamTabs or #TeamSpaces?
Reply
#3

Skinny controllers, fat models - try to keep your functionality and business logic in models, that way it's easily re-usable.

Another thing I sometimes do when I have more time to pre-plan, I start writing mock model-methods in controllers to see how it lays out, then filling in functionality for methods afterwards.
Reply
#4

Ironical the Docs state always use the following example and never use it in their own examples Smile

>>> if (strpos($str, 'foo') === FALSE)
Reply
#5

Take the time to learn Php heredoc because lengthy lines can be drastically simplified and understood at a single glance.

Always use Php 7 and learn how to use declare(strict_types=1);

Declare a function's return type and always only have a single return in every function/method.
Reply
#6

(This post was last modified: 07-10-2018, 06:24 PM by John_Betong.)

...and not forgetting...

Never ever use Css styles in Libraries, Controllers or Models which I have learnt from bitter experience in trying to debug Views.
Reply
#7

(This post was last modified: 07-10-2018, 07:37 PM by kaitenz.)

(07-10-2018, 08:16 AM)php_rocs Wrote: @kaitenz,

Are you #TeamTabs or #TeamSpaces?
I'm on #TeamTabs

(07-10-2018, 01:48 PM)Pertti Wrote: Skinny controllers, fat models - try to keep your functionality and business logic in models, that way it's easily re-usable.

Another thing I sometimes do when I have more time to pre-plan, I start writing mock model-methods in controllers to see how it lays out, then filling in functionality for methods afterwards.
Wow. We are on the same page.
I have a controller named "Test" where I test some codes/models/libraries/helpers in this controller.

(07-10-2018, 05:31 PM)John_Betong Wrote: Take the time to learn Php heredoc because lengthy lines can be drastically simplified and understood at a single glance.
Heard them before. Haven't tried to use it. Maybe this is the time.. Hahaha.

(07-10-2018, 06:23 PM)John_Betong Wrote: ...and not forgetting...

Never ever use Css styles in Libraries, Controllers or Models which I have learnt from bitter experience in trying to debug Views.
Same. I always separate them. Just like in JavaScript, I don't like using the in-line JS thing because it is messy and hard to maintain. But that is just my taste and maybe it is useful to others but not to me.
Reply
#8

Just want to add this so if someone saw this thread (maybe because we are on the same page):

(03-27-2018, 09:08 AM)jreklund Wrote: You should not use xss_clean on input
https://www.codeigniter.com/user_guide/i...ation-rule

Use html_escape on output instead
https://www.codeigniter.com/user_guide/g...tml_escape

If you need users to submit html use
http://htmlpurifier.org/

If users submit data with AJAX they are till vulnerable to CSRF, so please add that.

Darn! I always use xss_clean() on input. That's why when I tried to hack my own creation, I always outputting something like this:
Code:
[removed]script[removed]
Reply
#9

Another neat way to simplify code is to drop nested if-else statements and exit method as early as possible.

PHP Code:
public function test($input)
{
 
   if (is_integer($input)) {
 
       if ($input 100) {
 
           return $input 2;
 
       } else {
 
           if ($input 0) {
 
               return $input 3;
 
           } elseif ($input 50) {
 
               return $input;
 
           } else {
 
               return $input 4;
 
           }
 
       }
 
   }

 
   return false;


Could be simplified to:
PHP Code:
public function test($input)
{
 
   if (!is_integer($input)) {
        return 
false;
    }

    if (
$input 0) {
 
       return $input 3;
    }

 
   if ($input 100) {
 
       return $input 2;
 
   }

    if (
$input 50) {
 
       return $input;
 
   }

    return 
$input 4;

Reply
#10

(This post was last modified: 07-14-2018, 10:19 PM by John_Betong.)

(07-10-2018, 11:24 PM)Pertti Wrote: >>> Another neat way to simplify code is to drop nested if-else statements and exit method as early as possible.

As mentioned I prefer declaring a default  $result and only returning a single $result.

Each to their own [Image: smile.gif]
PHP Code:
//===================================
function test_2($input)
:
int
{
  $result = -9999999// DEFAULT

  if (! is_integer($input))
  {
     // DEFAULT // false;
     // $result = -9999999; 

  }else if ($input 0) {
    $result $input 3;

  }else if ($input 100) {
    $result $input 2;

  }else if ($input 50) {
    $result =  $input;

  }else {
    $result =  $input 4;
  }

  return $result;
}
  
Reply




Theme © iAndrew 2016 - Forum software by © MyBB