on semantic style in COMP1511 assignment 0
a reply-to-public, written 2017-08-27

I’m wondering which of these functions would be considered more readable/stylish, when they both perform the same role:

Generally, is it better to - declare a new variable with a better name within the function, or - change the value of an existing variable within the function, then give it a new useful name when it returns it to the main function?

That’s a really good philosophical question, and in the best traditions of good philosophical questions, here’s a good philosophical answer: it depends.

In this case, it depends how you’re thinking about the problem, and how your particular programming language makes you think about the problem of values and names moving through the system.

Other languages can be much more up-tight about mutability, with some languages making all “variables” immutable by default, and some making mutability impossible.

Indeed, this course is inspired heavily by a course inspired heavily by a course inspired heavily by a programming language that makes mutating values impossible, except by applying a function to it.

In that vein, I’d suggest not changing values without also changing the names applied to them, and thus I’d suggest the style of the second variant here is closer to what we’d want,

(A momentary digression: many of my favourite programming languages have this property, because it makes it very pleasant to reason about values in the program.)

Unfortunately, C is a very different language, and in many ways a much more relaxed language: values can change and mutate in all kinds of ways while retaining the same name. Good C style often exploits this and other features of imperative programming languages.

In summary, then, there’s good arguments both for and against… shake the Magic Eight Ball again? :-)

I would suggest the second function is closer to what we’d like, though it’s still a bit off in the style we want. Try to assign as little as possible, and instead take advantage of values not changing over time.

(I’d also suggest, while I’m here: more whitespace, and better function names).

Try something like:

int getLocalStdMonth (int timezone, int month, int day, int timeUTC) {
    int retval = month;

    if (willOverflow (addTimes (timezone, timeUTC))) {
        if (isLastDayOfMonth (month, day)) {
            if (month == DECEMBER) {
                retval = JANUARY;
            } else {
                retval += 1;

    return retval;

So, we set a value up-front, then if and only if this long, intricate sequence of conditions holds, we change it. Remember that every if doesn’t necessarily need an else…