This is a subjective question as I want to gauge if it's worth me moaning at my co-workers for doing something which I find utterly detestable.
The issue is that a bunch of my co-workers will truncate method calls to fit a width. We all use widescreen laptops that can handle large resolutions (mine is 1920x1200) and when it comes to debugging and reading code I find it much easier to read one line method calls as opposed to multiple line calls.
Here's an example of a method (how I would like it):
IReallyLongInterfaceName instanceOfInterfaceName = OurContainer.retrieveClass(IReallyLongInterfaceName.class, param1, param2, param3);
(I do hate really long interface/class names as well :)
It seems that this doesn't render well on StackOverflow, but I think most of you know what I mean. Anyway, some of the other devs do the following.
IReallyLongInterfaceName instanceOfInterfaceName = OurContainer.retrieveClass(IReallyLongInterfaceName.class,
param1,
param2,
param3);
Which is the easier to read at the end of the day for you and would I be unreasonable in asking them to use the first of the two (as it is part of our standard)?
I find the first example more readable in general, though if it is longer than some predefined limit (120 characters, for me), I would break the lines:
IReallyLongInterfaceName instanceOfInterfaceName =
OurContainer.retrieveClass(IReallyLongInterfaceName.class,
param1, param2, param3);
Maybe you should have as part of your standard build process some sort of checkstyle plugin which checks for exactly that kind of thing? If you've agreed the standard with your co-workers it seems reasonable to ask them to keep to it.
I personally find the second of the two options the more readable, but that's just because I don't have a widescreen monitor ;)
If its exlicitly stated in the companies coding standard that method one is the correct method then by all means moan at them, after all they are not adhering to the company standards.
If its not exlicitly stated then I guess now would be a good time to get it into the standard.
One thing to be aware of though, if you are using an IDE with autoformatting is that it may take it upon itself to reformat the methods to style 2 when its run.
So even if everyone is writing to style 1, it may not end up looking like that when they are finished with it.
and like Phil, I find method 2 much more readable, since you can see everything you need to see without having to scroll your eyes sideways :)
I prefer the second example. Even though you may have widescreen laptops, you might not always have windows full screen, or in your IDE you may have a lot of other panels around the main coding area that reduce the available width for displaying code.
If the line can't fit without scrolling, then vertical scrolling is preferable to horizontal scrolling. Since we read left-to-right, horizontal scrolling would mean moving backwards and forwards all the time.
I prefer one parameter per line to Avi's suggestion, which is arbitrary to me. If you spread the parameters over multiple lines but have several on each line, it makes it more difficult to find particular parameters when reading the code.
I prefer option #2, as well. The issue isn't just how it looks on screen (and if I had 1920 horizontal pixels, I'd have a lot more docked windows), it's how it looks if I need to print it and read it. Long lines will print terribly out of most IDEs, whereas lines broken by an author with the intent to improve legibility will print well.
Another point is general legibility. There's a reason magazines and newspapers are printed in columns -- generally, readability of text (particularly text on-screen) is improved by shorter lines and better layout/formatting.
I think 80 might be overly arbitrary, but I'm using 10pt Consolas, and I seem to be able to get about 100 characters per line on a standard 8.5" printed page.
Now at the end of the day, this is a holy war. Maybe not as bad as where to put your curly braces, but it's up there. I've given you my preference, but the real question goes back to you: What's your company's standard? It sounds to me like they've standardized on option #2, which means for the sake of the team, you should probably adapt to them.
I prefer option 2, but optionally with comments for parameters where the variable name is not obvious. When you have a function call that is asking for a bunch of parameters, it can be pretty hard for reviewers to tell what the code is doing.
So, I generally code like this if there are more than 3 parameters to a given function:
applyEncryptionParameters(key,
certificate,
0, // strength - set to 0 to accept default for platform
algorithm);
Related
Greeting, wise ones!
I am trying to make a generator for pictures like this one. My idea is to make 2 patterns (vertical lines and horizontal lines). After that, I need to make vertical lines only appear "within" the letter but go a bit beyound if they don't intersect a horizontal line. Same for horizontal line, just for being "outside" the letter.
To perform this I need to know, which pixels are "within" letters of the text() object and which are not. This is the only thing, that I can't get my head around. Any ideas on how to implement this?
(If you have a simpler idea of how to make this generator, I'll happily read about them as well, I'm not too sure that mine is the best)
TL;DR: solution
So, initially, I was doing it in Java Processing and was thinking in terms of points and "if" conditions. I couldn't get any meaningful help and abandoned the problem, doing the thing by hand.
However, later I encountered a problem with detecting if a point is within a polygon in Unity and found a solution that includes the concept of raycasting. It's easily implemented in Unity but will require some extra work in something like Java Processing. In any case, this is an excellent answer to my question. I hope it helps anyone who encounters a similar problem.
I'm trying to grab some data about the source code file and the node. If I want to capture source line number (int line = ctx.getStart().getLine();) if it goes in exitEveryRule am I correct in thinking that this will execute each time a rule ends? I saw another post that seemed to indicate this was more for errors.
I started to add code to each of the rules but in Java, that's a lot of rules. Seems like I should simply use this and be done. But before I delete and retry, any issues with that approach?
It did in fact work but there were some interesting dependencies on the grammar. It worked as a first effort, but we found we needed to extend the grammar is a few minor ways to get a consistent output. Most were "intermediate" steps so we could create a consistent. So it does work, and it does what you would expect but in the end, we needed to extend it a bit anyway.
I am currently working on a program that preforms techniques such as image segmentation along with a few others. However I have the task ahead of me of filling a segmented area (this will be a blank area) based on its surrounding pixels.
This is a lot like what photoshop likes to call content aware fill, however me being only one person am wondering the best way I could approach this type task. Also how I should start to think about getting something, obviously not as technical and robust, but similar in some sense to work.
I am not currently aware of any classes that may help with something like this but any help on this would be greatly appreciated.
You are after Inpainting. There are many ways to do it, and an Inpainting survey by BertalmÃo, Caselles, Masnou, Sapiro - 2011, presents lots of results and references about them.
Around here you will also find sample results using the technique, for instance at https://stackoverflow.com/a/13666082/1832154 you will see one together with http://www.cc.gatech.edu/~sooraj/inpainting/ that gives a complete implementation. At https://stackoverflow.com/a/14295915/1832154 you can see another sample result, together with a simplistic reference to a different inpainting method.
I'm beginning to write a source-code editor in Java as a personal project/hobby. Before I lead myself down any awful paths, I would like to ask a few questions:
Is a JTextPane an appropriate component to use for editing the text? As far as examples I have seen go, it looks like it could become very troublesome and perhaps even very SLOW/memory inefficient to style text in large files, as String arrays are used in styling, which could become very large very quickly. (Note: I require the ability to insert components, such as images, in addition to text in my editor. I have only seen the ability to do so in JTextPane.)
Would internally marking up the text with XML/HTML and using something as simple as a JPanel to render it be a more computationally/memory efficient, albeit less simple, route?
When loading the source of the file into a program, would a better design choice be to load the entire file, or load only what is being viewed, plus a small to moderately-sized buffer?
Any other advice as I embark on a long journey of a hobby would be greatly appreciated!
Q1: Have you tried it? Its certainly enough to start with, and you'll run into other issues along the way. Just try not to design around it exclusively. i.e. don't program yourself into a corner so you can switch later if you want.
Q2: I doubt it.
Q3: Load the entire file into memory - hands down. Your computer has a lot of RAM, so use it.
Q4: Try several different quick prototypes. There's nothing wrong with a few failed attempts if you don't invest too much in them and you learn a lot when you do them.
Just wondering if there are a set of design patterns for complex string manipulation?
Basically the problem I am trying to solve is I need to be able to read in a string, like the following:
"[name_of_kicker] looks to make a clearance kick, but is under some real pressure from the [name_of_defending_team] players. He gets a [length_of_kick] kick away, but it drifts into touch on the full."
or
"[name_of_kicker] receives the ball from [name_of_passer] and launches the bomb. [name_of_kicker] has really made good contact, it's given a couple of [name_of_attacking_team] chasers ample time to get under the ball as it comes down."
And replace each "tag" with a possible value and check if the string is equal to another string.
So for example, any tag that represents a player I need to be able to replace with anyone of 22 string values that represent a player. But I also need to be able to make sure I have looped through each combination of players for the various tags, that I may find in a string. NOTE, the tags listed in the above 2 samples, are not the only tags possible, there are countless other ones that could come up in any sentence.
I had tried to create a load of nested for loops to go through the collection of players, etc and attempt to replace the tags each time, but with there being many possibilities of tags I was just creating one nested for loop within another, and it has become unmanageable, and also I suspect inefficient, since I need to loop through over 1,000 base string like the samples above, and replace difference tags with players, etc for each one...
So are there any String manipulation patterns I could look into, or does anyone have any possible solutions to solving a problem like this.
Firstly, to answer your question.
Just wondering if there are a set of design patterns for complex string manipulation?
Not really. There are some techniques, but they hardly qualify as design patterns. The two techniques that spring to mind are template expansion and pattern matching.
What you are currently doing / proposing to do is a form of template expansion. However, typical templating engines don't support the combinatorial expansion that you are trying to do, and as you expect anticipate, it would appear to be an inefficient way to solve your problem.
A better technique would appear to be pattern matching. Let's take your first example, and turn it into a pattern:
"(Ronaldino|Maradonna|Peter Shilton|Jackie Charlton) looks to make a clearance kick, but is under some real pressure from the (Everton|Real Madrid|Adelaide United) players. He gets a ([0-9]+ metre) kick away, but it drifts into touch on the full."
What I've done is insert all of the possible alternatives into the pseudo-template, to turn it into a regex. I can now compile this regex to a java.util.Pattern, and use it to match against your list of other strings.
Having said that, if you are trying to do this to "analyse" text, I don't rate your chances of success. I think you would be better off going down the NLP route.
What you're describing looks a bit like what template engines are used for.
Two popular ones for Java are:
FreeMarker
StringTemplate
But there are many, many more, of course.
MY two cents,As you stated "I was just creating one nested for loop within another, and it has become unmanageable,"
You are looking in the wrong direction my friend there is whole universe of solutions to the problem you are facing ,simply know as a rule engine.
There are various type of rule engines(business rule engines,web template engines etc.) but for above requirement i suggest business rule engines.
Can't comment on which one to use as it depends upon
Multi-threading.
Open Source/Commercial.
Load factor/Processing time etc.
Hope it helps
http://ratakondas.blogspot.in/2012/06/business-rules-engines-white-paper.html
[read the summary section it gives best advice.]
https://en.wikipedia.org/wiki/Business_rules_engine#Types_of_rule_engines
https://en.wikipedia.org/wiki/Comparison_of_web_template_engines
Welcome to the world of rule engines :)