+ 3

Shorter code

I'm a lazy person(but I love writing many codes :D) so i prefer to wirte shorter code(but not always). I want to know shorter implementation of some fragments codes. For example: if(a>b) return a; else return b; I can write as: return a>b?a:b; 1. What do you think about this way to writing code? 2. If you know other examples to write shorter code, please impart your knowledge.

16th May 2017, 8:36 PM
Wojciech Bruski
Wojciech Bruski - avatar
3 Antworten
+ 8
Doing the latter instead of the former is perfectly fine. As @Helioform has mentioned, as long as it stays readable. Less lines are only better when a more efficient way of coding is introduced to the project, as in the example your provided. i.e. compare if(a>b) return a; else return b; and return a>b?a:b;
17th May 2017, 2:33 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
Less lines of code is usually a good thing in terms of code quality metrics, as long as it stays readable.
16th May 2017, 10:36 PM
Karl T.
Karl T. - avatar
+ 4
you can remove the ? a:b, just do return a>b; Note : this will return boolean
16th May 2017, 8:42 PM
hawk
hawk - avatar