question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling functions for their side-effects

See original GitHub issue

It is fairly common to have functions that register information at the global level. Doing this involves some syntax that usually quite ugly. The two ways of doing this are to set a variable that is then disregarded or to assign a property to the output of the function which returns null thus causing the property to not be output.

Calling functions strictly for their side-effects is not something I’m particularly keen on encouraging – especially since Sass functions mostly return a useful value. That said, I think we should have the conversation about whether we should allow a bare function to be called at the root-level of the stylesheet since I’ve had several people ask me for this feature.

Example:

@import "some-framework";
register-framework-thing(a-thing, 10px);

Note: we could add a directive to do this with existing syntax rules. but this offers so little over @include that I don’t think it’s worth it.

@nex3 thoughts?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:2
  • Comments:30 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
ArmorDarkscommented, Jan 5, 2018

Well, I’d rather drop mixins altogether. And instead have a function, which could return CSS fragments and thus act as mixins.

So, instead of this:

@mixin borders ($value) {
  $final-border: ($value * 4) blue solid;

  border-left: $final-border;
  border-right: $final-border;
}

@include borders(1px)

There will be only this way:

@function borders ($value) {
  $final-border: ($value * 4) blue solid;

  @return {
    border-left: $final-border;
    border-right: $final-border;
  }
}

@include borders(1px)

This will eliminate the current issue with side-effects since the function will become the only and clear way to reuse code chunks and declare side effects.

And since distinguishing between mixins and functions will be no longer there, it will make a lot of things much clearer. For instance, it will be possible to use functions, which return CSS fragments, within other functions to achieve some complex effect. This is something that’s not that easy when you need to pass a piece of code from function to mixin or the other way.

Some might not like @return of CSS fragment part, but I found that way much clearer since it nicely decouples logic (part with $final-border) from actual output.

1reaction
ArmorDarkscommented, Mar 1, 2017

I tend to agree with @chriseppstein

Currently we already have very blurred mixins and functions, and out-of-place usage of mixins to make side effects. Mixins originally had to produce styles, not any side-effects, so it always felt to me rather like a hack. Thus I’m voting for a clear way to use functions for same purpose.

I wonder why @nex3 leaved this discussion? Is this like an act of protest? 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Side Effects, Pure Functions and Void Functions
In general, calling a function can (1) cause a side effect to occur and (2) return an answer. For example, random() returns an...
Read more >
Side effect (computer science) - Wikipedia
In computer science, an operation, function or expression is said to have a side effect if it modifies some state variable value(s) outside...
Read more >
Dealing with side effects and pure functions in javascript
I will call a side effect to anything that compromises the purity of a function. The list includes but is not limited to:....
Read more >
What are Side-Effects in Programming? | Nerd For Tech
We call them side-effects when it uses or changes stuff outside its scope like a variable passed by reference, global variable, reading user ......
Read more >
Functions and Side Effects
Side effect : a change in the program's state caused by an expression or a function call · Side effects make it harder...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found