5 tips to write better Content (code) as a developer!

5 tips to write better Content (code) as a developer!

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler

image.png

Hey, heylow.

Thank you very much for stopping in.

I have been thinking of the best place to start my blog and there are a lot of things I have at heart I would love to write about but I have not had the push to make my first post.

Here we go, this is me starting my blog officially🤭

I would be writing about Human-Computer Interaction (mainly for the web and mobile.), 3d Interface Development, Animations in web and mobile design, cybersecurity and life hacks for developers.

I love sharing my knowledge and I would love to hear from amazing and curious developers like me.

I started this blog in the light of learning with you, my astounding reader.

Phew, that's out of the way.

phew.gif

My first post would be centred around the art of writing as a developer.

Yup! When I say writing, I do not mean writing texts but code.

As a software developer, I have always seen writing codes as something regular to do to get the app or product out. Thinking about writing codes that way has always been counter-productive and has always caused me to write unoptimized and unreadable code. I would be sharing techniques and methodologies that have helped me conquer this problem as a developer.

One of the things you don't want as a developer is to read code that is written poorly. We are not just coding writers. We change the world with our content (code). We make the world a better place with our code.

Yes, the users don't get to see your code but they get to feel the effect. They want to use the product of your code with a "God bless whoever made this" effect. Like I do whenever I use Canva (priceless).

giphy.gif

Heads up! You've got two people to serve. Your user and your colleagues (other developers).

This post is more about serving your colleagues super awesome code that can be modified and built on easily. Think of yourself more like a builder. You don't just write code for the sake of writing it. You are building a foundation that can be built on.

This is even more responsibility when you are building the first version of the software.

Here are 5 tips that have helped me write better code as a developer.

1. Write less misleading comments

One of the very amazing things that make our code readable is the comments but this can be a pitfall when used too much or inappropriately. When writing comments, you want to make sure they are super necessary and worthwhile. You also want to make sure that what the comment is tied to is not super dynamic. Take, for example, this function written in Js;

//this code calculates the correct subtotal for the cart.
let calculate = (items) => {
      let num = 0;

      items.forEach(item => {
           num += item.price;
      })

      return num + ( item.length * 4 )
}

You would agree with me that the code above gives the sense that this calculate function is to one specific thing. Calculate the SUBTOTAL price for the cart. Heads up! The function does not just do that, something is happening in the return statement. Another developer reading this function might get confused. Talking about misleading comments. Maybe at the start of the project, the function was intended to calculate just the subtotal and that is what the comments reflected but at some point, changes were made to the function and for some reason, you didn't get to update the comment. This is a mild example of what could happen when you use comments when not necessary. Comments are to be used when they are extremely needed.

What! Viceodev, how then do I write my code without comments? As I said, comments are to be used when necessary not totally avoided. Here are a few things to look out for when writing comments while coding.

image.png

  • Is it necessary?
  • Is the item being described dynamic?
  • Can I optimize my code to avoid the comment?
  • Would this comment become obsolete soon?

Taking the above into consideration would really help while doing your content(code) writing.

2. Name your variables, constants, and functions appropriately.

This is a super important hack for developers. As a developer, you would often be tempted to use acronyms and short forms when defining your variables.
One of the major things that have helped me write better code is naming my variables with the appropriate name no matter how long it is. Take, for example, the optimized version of the same calculate function we wrote before;

let calculate_subtotal_for_cart_with_charges = (items_in_cart) => {
      let subtotal = 0;

      items_in_cart.forEach( item_in_cart => {
           subtotal += item_in_cart.price;
      })

      let subtotal_with_charges = sub + ( item.length * 4);

      return subtotal_with_charges;
}

Just by changing the variable name, we have been able to eliminate the need for comments and the function is less ambiguous and more readable.

3. Separate your code as often as you can.

I guess this headline took you off-guard. When I say separating your code, I literally mean making your functions shorter by breaking tasks between functions. This really helps in writing reusable code and functions. It is also useful for avoiding nested control structures that overcomplicate our code. Also, break your code when too long into files if possible or classes. Let's take a practical example

function login($user_credentials){
    $field_check = Validate::validate($user_credentials, [
        'email' => 'required|email',
        'password' => 'required'
    ]);

    if($field_check->validated === true){
        $user_credentials_from_database = $db_connect->query('where', 'email', $user_credentials->email);

        if(count($user_credentials_from_database) > 0){
            if($user_credentials_from_database->banned === true){
                return 'Opps! Your account has been banned!';
            }else{
                /** then check if the user's password and email checks */
            }

        }else{
            return 'User not found!';
        }
    }else{
        return $field_check->message;
    }    
}

The above function obviously has a nested control structure and would still continue if there are more things to check. As a developer, you want to make sure your code is as readable as English (possibly😎). The above code can be optimized by writing some structures into functions like so;

function login($user_credentials){
    $field_check = validate_user_credentials($user_credentials);

    $does_user_exists = check_db_for_user($user_credentials);
    $is_user_banned = check_if_user_is_banned($user_credentials);

    /** then check if the user's password and email checks */  
}

The above functions seem definitely neater. What I did was abstract some structure into functions and then escape the individual function with an error when things don't check out (this could be optional as some people prefer having a controller).

We could even further optimize the code by using a class variable to avoid passing in the $user_credential variable as an argument always.

4. Use classes and do more object-oriented programming (OOP).

image.png

The need for OOP cannot be overemphasized. OOP was built majorly to help developers write less code and more reusable code. As a developer, with whatever programming language you learn, make sure to learn the OOP part of the language. Beautiful enough, OOP concepts are almost the same across all programming languages. Amazing right! You don't always have to learn new concepts, rather you just have to learn a new syntax.

Learning object-oriented programming gives you the power to abstract functionalities to classes you can extend to, mostly called utility classes. I would go into depth in my upcoming posts.

5. Optimize Your Code Often.

This is an obvious one but a lot of developers fall short of this. We write codes and then just add things to it and never optimize or refractor until it causes a mind-blowing setback. You think about it as a maintenance routine. You could do your code optimization once or twice every week.

Wait! There is more...

Just kidding, I hope you enjoyed reading this. Please drop your thoughts , I would love to hear them.