DEV Community

arpitgupta1250
arpitgupta1250

Posted on

1

Tips to write smart code

1. Cascades

Normal way -

Paint x = Paint();
x.color = Colors.red;

Smarter way -

Paint x = Paint();
..color = Colors.red

2. Null-aware operator

Normal way -

int money;
if(money == null){
money = 0;
}

Smarter way -

int money;
money ??=0;

3. Ternary Operator

Normal way -

String env;
if (temp > 35){
env = "hot";
}else{
env = "cold";
}

Smarter way -

String env = temp > 35
? "hot"
: "cold";

4. Assert statement

Normal way -

if(x == null){
throw AssertionError("x is null");
}

Smarter way -

assert(x !=null , "x is null")

5. Getters

Normal way -

List mylist = [1,2,3,4,5];
int len = mylist.length;
int length(){
return len;
}

Smarter way -

List mylist = [1,2,3,4,5];
int get length(){
return mylist.length;
}

These were the 5 tips.
Hope you like them.

Top comments (0)

Image of Timescale

πŸ“Š Benchmarking Databases for Real-Time Analytics Applications

Benchmarking Timescale, Clickhouse, Postgres, MySQL, MongoDB, and DuckDB for real-time analytics. Introducing RTABench πŸš€

Read full post β†’

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay