3

I am currently refactoring an application that i built in JavaScript.
The application uses a starting hour and a total working hour count in order to construct a timetable for daily, weekly and monthly views. The starting hour and total working hour count are the same for all timetables

I considered using a pattern to initialise the Timetable and started reading up on how to proceed in doing so in Javascript as I am not too familiar with the language. My orginal initialisation function for the Timetable had the following signature:

FillTimeTable( startingTimeWeekDay, workinghourCountWeekDay );  

Problem with this method is that for a different view of the timetable I have to call the other views with the same signature again.

FillWeeklyTimeTable( startingTimeWeekDay, workinghourCountWeekDay );
FillMonthlyTimeTable( startingTimeWeekDay, workinghourCountWeekDay );

If the working hours change I or the poor bugger maintaining my code would have to alter them at different locations within the code and due to the way I organised my code they are not placed as neatly as here. So during refactoring I considered alternatives like a global variable, Singleton or an Object.

I read about objects, private members and constructors and decided that this might be the solution after reading up on Singleton Implementations and binning the global variables.

So currently I have the following code:

function Timetable( startingTimeWeekDay, workinghourCountWeekday) {  
    this.startWeekday = startingTimeWeekDay;  
    this.hoursWeekday = workinghourCountWeekday;
}

Timetable.prototype.startWeekday = function() {  
    return this.startWeekday;
}

Timetable.prototype.hoursWeekday = function() {
    return this.hoursWeekday;
}

var myTimetable = new Timetable( 6, 8 );   

Everything is working fine so far, but myTimetable just seems like a really fancy way of declaring a global variable to me.

Is this the case?

Did I just decide on the wrong strategy or is this the way to go or have I missed something essential to working with JavaScript?

4
  • Are the hours per week and working hour count the same for all timetables? Aug 13, 2013 at 13:35
  • 2
    The real problem is that all that extra code does nothing -- it provides no more encapsulation than using an object with two properties, but is way more verbose; and you spelled the parameters differently from the fields -- possibly a side effect of the verbosity? Whether you've created a global variable depends on whether the code appears inside a function (which would make it not global) or not.
    – user39685
    Aug 13, 2013 at 13:39
  • @AaronKurtzhals: yes they are. I calculate the total hours per day using startTime and working hour count. Aug 13, 2013 at 13:44
  • @MattFenwick: Okay, do you have a better solution in order to refactor this any better instead of just bloating code? That's what you are saying, right? Aug 13, 2013 at 13:45

2 Answers 2

1

If the application — or rather the instance of the application in a particular browser while it is hosting that page — has only one sense of “starting hour” and one sense of “total working hour count” that it shares throughout itself, it's not a wrong thing to have each of those things represented by a single variable in the script. Scattering it all over the place would be worse.

This in turn means that you're either going to naturally have global variables or a unique object that holds that state. From a pattern-theoretic sense, a unique object that is injected into the consumers of the object (either by setting a property on that object or via a construction argument) is best, but for small applications there's no need to get that complex; global variables or a “well-known” global holder object can do just as well. (If the code is small, refactoring it when it grows won't be a big chore. If the code is large, you use the fancier style to start with.)

3

I suggest that you create an object to hold the work schedule information, then pass the object to your Timetable functions.

var companyXWorkSchedule = {
    "startingTimeWeekDay": 6,
    "workinghourCountWeekday": 8
};

You don't need to create a global variable. companyXWorkSchedule could be a property on an object.

edit: added missing equals sign

3
  • Just a note, quoting the keys isn't necessary in javascript (but won't harm anything)
    – Izkata
    Aug 13, 2013 at 14:49
  • 1
    Would still have to pass it into every function as a parameter. Does solve the long signature, but not really the design issue. Aug 13, 2013 at 15:21
  • @BenMcDougall what do you mean by not solving the design issue? Which design issue? Aug 13, 2013 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.