Please write expressive code instead of oneliners

It makes it easier for us DSA beginners to understand your solutions better.

Which one do you think is easier to read and understand what is happening in the code?

Example 1:

function sortedSquares(nums: number[]): number[] {
  let left = 0;
  let right = nums.length - 1;
  const result: number[] = [];

  while (left <= right) {
    const sqLeft = nums[left] ** 2;
    const sqRight = nums[right] ** 2;

    if (sqLeft > sqRight) {
      result.unshift(sqLeft);
      left++;
    } else {
      result.unshift(sqRight);
      right--;
    }
  }

  return result;
}

Example 2:

function sortedSquares(nums: number[]): number[] {
  let l = 0; let r = nums.length - 1; const rr: number[] = [];
  while (l <= r) rr.unshift(nums[l] ** 2 > nums[r] ** 2 ? nums[l++] ** 2 : nums[r--] ** 2);
  return rr;
}

My example is perhaps a bit of an exaggeration, but I think you understand what I mean.

Don't use a bad coding style as an excuse to make your solution X lines shorter and use descriptive variable names, if possible.

Comments (1)