0

I'm curious about this. I've recently had the idea of isolating segments of code using the curly braces for both visual organization and to isolate the variables to that specific scope (if only to keep them from cluttering the suggestions in Eclipse in larger functions). For instance:

public void startInstall()
{
    boolean success = false;
    m_progress = 0;

    // Determine Chance of Success (This is isolated in curly braces)
    {
        double weight_total = 0;
        double weight_sum = 0;
        for(int i=0; i < Data.m_feature_selection.size(); i++)
        {
            if(Data.m_feature_selection.get(i))
            {
                int weight = Data.m_feature_weight.get(i);
                weight_total += Math.abs(weight);
                weight_sum += weight;
            }
        }
        double succ_chance = (weight_sum / weight_total) + 0.15;
        if(Math.random() <= succ_chance)
            success = true;
    }
    // Do more stuff....
}

Does this affect performance? Is it against convention? Would doing this be frowned upon in a professional environment?

Luke_G
  • 5
  • 1
  • 4
    I don't think that this is a very good idea. Instead, isolate code in methods/classes and such – Hovercraft Full Of Eels Oct 20 '19 at 17:55
  • 2
    I could do this only to isolate local variables, nothing else - but still - cannot recall to ever doing it. – Antoniossss Oct 20 '19 at 17:57
  • 1
    I don't think it would affect performance - it would be treated as a method to all intents and purposes. I would suggest you make it an actual method rather than abuse the language. You'll be naming the blocks and adding named `break` statements next... – Boris the Spider Oct 20 '19 at 17:58
  • @BoristheSpider, not quite - it has access to local variables, while method doesn't. – Lev Leontev Oct 20 '19 at 18:00
  • @LeoLeontev fair. That may make a difference under certain circumstances. – Boris the Spider Oct 20 '19 at 18:03
  • @BoristheSpider &LeoLeontev This is the main reason I did this, because it allows me to access the local variables while also isolating the code segment itself similar to a method. I also figure, if the code is only being used once, right here, and nowhere else, then why should I make it a method, other than for organization? Honestly, the code in this whole program is messy. I'm not trying to make it nice, I'm just rushing to get it done so I can use it for a project in my creative writing class (oddly enough). – Luke_G Oct 20 '19 at 19:02

1 Answers1

3

If you need to do that, you should break the block out into a method.

Further, comments are a code smell. In almost every case, if you have to comment your code, it's poorly written:

Turn your comment into a method name!

double determineChanceOfSuccess() {
    double weight_total = 0;
    double weight_sum = 0;
    for(int i=0; i < Data.m_feature_selection.size(); i++) {
        if(Data.m_feature_selection.get(i)) {
            int weight = Data.m_feature_weight.get(i);
            weight_total += Math.abs(weight);
            weight_sum += weight;
        }
    }
    double succ_chance = (weight_sum / weight_total) + 0.15;
    return succ_chance;
}

Now your main code is readable!

public void startInstall() {
    m_progress = 0;

    double succ_chance = determineChanceOfSuccess();

    boolean success = Math.random() <= succ_chance;

    // Do more stuff....
}

Note slight code clean up too.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Moreover the way you access data to compute the chance is smelly too; looks like you get through a public static field of a class ?! You should consider adding an array as argument of the method, and calling it with the appropriate data. So you could then unit test the logic you put in your computation with arbitrary data. – Michael Zilbermann Oct 20 '19 at 18:45
  • I disagree about the comments - as those not nececerly have to describe the code, but eg reasoning behind it - bussiness rules, explanation why other, maybe more obvious solution will not work here etc. – Antoniossss Oct 20 '19 at 18:53