Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing { } to segment large blocks of code to improve code-readability - Good practice?
    text
    copied!<p>I'm considering the option of using anonymous <strong>{ }</strong> code blocks to logically distinguish "code blocks" inside the same method call, something that (theoretically) should improve readability of the code.</p> <p>I'm wondering which of the following 2 code segments is better to your eyes?</p> <p>Also, are the 2 code segments compile to the same bytecode?, In other words, can using <strong>{ }</strong> hurt in any way the performance of the code?</p> <h2>Option 1: Code block without { } identation</h2> <pre><code> public static String serviceMatch(HttpServletRequest servletRequest, RequestTypeEnum requestTypeEnum, ...censorsed..., RequestStatistics requestStatistics) { Request request; // We get the parser that fits the ...censorsed..., effectively transforming the HTTPReqeuest to application local "Request*" object RequestParser parser = RequestParserFactory.getParser(...censorsed...); // Populate basic parameters, the "heavy" data will be lazy loaded request = parser.parse(servletRequest); // Instead of polluting the parsers let's put it here... (unless we identify meaningful justifications for the other alternative of changing RequestParser.parse() interface. request.requestType = requestTypeEnum; // Store the request statistics object on the request, so that we have access to it from all over the code request.requestStatistics = requestStatistics; // Update timestamp when request was parsed request.requestStatistics._1_end_parseRequest = System.currentTimeMillis(); /* * ...censorsed... */ MatchResult matchResult = Matcher.findMatch(...censorsed...); /* * ...censorsed... */ String reply = ReplyFormatFactory.getFormatter(...censorsed... // Update timestamp when reply finished construction request.requestStatistics._6_end_formatReply = System.currentTimeMillis(); return reply; } </code></pre> <h2>Option 2: Code block with { } identation</h2> <pre><code> public static String serviceMatch(HttpServletRequest servletRequest, RequestTypeEnum requestTypeEnum, ...censorsed..., RequestStatistics requestStatistics) { Request request; /* * Request parsing block */ { // We get the parser that fits the ...censorsed..., effectively transforming the HTTPReqeuest to application local "Request*" object RequestParser parser = RequestParserFactory.getParser(...censorsed...); // Populate basic parameters, the "heavy" data will be lazy loaded request = parser.parse(servletRequest); // Instead of polluting the parsers let's put it here... (unless we identify meaningful justifications for the other alternative of changing RequestParser.parse() interface. request.requestType = requestTypeEnum; // Store the request statistics object on the request, so that we have access to it from all over the code request.requestStatistics = requestStatistics; } // Update timestamp when request was parsed request.requestStatistics._1_end_parseRequest = System.currentTimeMillis(); /* * ...censorsed... */ MatchResult matchResult = Matcher.findMatch(...censorsed...); /* * ...censorsed... */ String reply = ReplyFormatFactory.getFormatter(...censorsed... // Update timestamp when reply finished construction request.requestStatistics._6_end_formatReply = System.currentTimeMillis(); return reply; } </code></pre> <p>Thanks for the review, Maxim.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload