Skip to main content

Maintainablilty

A question:

What does the following code do? 
public static string GetThing1(string s) 

     return s.Substring(s.Length / 2 - (1 - s.Length % 2), 2 - s.Length % 2); 
}

Given that code, take a look at the following, what doe this code do?

public static string GetThing2(string s)
{
    //Given a string return the middle char if odd, middle 2 if even
    string retVal = "";
    bool even = (s.Length % 2 == 0) ? true : false;
    int middle = (s.Length % 2 == 0) ? s.Length / 2 - 1 : s.Length / 2;
    if (even)
        retVal = s.Substring(middle, 2);
    else
        retVal = s[middle].ToString();
    return retVal;
}


It probably didn't take too long to figure out they both do the same thing. They are two different answers to the following question:
Write a function to get the middle character of a string. Return the middle character if it the length is odd, or the middle 2 characters if the length is even.
Which version took less time to understand? Personally, I found the second version easier to understand. I also figure it will be easier to debug. 

And so?

I happened across these two solutions at CodeWars.com. The first answer was voted a best practice.  
Isn't maintainability a best practice? Isn't clarity a tool for enhancing maintainability? I think that under the assumption that the code I write will be looked at later by someone who has no idea what the code needs to do, I need to leave as many markers as possible. I need to be explicit in my coding. This might be an extreme example. The second version could be re-written to be more concise - especially to try and get rid of some of the extra memory used. 

But what about making the fact that the return is value is a "this" or "that" known explicitly? The intent is obscured in the one-liner. This becomes a more interesting trade-off - will this routine be called frequently? How much are we paying in performance for the conditional? Does it need to be streamlined? How do you keep maintainability and meet that goal? 

As Abelson and Sussman phrase it in “Structure and Interpretation of Computer Programs,” “Programs must be written for people to read, and only incidentally for machines to execute.”
(quote from Code Watch: Let's be clear about code clarity)
 
These are the questions I am asking myself about this code - what about you?

Comments

Popular posts from this blog

Unit Testing - What to Test

This I wrote to answer a question that came up when we were discussing our software process and I was training developers on how to unit test. It seems a simple enough question, but I kept pondering it and delving deeper until I realized I needed to write this monograph. What unit tests should we write? How do we know what to test? Ideally, unit tests should cover every path through the code. It should be your chance to see every path through your code works as expected and as needed. If you are practicing Test Driven Development then it's implied everything gets a test. In the real world, you might not be allowed to test everything - for instance, if the testing suite ends up taking a week to run, then the world will have changed by the time it finishes and the test results will be obsolete. Unit testing at it's basic is testing an object, a method - the smallest unit of your code that it can test independently. It should test the inputs "goes into" an...

Risk Mitigations for Custom Applications

  In many healthcare applications, often due to the cloistered nature of the use cases – e.g. it will only be accessed by users authorized in a particular facility, such as an operating room suite – the needs for Authentication and Authorization are minimized when the system is designed and implemented. This presents a risk as soon as you allow for the possibility of users with ill-intent or that otherwise want to operate outside their given roles. Custom applications need to consider these possibilities and implement the following measure to ensure the integrity of the system. 1.   Authentication and Authorization Controls: Multi-Factor Authentication (MFA): Implement MFA for all user logins. This adds an extra layer of security beyond just a username and password. Role-Based Access Control (RBAC): Grant users access only to the data and functionalities they need for their specific role. This minimizes the potential for unauthorized access. Strong Password Policies: ...

You don't really know who you're talking to online...

The following is a story that I think highlights the assumptions that get you into trouble online... https://www.proofpoint.com/us/blog/threat-insight/i-knew-you-were-trouble-ta456-targets-defense-contractor-alluring-social-media This is particularly scary since we found so much utility in online connections during the pandemic and out of necessity, started trusting more online. Please note the timeline for this breach - it was a long, slow process, a key factor in many 'cons'. "Build trust" is a key first step, once someone has identified you as a party. You think...you're convinced you know who your talking to, but if you don't triangulate the identity with some non-online, ideally in-person information, you shouldn't trust. And even if you do get what seems like real-life confirmations of identity, you must look at questioning motives, needs, and keeping danger at arms-length. Online includes email, texting (sms), application chatbots, voice communicati...