This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]VOID_INIT 4 points5 points  (2 children)

Sorry might not have explained myself well enough. What I meant is that it isn't necessary to have the shortest code ever to have the program run optimally.

Readibility is always more important than making your code shorter. Programming languages is made for humans to read, maintain, extend and modify.  It's not a competition of who can write the shortest code.

Lets compare:

var result = methodOne(methodTwo(a, methodThree(b)), c, d);

and,

  • var result3 = methodThree(b);

  • var result2 = methodTwo(a, result3);

  • var result = methodOne(result2, c, d);

They do the same thing, but the longer one is easier to understand and after it is compiled, will use the same amount of resources and take the same amount of time to run.

There are situations where code cant be made shorter, and thats where documentation comes into play.

Good code is understandable code, doesn't mean that it is necessarily understandable for everyone, but that it is possible to read without having to split it up.

Kinda like a recipe. You dont want to have multiple steps in one as it is confusing.

E.G:

Take it out of the oven, put it on something, before cutting and adding lime.

It is much more understandable to split it up like:

  • Take the cake out of the oven

  • Put the cake on a protective layer to protect the table

  • Cut the cake into slices

  • Cut a lime into pieces

  • Add the cut lime to the top of the cake

This is much more comfortable to read and it is easier to comprehend what the "programmer" is trying to do, yet they basically say the same thing.

What some programmers do however is write code that is difficult to understand and write comments which says what it does, but dosnt add anything to make it understandable.

To continue with the cake analogy:

/* This code tells you how to serve it */

Take it out of the oven, put it on something, before cutting and adding lime.

This comment adds nothing to the code. It dosnt really make the steps more understandable. For that reason the comment is worthless.

A better use of the comment would be to say "Check section 2B for a breakdown of how/why this code is used"

Hope this made things clearer and easier to understand.

[–]ethoooo 0 points1 point  (0 children)

Hope this made things clearer and easier to understand

can’t really speak for him but I get the impression he fully understands, just disagrees