Code Monkey home page Code Monkey logo

exo-heating-system's Introduction

Refactoring ScheduleManager.php

  • Added return type as string for stringFromURL
  • Condition to check the current time can be a function
gettimeofday( true ) > self::startHour() && gettimeofday( true ) < self::endHour()

// to
private static function isCurrTimeInBetween(): boolean {
    return gettimeofday( true ) > self::startHour() && gettimeofday( true ) < self::endHour();
}
  • The two if conditions are never executed always. We can make it a if else statement.
if ( self::isCurrTimeInBetween() ) {
    $hM->manageHeating( $t, $threshold, true );
} else  {
    $hM->manageHeating( $t, $threshold, false );
}
  • Return value from endHour() and startHour().
  • Converted type boolean to bool.
  • Remove the if else condition and replace the 3 parameter of manageHeating()
if ( self::isCurrTimeInBetween() ) {
    $hM->manageHeating( $t, $threshold, true );
} else  {
    $hM->manageHeating( $t, $threshold, false );
}

// to

$hM->manageHeating( $t, $threshold, self::isCurrTimeInBetween() );

Refactoring HeatingManagerImpl.php

  • Converted type boolean to bool.
  • Try catch can be enclosed once instead of each if block, since we are not doing things differently in either case.
  • Since both the if conditions are have && $active we can add a negate and do an early return.
if (!$active) {
    return;
}
  • Common code to create socket and connect can be moved before the if condition
if ( !( $s = socket_create( AF_INET, SOCK_STREAM, 0 ) ) ) {
    die( 'could not create socket' );
}
if ( !socket_connect( $s, 'heater.home', 9999 ) ) {
    die( 'could not connect!' );
}
  • Common code to send and close the socket can be moved after the if condition
socket_send( $s, $m, strlen( $m ), 0 );
socket_close( $s );

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.