برچسب: names

  • How to choose meaningful names? | Code4IT


    Just a second! 🫷
    If you are here, it means that you are a software developer.
    So, you know that storage, networking, and domain management have a cost .

    If you want to support this blog, please ensure that you have disabled the adblocker for this site.
    I configured Google AdSense to show as few ADS as possible – I don’t want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.

    Thank you for your understanding.
    Davide

    One of the fundamentals of clean code is: use meaningful names.

    But choosing meaningful names takes time!

    Time spent finding good names is time saved trying to figure out what you meant.

    How to approach it? Good names do not come on the first try!

    My suggestion is: at first, write down the code as it comes.

    public static string Get(string input)
    {
      char[] arr = new char[input.Length];
      int i = input.Length - 1;
      foreach (var e in input)
      {
        arr[i] = e;
        i--;
      }
    
      return new String(arr);
    }
    

    And then, when you have almost everything clear, choose better names for

    • classes
    • methods
    • parameters
    • variables
    • namespaces
    • libraries
    public static string GetReversedString(string originalString)
    {
      char[] reversedChars = new char[originalString.Length];
      int currentIndex = originalString.Length - 1;
      foreach (var currentChar in originalString)
      {
        reversedChars[currentIndex] = currentChar;
        currentIndex--;
      }
    
      return new String(reversedChars);
    }
    

    Probably, you’ll never reach perfection. Sad, but true.

    You might want to add some tests to your code, right? RIGHT??

    A good moment to choose better names is while writing test: at that moment your tests act as Clients to your production code, so if you find that the name of the method does not fully represent its meaning, or the parameter names are misleading, this is a good moment to improve them.

    And don’t forget about private variables and methods!

    So, what is “a good name”?

    A good name should express:

    • its meaning (what a method does?)
    • its scope (for items in loops, even var i = 0 is acceptable, if the scope is small)
    • what it represents (originalString is, of course, the original string)

    👉 Let’s discuss it on Twitter or on the comment section below!

    🐧

    This article first appeared on Code4IT





    Source link

  • Use pronounceable and searchable names | Code4IT


    Just a second! 🫷
    If you are here, it means that you are a software developer.
    So, you know that storage, networking, and domain management have a cost .

    If you want to support this blog, please ensure that you have disabled the adblocker for this site.
    I configured Google AdSense to show as few ADS as possible – I don’t want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.

    Thank you for your understanding.
    Davide

    Ok, you write code. Maybe alone. But what happens when you have to talk about the code with someone else? To help clear communication, you should always use easily pronounceable name.

    Choosing names with this characteristic is underrated, but often a game-changer.

    Have a look at this class definition:

    class DPContent
    {
        public int VID { get; set; }
        public long VidDurMs { get; set; }
        public bool Awbtu { get; set; }
    }
    

    Would you say aloud

    Hey, Tom, have a look at the VidDurMs field!
    ?

    No, I don’t think so. That’s unnatural. Even worse for the other field, Awbtu. Aw-b-too or a-w-b-t-u? Neither of them makes sense when speaking aloud. That’s because this is a meaningless abbreviation.

    Blah blah blah

    Avoid using uncommon acronyms or unreadable abbreviations: this helps readers understand better the meaning of your code, helps you communicate by voice with your colleagues or searching for a specific field using your IDE

    Code is meant to be read by humans, computers do not care about the length of a field name. Don’t be afraid of using long names to help clarity.

    Use full names, like in this example:

    class DisneyPlusContent
    {
        int VideoID { get; set; }
        long VideoDurationInMs { get; set; }
        bool AlreadyWatchedByThisUser { get; set; }
    }
    

    Yes, ID and Ms are still abbreviations for Identifier and Milliseconds. But they are obvious, so you don’t have to use complete words.

    Of course, all those considerations are valid not only for pronouncing names but also for searching (and remembering) them. Would you rather search VideoID or Vid in your text editor?

    What do you prefer? Short or long names?

    👉 Let’s discuss it on Twitter or on the comment section below!

    🐧





    Source link