+ 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.
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;
+ 6
Less lines of code is usually a good thing in terms of code quality metrics, as long as it stays readable.
+ 4
you can remove the ? a:b, just do return a>b;
Note : this will return boolean