DEV Community

Discussion on: 10 CODING MISTAKES THAT MAKE YOUR CODE SMELL

Collapse
 
techsuman2019 profile image
Tech Suman

"No argument is best" What's your rationale behind this? If a function isn't receiving any argument, then either of three things can be said about it. One, it's not receiving data from other parts of the program and is directly interacting with the user to get it's inputs. Two, it's interacting with global variables. Three, it's a routine task, that doesn't need any data. If it's return value is session invariant, or is using some other library like datetime, then it makes sense, otherwise, function with no argument seems a little funky to me. The first two possibilities are a big no no.

Collapse
 
eljayadobe profile image
Eljay-Adobe

I agree with this. Having a function with no arguments means the function only has a side-effect, or operates on variables external to the function. It is far better to pass in all parameters to the function than having a function that is entangled with external variables (including member object variables, which are "global" to the function, even if the function is a member function of the same object).

Collapse
 
alexanderholman profile image
Alexander Holman

There are a few more than 3 ways to peel a potato! e.g. where the data owns the method, and the methods mutate the instance directly without any input, see the example below:

class Account {
    private LocalDate expires; // e.g. 2019-08-19
    public void renew()
    {
        this.expires = this.expires.plusYears(1); // now 2020-08-19
    }
}