1
\$\begingroup\$

I'm working on my way to write most efficient, reusable and robust code, and this is why I'd like to receive some support. I've written a datetimepicker script that allows datetime chosing, and a shortcut button that will subtract from higher date (the right input) a variable number of minutes:

Is there a way to simplify my code even more?

<form> 
  <input type="text" name="poczatek" id="od" /> 
  <input type="text" name="koniec" id="do" /> 
  <input type="button" value="10" class="odejmijCzas"/> 
  <input type="button" value="20" class="odejmijCzas"/> 
  <input type="button" value="30" class="odejmijCzas"/> 
</form>

JavaScript

dateTimeFormat = new Object();
dateTimeFormat.dwucyfrowa = function(m) {
    return m < 10 ? '0' + m : m;
}
dateTimeFormat.format = function(date) {
    return date.getFullYear() + '-' + dateTimeFormat.dwucyfrowa(date.getMonth() + 1) + '-' + dateTimeFormat.dwucyfrowa(date.getDate()) + ' ' + dateTimeFormat.dwucyfrowa(date.getHours()) + ':' + dateTimeFormat.dwucyfrowa(date.getMinutes());
}
$(document).ready(function() {
    inputOd = $("#od");
    inputDo = $("#do");

    inputOd.add(inputDo).datetimepicker({
        changeMonth: 'true'
    });

    currentTime = new Date();
    inputOd.add(inputDo).val(dateTimeFormat.format(currentTime));

    $('.odejmijCzas').click(function() {
        odejmijCzas($(this).attr('value'));
    });
});

function odejmijCzas(liczbaMinut) {
    czasDo = new Date(inputDo.val());
    roznicaCzasu = czasDo.getTime() - liczbaMinut * 1000 * 60;
    czasOd = new Date();
    czasOd.setTime(roznicaCzasu);
    inputOd.val(dateTimeFormat.format(czasOd));
}​
\$\endgroup\$

migrated from stackoverflow.com Jun 22 '12 at 14:56

This question came from our site for professional and enthusiast programmers.

  • \$\begingroup\$ Are you having a problem with your code? \$\endgroup\$ – Andrew Barber May 21 '12 at 7:31
  • \$\begingroup\$ no it's working fine, but i'm wondering if i can shorten it even more \$\endgroup\$ – Malyo May 21 '12 at 7:31
  • \$\begingroup\$ Well this is programming question, i want to know if i can make the code shorter \$\endgroup\$ – Malyo May 21 '12 at 7:32
  • 2
    \$\begingroup\$ one great way to improve it - is not to use Polish for variable names \$\endgroup\$ – zerkms May 21 '12 at 7:33
2
\$\begingroup\$

A quick review:

High Level Overview

This code is okay except for 2 major items: you are not declaring your variables, making them escape all scope. And you use non-English variable names and function names, not every developer speaks Polish, though we should all understand English.

Naming

  • English is the one true language for naming, please find English equivalents for odejmijCzas, roznicaCzasu etc.

Idioms and Custom

  • Do not use = new Object();, use = {}

Scope

  • You must declare everything with var, let, or const
  • I would declare dateTimeFormat and odejmijCzas inside $(document).ready(function() { to keep the namespace clean

JSHint.com

  • Consider using http://jshint.com/ to perfect your code, most of these items were found by this tool as well
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy