Toribash
Original Post
Java Snippet
Playing around with Swing and string parsing, finally bothered getting this finished.

It factors any factorable trinomial.

Not really an experienced dev, so any advice on optimization or what I'm doing stupidly would be great.

Runnable .jar file here: http://www.mediafire.com/?jol41bkku9v0mqi

Code in spoiler below.

Full code



Edit: reviewing this, it seems somewhere along the way something didn't save, this version isn't compatible with negative numbers. Will fix soon.
Last edited by Boredpayne; Jul 18, 2011 at 02:27 AM.
Buy TC for a great price here! http://forum.toribash.com/showthread.php?t=240345
Buy VIP and Toriprime for a great price here! http://forum.toribash.com/showthread.php?t=237249


Hey look more than two lines.
Nicely done! I can see a lot of minor semantic/grammatical errors, but the program runs nicely. It is also good that you are looking to optimisation, just be aware that in this program it really isn't needed I also like how you didn't use OO, since it is not necessary.


The first error I see is in naming conventions;

Naming conventions



Spacing



Shorthand



Repetition



Need for modules





It is nice, but as you can see there is a lot of room for improvement.

I suggest reading about coupling and cohesion (two topics that I didn't even really touch on, but will be hugely useful).
If you want to learn some higher level design, you could also take a look at some Design Patterns.
Originally Posted by Gorman View Post
Nicely done! I can see a lot of minor semantic/grammatical errors, but the program runs nicely. It is also good that you are looking to optimisation, just be aware that in this program it really isn't needed I also like how you didn't use OO, since it is not necessary.

Thanks. I meant more like finding the coefficients exponents more quickly, but looking over it there really isn't any way to improve it.

Originally Posted by Gorman
The first error I see is in naming conventions;

Naming conventions


Somewhere I got the bad habit of naming String variables starting with a capital. They should probably be renamed test1, test2, etc.

I'll get around to renaming the class TrinomialFactorer.

I only put the method name Trinomial in caps because I call the JTextArea for input trinomial. This is probably poor style, and I'll rename trinomial to trinomialInput and Trinomial to trinomial.
Originally Posted by Gorman

Spacing


Will do, and will also add spacing before/after &&'s in if statements.
Originally Posted by Gorman

Shorthand


I've never really felt happy doing this, but if it's convention I'll change it.
Originally Posted by Gorman

Repetition


I was actually thinking of using inline conditionals, but that's simply not possible if I want to add compatibility for inherent coefficients of one, such as in x or -x.

I know the entire section is repetitive, but the only way I could think to fix it was to create a for loop and array of two strings, and it seemed like more effort than it was worth given the level of indentation I was already using.
Originally Posted by Gorman

Need for modules


Well, it does work independently, but it recycles 4 different strings from the other part. Also, the block of text applies to an earlier section, where it bruteforces two numbers here: Java code:

topBox=cTerm*coefficient;
System.out.println(topBox+" "+bottomBox);
done:for (r=Math.abs(topBox);r>=Math.abs(topBox)-(2*Math.abs(topBox));r--){
for (t=Math.abs(topBox);t>=Math.abs(topBox)-(2*Math.abs(topBox));t--){
if (t*r==topBox){
if (r+t==bottomBox){
break done;
}
}
}
}
if (coefficient==1){
return (q+"(x+"+r+")(x+"+t+")");
}

I suppose I should move the block of text up before that, and I could make this and all later code two separate functions, but I just don't see the point in splitting up these two parts. It almost seems like a waste of memory. Are small functions just a way of making the code more understandable?

Originally Posted by Gorman
It is nice, but as you can see there is a lot of room for improvement.

I suggest reading about coupling and cohesion (two topics that I didn't even really touch on, but will be hugely useful).
If you want to learn some higher level design, you could also take a look at some Design Patterns.

I'll be updating it soon.

Reading up coupling and cohesion too, thanks for the feedback.
Buy TC for a great price here! http://forum.toribash.com/showthread.php?t=240345
Buy VIP and Toriprime for a great price here! http://forum.toribash.com/showthread.php?t=237249


Hey look more than two lines.
"I've never really felt happy doing this, but if it's convention I'll change it."
Well, not only that but it is also faster. I am not honestly sure if the Java compiler is smart enough to pick this up, but consider;
i = i / 2
and
i/=2

The first statement involves loading i, adding 1, then storing the answer. The second simply divides i by 2 (which is a built in instruction), thus it is more efficient. It is also 1 characters shorter (yay!) and more readable (ok, 50% of the reason why it is more readable is because it is convention :P).

So yeah, please do!



"Are small functions just a way of making the code more understandable?"
Well that is one of the benefits. Obviously function names describe the code within the function, so you can quickly understand a function by glancing at it and seeing what it calls;

public void DoSomething() {
int n = GetNumber();
DoStuffToNumber(n);
}

It also makes it easy to replace things. For example, I didn't really try too hard to understand your code, but from what I gather there are multiple algorithms that could be used to find the factorial. There is a particular "design pattern" called a "strategy", in essence it encapsulates an algorithm in an object, so that it is extremely modular and easy to change. This could benefit you, as if you separated it in to functions then you could easily swap the algorithm if you found it inefficient.

Consider this; imagine you are building a search engine, and initially you use merge sort to sort your list of pages.
If you have a separate function for the sort function, it is trivial to swap it to quicksort or radix sort or a BST. However if you had the whole thing in one big function, then it would be very hard to read and to find where you had to swap.

That is the biggest plus of modular design, it keeps things separate.




My explanation was kind of poor, but I hope you understand as much. You can find a better description online if you google it.