The Programming Language Tips
Internal company meeting script
Tips
Heavy and rewriting | static constructor function | CD-agent
Synchronization Agent | Resources | Memory Management
Heavy and rewriting
Problems
Day-to-day discussions, the terms are not unified bring some confusion
Used the expression
Example
Class Base (
)
Class Derived: Base (
)
Class Client (
Void Test (Base obj) (
Console.WriteLine ( "base");
)
Void Test (Derived obj) (
Console.WriteLine ( "derived");
)
Static void Main (string [] args) (
Base obj = new Derived ();
New Client (). Test (obj); / / output "base"
)
)
Static constructor function
Problems
1, in the tools category, there are usually no need to initialize the static method is called before, such as configuration information read
2, in the general category of the complex static information required in any case before the initialization method is called
I have seen solutions
1, in each of the static method call in all the necessary steps to initialize
(Public class SomeUtilClass
Private SomeUtilClass () (
)
Private static void Init () (
//….
)
Public static string GetUID () (
Init ();
Return uid;
)
Public static string GetConnectionString () (
Init ();
Return connString;
)
)
2, in the general structure function initializes
(Public class SomeMapperClass
Private static Hashtable types;
Public SomeMapperClass () (
If (types == null) (
Types = new Hashtable ();
Types.Add ( "RED" Color.Red);
Types.Add ( "GREEN" Color.Green);
Types.Add ( "BLUE", Color.Blue);
)
)
Public Color GetColor (string color) (
Return (Color) types [color];
)
)
I recommend solutions
Using the static structure function (C #), or static initialization block (Java)
[C #]
(Public class SomeClass
Static SomeClass () (
Init ();
Types = new Hashtable ();
Types.Add (…);
Types.Add (…);
)
)
[Java]
(Public class SomeClass
(Static
Init ();
Types = new HashMap ();
Types.put ( "", "");
Types.put ( "", "");
)
)
Effect
1, Once, only once
2, the definition of abnormal processing is required, reference can be standardized
2, multi-threaded, whether there is a problem, I do not know, talk about
CD-agent
Problems
There is a set internal targets, from the object to control the increase of its elements deleted, but customers need access to the pool is made of the information you want, and it is impossible for all target customers provide corresponding method, the need to return to the Internal set, but do not allow customers to add or delete elements
I have seen solutions
Set directly on behalf of the members return invoked only in the document can not require customers to set the element additions and deletions
(Public class SomeClass
Private List attrs;
Public List GetAttributes () (
Return attrs;
)
)
I recommend solutions
1, the preferred language functions
2, the second best class library provided by the function
3, their packaging Agency, or return to the depth of copy, or use of AOP
[C + +]
Class config
(
Public:
Const list <string> & get_attributes () (
Return attrs;
)
Private:
List <string> attrs;
);
[C #]
(Public class SomeClass
Private IList attrs;
Public IList GetAttributes () (
Return ArrayList.ReadOnly (attrs);
)
)
[Java]
(Public class SomeClass
Private List attrs;
Public List getAttributes () (
Return Collections.unmodifiableList (attrs);
)
)
Effect
1, the language features can help compiler in the period of inspections, to ensure that the process of additions and deletions even elements of the code does not exist, but, intentionally or unintentionally, to the powerless transition const
2, class libraries provided by the features can help in the operation carried out checks to ensure that the process of additions and deletions to the operational elements are dished out anomalies
Synchronization Agent
Problems
Object to the introduction of a security thread synchronization mechanisms, has made object in a single-threaded environment, the performance of unnecessary costs, have examples such as writing copy COW
I have seen solutions
Is turning a blind eye, no treatment, the use of synchronization primitives
[C #]
(Public class SomeClass
[MethodImplAttribute (MethodImplOptions.Synchronized)]
Public void Add (string name) (
Attrs.Add (name);
)
)
[Java]
(Public class SomeClass
Public synchronized void Add (string name) (
Attrs.add (name);
)
)
I recommend solutions
The reference libraries, providing no synchronization primitive type, and the synchronization agent; JDK early in the Vector and HashTable are synchronized category, the new ArrayList and HashMap is not synchronized, Collections provide a static method returns synchronization Acting When in a multi-threaded environment necessary to change the set, the use of Agency
[C #, in a multi-threaded environment, the use of synchronous code Agent]
(Public class SomeClass
Public SomeClass (IList source) (
Attrs = ArrayList.Synchronized (source);
)
Public void Add (string name) (
Attrs.Add (name);
)
Public void Remove (string name) (
Attrs.Remove (name);
)
)
[C #, single-threaded environment, the use of synchronous code Agent]
(Public class OtherClass
Public OtherClass (IList source) (
Attrs = source;
)
Public void Add (string name) (
Attrs.Add (name);
)
Public void Remove (string name) (
Attrs.Remove (name);
)
)
[Java, in a multi-threaded environment, the use of synchronous code Agent]
(Public class SomeClass
Public SomeClass (List source) (
Attrs = Collections.synchronizedList (source);
)
Public void add (string name) (
Attrs.add (name);
)
)
[Java, single-threaded environment, the use of synchronous code Agent]
(Public class OtherClass
Public OtherClass (List source) (
Attrs = source;
)
Public void add (string name) (
Attrs.add (name);
)
)
Effect
Having the functions do not need to pay for additional costs
Resource Management
Problems
Sometimes need precise control over the allocation of resources and the timing of the release, ensuring that the resources of abnormal security, to avoid leakage of resources, resulting in deadlock, the paper loss, such as excessive database connection
I have seen solutions
In the absence of real objects and Analysis of the local structure function in the language, try / catch / finally flooding in the code
The use of middleware can help solve some of the resources management, such as database access
AOP may be based on the Resource Management Framework
I recommend solutions
In C + +, automated management of resources is born, that is, the BS "resource management is initialize" (RAII)
In C #, + can be used IDispose made using approximate the effect of RAII
In Java, I do not know, talk about
[C + +, RAII only examples, the paper should be the preferred operating std:: ftream, etc.]
Class File
(
Public:
Explicit File (string path) (
Pf = fopen (path.c_str (), "rwb");
)
~ File () (
Fclose (pf);
)
Operator FILE * () (
Return pf;
)
Private:
FILE * pf;
);
[C + +, the client code RAII only examples, the paper should be the preferred operating std:: ftream, etc.]
Void test ()
(
File file ( "auto.txt");
Char buf [256];
Fread (buf, 0, 256, file); / / Even if this operation will throw an error, the paper will still be closed
)
[C #, just examples]
Public class File: IDisposable (
Private FileStream fs;
Public File (string path) (
Fs = new FileStream (path, FileMode.Create);
)
Public static implicit operator FileStream (File file) (
Return file.fs;
)
Public void Dispose () (
Fs.Close ();
)
)
[C #, just examples]
Public class Test (
Void test () (
Using (File file = new File ( "auto.txt")) (
/ / Some read, write, etc.
)
/ / Document has been closed, even dished out a further operation abnormal
)
)
Effect
1, resource management automation, is not confined to memory
2, the use of C + + templates, uniform definition of the packaging most of the resources of the current C # definition of resources only for each separate category, or use the AOP
Memory Management
Problems
Avoid memory leaks in Java
Solution
Discuss
[Reference: Recommended
1.C # standards: ECMA-334: C # Language Specification
2.Java standards: The Java? Language Specification Second Edition
3.C + + standard: ISO / IEC 14882:2003 Programming Languages - C + +
4.The C # Programming Language
5.The Java Programming Language
6.The C + + Programming Language
Tags: programming, tips








0 Comments to “The Programming Language Tips”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.