DEV Community

endan
endan

Posted on

What is your favourite function()?

I think mine would be json_encode(). I think it's so cool that a single function can make your data understood by other machines. (I know other machines can interpret different kinds of data, but c'mon...)

How about you? What's your favourite function?

Oldest comments (36)

Collapse
 
nektro profile image
Meghan (she/her) • Edited
new Date()
Collapse
 
itsasine profile image
ItsASine (Kayla)
var moment = require('moment');

😉

Collapse
 
avalander profile image
Avalander
explain_fork_or_die();

Reference

Collapse
 
teej profile image
TJ Fogarty

Based on usage it's probably console.log() or var_dump().

At the moment I'm enjoying some JavaScript array methods - filter, map, reduce.

Collapse
 
jjjjcccjjf profile image
endan

var_dump (); die ()!!!

Collapse
 
inozex profile image
Tiago Marques

I usually do like:
die(var_dump());

Thread Thread
 
jjjjcccjjf profile image
endan

I stopped doing this simply because I can't do it if I dump multiple variables at once.

var_dump($res);
var_dump($res2);
die();
Thread Thread
 
lexlohr profile image
Alex Lohr

But you could do die(var_dump($res, $res2));, couldn't you?

Thread Thread
 
inozex profile image
Tiago Marques

You can

Thread Thread
 
jjjjcccjjf profile image
endan

Really? Wow, #TIL!

Thread Thread
 
inozex profile image
Tiago Marques

Yeah, just like console.log(var1, var2) in JS

Collapse
 
31547 profile image
31547

typeof(arg);

surprisingly useful

Collapse
 
bgadrian profile image
Adrian B.G.

random() functions, there is so much more behind them then we usually think. I can talk hours about entropy and sources, techniques, pseudo algorithms and how can it improve almost any software product.

Collapse
 
jjjjcccjjf profile image
endan

Improve in what way?

Collapse
 
isaacleimgruber profile image
IsaacLeimgruber

In the way of randomized algorithms

Collapse
 
bgadrian profile image
Adrian B.G. • Edited

As in, some product features can be improved using random (shuffle or generate things), most basic example is chose new 5 news headlines to see from the top 100 articles to an user, each x seconds/page views.

Thread Thread
 
jjjjcccjjf profile image
endan

inb4
p e r s o n a l i z a t i o n
🤣

Collapse
 
alchermd profile image
John Alcher
$("");

Because it took me almost half a year to realize that it's a function, not a language construct.

Collapse
 
jishvi profile image
Jishnu Vasanth

Arrow function 🤓. this has never been easier.

Collapse
 
essiccf37 profile image
essic

mine is fmap ...
fmap :: (a -> b) -> F a -> F b
with F being a Functor :-)

I find this one, truly comforting for some reason.

Collapse
 
bertilmuth profile image
Bertil Muth

I like map.put and map.get. Maps/Dictionaries are incredibly useful data structures that we tend to take for granted nowadays.

Collapse
 
juankortiz profile image
juankOrtiz

Don't know if it's only a function, but ajax in Javascript is something else: the whole idea of a function connecting asynchronously to a server and retrieving data without the need of recharge the entire page is amazing.

Collapse
 
itsasine profile image
ItsASine (Kayla) • Edited
expect(true).toBe(true);
  1. It's a super useful dumb placeholder when just building out describes, its, beforeAlls, etc in Jasmine tests without actually caring about the legit expectations yet.
  2. It actually failed for me once, which was... enlightening
Collapse
 
jundialwan profile image
Jundi Alwan

map, filter, reduce, forEach, Array.prototype.some, Array.prototype.every

Working with array in Javascript never been easier.
Love it all!

Collapse
 
isaacleimgruber profile image
IsaacLeimgruber

Agreed, high order functions are best

Collapse
 
maxmaxymenko profile image
Max Maxymenko • Edited

Love throwing exceptions around ;)

throw new Exception("...");
throw Error("...");
raise Exception.Create('...');
Collapse
 
pilskalns profile image
Andžs

In PHP I always include file with my custom helper functions. These two wrap output with <pre/> tags, which allows nice&quick debugging via browser from any class or template code.


function pre($obj = null, $escape = false){
    echo "<pre>";
    if($escape){
        $obj = htmlspecialchars ( print_r($obj, true) );
    }
    print_r($obj);
    echo "</pre>";
}

function dump($obj = null){
    echo "<pre>";
    var_dump($obj);
    echo "</pre>";
}
Collapse
 
jjjjcccjjf profile image
endan

var_masterpiece is also an excellent browser extension too for debugging!

Collapse
 
dfellini profile image
Dan Fellini

HA! I have a pre() function too! I use it many, many times a day. Mine has the $obj, but then a title, so if I have multiple pre()s going, I know which is which. Like, pre( $obj, 'This is the user obj');
Love it.

Collapse
 
joshcheek profile image
Josh Cheek

In Ruby, backticks are a function call. And what's especially cool is that heredocuments with backticks call the same function, but with multi line arguments. So you can define them to do other, more interesting things, like call out to other languages! In practice, I mostly use it when experimenting with some slightly tedious external resource, I basically learned PostgreSQL by running them through these kinds of functions.

require 'open3'

def `(str)
  Open3.capture3('php', stdin_data: str).first
end

<<~`PHP` # => "hello cruel world\n"
hello \
<?php echo "cruel" ?> \
world
PHP