📜 ⬆️ ⬇️

How to clean your karma code?

Does it often happen to you that you meet a bad code and mentally scold the author?
And then come to the conclusion that the code is not so bad, and the author was scolded in vain ... But the residue remained!

In general, I was meditating just before the monitor, and visited me such is the code:

class Graph
{
/**
* retrieve min value for y-axis
*/

public int getYaxisMin () {
int result = getMinAmount();
result = result / 100 * 100 ;
return result;
}

/**
* retrieve max value for y-axis
*/

public int getYaxisMax () {
int result = getMaxAmount();
result = ( result + 100 ) / 100 * 100;
return result;
}

// , , , 2010 ,
// -, .
// -!

...
}


')
Following this code, of course, I get Negative Emotion . Perplexity
Why it is necessary to divide by one hundred and then multiply by one hundred?
Why do we need to add a hundred and make a hundred? What is going on here? WTF?

Solution

During the debug, it turns out that this code works fine. Since we are talking about integers, when dividing by 100, the result is rounded, and when you multiply by 100, a smaller number is obtained. For example, from 666 it is obtained (666/100) * 100 = 6 * 100 = 600. That is, the value is rounded to the nearest hundred.

What is the result? The code seems to be there, it seems to work, that is, there is nothing to blame the programmer. But at the same time, I spent the strength to figure it out, and most importantly - I said aloud " What The Fuck " which increased my karma by one.

Karma - the cosmic law

In Mahabharata there is such a thing as karma code. The karma of the code is blacker the more a person says “ What the Fuck ”, looking at it. The Vedas say that the language in which the code will be implemented in the next reincarnation depends on the number of WTFs caused in the last implementation. If about the code at least once said "What the fuck", then the next time it definitely does not have to be implemented on Scala, if they said "What the fuck" five times, then Java will shine for him the maximum, and if 20 times more, then to be born to it in PHP or even Perl'e.

Getting rid of karma

How to clear the karma of your code?

As stated in the Vedic texts, any programmer can change the fate of the code if on the day of Bhadra Purnima he discovers the scripture “ import org.junit.Test ” and writes the cleansing mantra:
public class GraphTest
{
@Test
public void yaxisMinTruncatesToLower100 ()
{
assertEquals( 0 , new Graph( 48 , 415).getYaxisMin());
assertEquals( 100 , new Graph( 100 , 415).getYaxisMin());
assertEquals( 900 , new Graph( 914 , 415).getYaxisMin());
}

@Test
public void yaxisMaxTruncatesToUpper100 ()
{
assertEquals( 100 , new Graph(48, 96 ).getYaxisMax());
assertEquals( 200 , new Graph(48, 100 ).getYaxisMax());
assertEquals( 500 , new Graph(914, 415 ).getYaxisMax());
}
}



By the way, the Shastras recommend also to clean up their own karma, deleting indecent comments and making the code three times shorter:
class Graph
{
...
public int getYaxisMin() {
return getMinAmount() / 100 * 100 ;
}

public int getYaxisMax() {
return getMaxAmount() + 100 ) / 100 * 100 ;
}
...
}


Well, now the karma of your code is a bit cleared. But on this our training is not legal. We are only at the beginning of the Path. Next time we will look at why our code needs to confess every month and pass the sacrament. And it's better - every day, ideally - even hourly. In ancient writings, this process is called Continuous Integration .

And today the lesson is over.

We summarize the knowledge gained:

Think with your head, do not believe in all garbage - and you will reach the highest transcendental abode!


So it goes.





Source: https://habr.com/ru/post/99525/


All Articles