Declaring all variables at the beginning of a function is a terrible evil.
If I set $ Result after checking! $ QueryResource, then I’d have to quickly re-read the code in search of the type of this variable.
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
function sql2array ( $query , $field = false ) { $Result = array ( ) ; if ( DEBUG ) print ( "Query: $query \n " ) ; $queryResource = $this -> query ( $query ) ; if ( ! $queryResource ) return false ; while ( $Data = $this -> fetch_array ( $queryResource ) ) { if ( ! $field ) $Result [ ] = $Data ; else $Result [ $Data [ $field ] ] = $Data ; } $this -> free_result ( $queryResource ) ; return $Result ; }
Just imagine: We have a function in 300 lines of code [2]. Somewhere on the 200th line, we need to swap two variable places. To do this, we climb 200 runoff higher to the beginning of the function, declare a variable temp, which has nothing to do with the whole function, but is used only once in one place, then again we return to the 200th line and change variable places ... I think , it's just a nightmare.It's not scary. When you write code, you know what's going on in it. It becomes scary to other people when they read the code and tries to find the variables they need in 300 lines.
As you can see, the function now returns two parameters - this is $ error - the status of the function execution and an array, if correctly executed.
- function sql2array ( $ query , $ field = false , & $ error = NO_ERROR ) {
- $ Result = array ( ) ;
- if ( DEBUG )
- print ( "Query: $ query \ n " ) ;
- $ queryResource = this -> query ( $ query ) ;
- if ( ! $ queryResource ) {
- $ error = QUERY_ERROR ;
- return false ;
- }
- while ( $ Data = $ this -> fetch_array ( $ queryResource ) ) {
- if ( ! $ field )
- $ Result [ ] = $ Data ;
- elseif ( isset ( $ Data [ $ field ] ) )
- $ Result [ $ Data [ $ field ] ] = $ Data ;
- else {
- $ error = FIELD_ERROR ;
- return false ;
- }
- }
- $ this -> free_result ( $ queryResource ) ;
- return $ Result ;
- }
Voila
- class A {
- private $ error ;
- public function getLastError ( ) {
- return $ this -> error ;
- }
- private function setError ( $ error ) {
- $ this -> error = $ error ;
- }
- public function sql2array ( $ query , $ field = false ) {
- $ Result = array ( ) ;
- if ( DEBUG )
- print ( "Query: $ query \ n " ) ;
- $ queryResource = this -> query ( $ query ) ;
- if ( ! $ queryResource ) {
- $ this -> setError ( QUERY_ERROR ) ;
- return false ;
- }
- while ( $ Data = $ this -> fetch_array ( $ queryResource ) ) {
- if ( ! $ field )
- $ Result [ ] = $ Data ;
- elseif ( isset ( $ Data [ $ field ] ) )
- $ Result [ $ Data [ $ field ] ] = $ Data ;
- else {
- $ this -> setError ( QUERY_ERROR ) ;
- return false ;
- }
- }
- $ this -> free_result ( $ queryResource ) ;
- return $ Result ;
- }
- }
Fully classes, I do not cite. But it already becomes clear for what reasons it is worth thinking about which method to use to change a variable. With all that, it is worth thinking about how your class can use in multi-threaded applications, or for example, when working with a database.
- class A {
- protected $ length ;
- public function getLength ( ) {
- return $ this -> length ;
- }
- }
- class B extends A {
- public function getLength ( ) {
- if ( $ this -> testLength ( ) )
- return ( $ this -> length + 1 ) * 100 ;
- return parent :: getLength ( ) ;
- }
- }
The code is obvious and clear. In this case, the parameters can be arbitrarily many without additional tweaks. And the functions take the parameters that they should take.
- BackgroundColor ( 1 , 1 , 255 ) ;
- BorderColor ( 255 , 1 , 1 ) ;
- BorderWidth ( 2 ) ;
- DrawRotation ( 30 ) ;
- AlphaColor ( 20 ) ;
- DrawRectangle ( 80 , 25 , 50 , 75 ) ;
Thus, not all methods are good. And what the author described is not a panacea at all.Practically, the same thing happens if you use the Command pattern.
- void DrawRectangle ( Rectangle rect ) {
- Line line ;
- float x1 , y1 ;
- float x2 , y2 ;
- x1 = rect. x - rect. width / 2 ;
- y1 = rect. y - rect. height / 2 ;
- x2 = rect. x + rect. width / 2 ;
- y2 = rect. y + rect. height / 2 ;
- line. color = rect. borderColor ;
- line. weigth = rect. borderWeigth ;
- line. alpha = rect. alpha ;
- // rotation and so on, let's imagine, forgot ...
- line. x1 = x1 ;
- line. y1 = y1 ;
- line. x2 = x2 ;
- line. y2 = y1 ;
- DrawLine ( line ) ;
- line. x1 = x2 ;
- line. y1 = y1 ;
- line. x2 = x2 ;
- line. y2 = y2 ;
- DrawLine ( line ) ;
- line. x1 = x2 ;
- line. y1 = y2 ;
- line. x2 = x1 ;
- line. y2 = y2 ;
- DrawLine ( line ) ;
- line. x1 = x1 ;
- line. y1 = y2 ;
- line. x2 = x1 ;
- line. y2 = y1 ;
- DrawLine ( line ) ;
- }
Source: https://habr.com/ru/post/59642/
All Articles